#!/bin/bash
###################################################
# suse-get - A download tool for SuSE Linux packages
#
# Copyright (C) 2002-2003 Markus Gaugusch <suse-get@gaugusch.at>
#
# CVS info:
# $Author: markus $
# $Date: 2003/04/22 20:19:49 $
# $Revision: 1.1 $
#
# Optimized for tabstop length 3
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##################################################

# initialisation of global constants
CURDIR=$PWD  # just a backup of PWD
BASENAME=`basename $0`
TMP=~/.suse-get # temporary directory for downloads
#SERVER=http://ftp.leo.org/pub/suse/
SERVER=http://ftp.gwdg.de/pub/linux/suse/ftp.suse.com/suse
ARCH=i386    # default architecture
INSTALLPKG=0 # install after download
REMOVE=0     # remove after installation
SOURCE=0     # get source rpm
SGVERSION=0.2 # suse-get version number
FOUCONF=/etc/fou4s.conf # fou4s config file for dl-server
INDEXFILE=$HOME/.suse-get/INDEX.gz


################################# functions
function showHelp()
{
	cat << __EOF
suse-get v$SGVERSION (c) 2003 Markus Gaugusch <suse-get@gaugusch.at>
usage: suse-get [-i|-q|-s|-y] package 

options:
	-i Install package after downloading
	-q Quiet (no messages)
	-r Remove after installing
	-s Get source RPM, not binary RPM

__EOF
	exit 0
}

################################# main code
#process options

test $# -eq 0 && echo "Run suse-get --help for more information" && exit 1
PARAMETERS=`getopt -l help,install -o ihqrsy -n 'suse-get' -- "$@"`
set -- $PARAMETERS
test $? -ne 0 && echo "Run suse-get --help for more information" && exit 1

while [ -n "$*" ]; do
	case $1 in
	-h|--help)
		showHelp ;;
	-i|--install|install)
		INSTALLPKG=1 ; shift ;;
	-q)
		QUIET=1 ; shift ;;
	-r)
		REMOVE=1 ; shift ;;
	-s)
		SOURCE=1 ; shift ;;
	--)
		shift ;;
	-*)
		echo "Invalid option: $1"
		exit ;;
	*)
		PACKAGES="$PACKAGES ${1//\'}" ; shift ;;
	esac
done

if [ -z "$PACKAGES" ]; then
	echo "Error: No package given!"
	exit 1
fi

#get SuSE release via fou4s
REL=(`SuSE-release`) 2>/dev/null
if [ -z "$REL" ] ; then
	REL=`cat /etc/SuSE-release | grep VERSION | cut -d ' ' -f 3`
else
	ARCH=${REL[3]}
	REL=${REL[1]}
fi

if [ -f $FOUCONF ] ; then
	server=`grep "^Server=" $FOUCONF | head -1`
	if [ -n "$server" ] ; then
		SERVER=${server##*=}
	fi
fi

#server data
if [ ! -f $INDEXFILE ] ; then
	if [ ! -d $INDEXFILE ] ; then
		echo "Creating ~/.suse-get for storing INDEX.gz ..."
		mkdir $HOME/.suse-get
	fi
	cd $HOME/.suse-get
	echo "Getting INDEX.gz (only needed once)..."
	wget -q $SERVER/$ARCH/$REL/INDEX.gz
fi
if [ $INSTALLPKG -eq 1 ] ; then
	cd $TMP
else
	cd $CURDIR
fi
for PACKAGE in $PACKAGES ; do
	result=`zgrep "suse\/.*$PACKAGE" ~/.suse-get/INDEX.gz | head -20`
	set -- $result
	COUNT=$#
	if [ $COUNT -gt 1 ] ; then
		#[[ $PACKAGE != *.spm ]] && [[ $PACKAGE != *.src ]] ; then 
		# the user wants an rpm, not an spm
		res=$result
		result=
		for i in $res; do
			if [ $SOURCE -eq 0 ] ; then
				[[ $i != *.spm ]] && [[ $i != *.src.rpm ]] && result="$result $i"
			else
				[[ $i == *.spm ]] || [[ $i == *.src.rpm ]] && result="$result $i"
			fi
		done
		set -- $result
		COUNT=$#
		if [ $COUNT -gt 1 ] ; then
			echo "More than one package matching your input was found:"
			PS3="Enter number: "
			select LINE in $result ; do
				PACKET=$LINE 
				break
			done
		else
			PACKET=$result
		fi
	else
		PACKET=$result
	fi
	PACKET=${PACKET## } # remove leading blanks
	PACKET=${PACKET##./} # remove leading ./
	if [ -z $PACKET ] ; then
		echo "Pakage not found: $PACKAGE"
		continue
	fi
	wget -c $SERVER/$ARCH/$REL/$PACKET
	FINISHEDLIST="$FINISHEDLIST `basename $PACKET`"
done
if [ $INSTALLPKG -eq 1 -a -n "$FINISHEDLIST" ] ; then
	rpm -Uvh $FINISHEDLIST && rm $FINISHEDLIST || echo "Could not install packages, you can find them in $TMP"
fi

