#!/bin/sh
# $Id: apt-get-freebsd.sh 58 2004-08-11 08:31:57Z taal $
# This is a apt-get a'like script for OpenBSD packages :)
# TODO: merge with apt-get-freebsd.sh to get apt-get-bsd.sh ?

# This should autodetect the OpenBSD release fine, however you can of course
# specify another OpenBSD mirror which is faster for you 
RELEASE=$(uname -r)
HARDWARE=$(uname -m)
BASEURL="ftp://ftp.openbsd.org/pub/OpenBSD/${RELEASE}/packages/${HARDWARE}/"

# You have to modify this variable to point to a location where
# you have write permission as well as a lot of space (let's say
# at least 500 Mb) for storing all downloaded packages
#CACHEDIR="/var/cache/apt-get-openbsd"
CACHEDIR="/home/taal/cache/apt-get-openbsd"

#. /home/taal/scripts/functions.sh

#################
# Other Variables
MYDIR=`pwd`
ME="$0"
WGET=/usr/local/bin/wget
PKGDIR="/var/db/pkg"
INDEXFILE="index.txt"
PACKEXT="tgz"
TAR="tar -xzf"

# Subroutines
usage() {
	echo
	echo "usage: `basename $0` \$command \$packagename [\$packagename] ..."
	echo 
	echo "example 1: `basename $0` s[earch] vim"
	echo "example 2: `basename $0` i[nstall] vim"
	echo
	exit
}

choose_downloader() {
	if [ -x $WGET ]; then
		FETCH=$WGET
		ARGS="-c"
	else
		FETCH=/usr/bin/ftp
		ARGS="-V -m"
	fi
}

check_index() {
	if [ ! -d "$CACHEDIR" ]; then
		echo "=> Cachedir: $CACHEDIR not found, creating now..."
		mkdir -p "$CACHEDIR" && echo "OK" || { echo failed, exiting...; exit 2; }
	fi
	if [ ! -f "${CACHEDIR}/${INDEXFILE}" ]; then
		echo "=> Indexfile '${INDEXFILE}' not found, getting it now..."
		cd "$CACHEDIR"
		$FETCH "${BASEURL}/${INDEXFILE}"
	fi
}

search_index() {
	lookfor="$1"
	echo -n "=> Searching for '$lookfor'..."
	RESULT=`grep -i "$lookfor" "${CACHEDIR}/${INDEXFILE}" | cut -f 1 -d '|'`
	if [ x"$RESULT" = x"" ]; then
		echo "not found."
	else
		echo "=> found the following results:"
		echo "========"
		echo "$RESULT"
		echo "========"
	fi
}

install_package() {
	NUMRESULTS=`echo "$RESULT" | wc -l | xargs echo`
	if [ "$NUMRESULTS" -eq 1 -a ! x"$RESULT" = x"" ]; then
		INSTALLPACK="$RESULT"
	else
		echo "!! I don't know what to install"
		exit 1
	fi
	echo "Going to install $INSTALLPACK"
	THISPACK="$BASEURL/$INSTALLPACK"
  gzip -t "$CACHEDIR/$INSTALLPACK" > /dev/null 2>&1
  GZIPRET=$?
  TARERROR=$(tar tzf "$CACHEDIR/$INSTALLPACK" 2>&1 > /dev/null)
	if [ x"$GZIPRET" = x"0" -a -z "$TARERROR" ]; then
		echo "=> found package in archive: $INSTALLPACK"
	else
		echo "=> package not found or download incomplete, downloading it now..."
		$FETCH $ARGS "$THISPACK"
	fi
	DEPS=$($TAR "$INSTALLPACK" +CONTENTS && grep depend +CONTENTS | sed -e 's/.*:\(.*\)$/\1/')
	echo -n "Checking Dependencies: "
	echo $DEPS
	for d in $DEPS; do
		if [ ! -d "$PKGDIR/$d" ]; then
			echo "=> Installing dependency: $d..."
			cd "$MYDIR" && $ME install $d
		else
			echo "=> OK: $d already installed"
		fi
	done
	echo "=> Installing $INSTALLPACK now..."
  # TODO:second time we're doing this test, put in subroutine
  gzip -t "$CACHEDIR/$INSTALLPACK" > /dev/null 2>&1
  GZIPRET=$?
  TARERROR=$(tar tzf "$CACHEDIR/$INSTALLPACK" 2>&1 > /dev/null)
	if [ x"$GZIPRET" = x"0" -a -z "$TARERROR" ]; then
	  cd "$CACHEDIR" && pkg_add "$INSTALLPACK"  && echo "OK" || { echo "!! something went wrong..."; }
  else
    echo "!! packagefile $INSTALLPACK appears corrupt! won't install."
  fi
}

# Main() :

choose_downloader
check_index
cd "$CACHEDIR"

if [ $# -lt 2 ]; then
	usage
fi

command="$1"
shift

case "$command" in
	s*)
		for package in "$@"; do
			search_index "$package"
    done
		;;
	i*)	
		for package in "$@"; do
			search_index "$package"
      package=$(echo "$package" | sed -e 's/\.$PACKEXT\$//')
		  install_package "$package"
    done
		;;
	*)
		usage
		;;
esac
exit
