#!/bin/bash
###################################################
# suse-get - A download tool for SuSE Linux packages
#
# Copyright (C) 2003-2004 Markus Gaugusch <suse-get@gaugusch.at>
#
# CVS info:
# $Author: markus $
# $Date: 2004/01/10 22:11:10 $
# $Revision: 1.7 $
#
# 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.
#
##################################################

# USER VARIABLES - CHANGE HERE!
USEFOUCONF=1 # use fou4s.conf, if existing,  to determine download server
LIMIT=20      # maximum number of results
SERVER=http://ftp.gwdg.de/pub/linux/suse/ftp.suse.com/suse
ARCH=i386     # default architecture
# DON'T CHANGE BELOW THIS POINT (or you will have less waranty than "none" ;)
##################################################
# initialisation of global constants
CURDIR=$PWD  # just a backup of PWD
BASENAME=`basename $0`
CONFDIR=$HOME/.suse-get
TMP=$HOME/.suse-get # temporary directory for downloads
#SERVER=http://ftp.leo.org/pub/suse/
INSTALLPKG=0  # install after download
REMOVE=0      # remove after installation
SOURCE=0      # get source rpm
SGVERSION=0.4 # suse-get version number
USEARCHIVES=0 # search for files, not packages
FOUFOUND=0    # if fou4s is found later, this is set to 1 for internal use
FOUCONF=/etc/fou4s.conf # fou4s config file for dl-server
INDEXFILE=$HOME/.suse-get/INDEX.gz
ARCHIVESFILE=$HOME/.suse-get/ARCHIVES.gz


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

options:
-f        Search for filename and not for packagename
-i        Install package after downloading
-q        Quiet (no messages)
-r        Remove after installing
-s        Get source RPM, not binary RPM
--nofou4s Don't use download server from fou4s.conf

__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,nofou4s,file,source,remove,quiet -o fihqrsy \
-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)
		INSTALLPKG=1 ; shift ;;
	-f|--file)
		USEARCHIVES=1 ; shift ;;
	--nofou4s)
		USEFOUCONF=0 ; shift ;;
	-q|--quiet)
		QUIET=1 ; shift ;;
	-r|--remove)
		REMOVE=1 ; shift ;;
	-s|--source)
		SOURCE=1 ; shift ;;
	--)
		shift ;;
	-*)
		echo "Invalid option: $1"
		exit 1 ;;
	*)
		PACKAGES="$PACKAGES ${1//\'}" ; shift ;;
	esac
done

if [ -z "$PACKAGES" ]; then
	echo "Error: No packages 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
	FOUFOUND=1
	ARCH=${REL[3]}
	REL=${REL[1]}
fi

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

#server data
if [ ! -d $CONFDIR ] ; then
	echo "Creating ~/.suse-get for storing search index ..."
	mkdir $CONFDIR
fi
if [ ! -f $INDEXFILE ] ; then
	cd $CONFDIR
	echo "Getting INDEX.gz (only needed once)..."
	wget -q $SERVER/$ARCH/$REL/INDEX.gz
fi
if [ ! -f $ARCHIVESFILE -a $USEARCHIVES -eq 1 ] ; then
	cd $CONFDIR
	echo "Getting ARCHIVES.gz (only needed once)..."
	wget -q $SERVER/$ARCH/$REL/ARCHIVES.gz
fi

if [ $INSTALLPKG -eq 1 ] ; then
	cd $TMP
else
	cd $CURDIR
fi

for PACKAGE in $PACKAGES ; do
	if [ $USEARCHIVES -eq 0 ] ; then
		result=`zgrep "suse\/.*$PACKAGE" $INDEXFILE | grep "\.rpm$"  | head -$((LIMIT*3))`
	else
		result=`zgrep "suse\/.*$PACKAGE" $ARCHIVESFILE | cut -d: -f1|uniq| grep -v -- "--->" | grep "\.rpm$"  | head -$((LIMIT*3))`
	fi
	set -- $result
	COUNT=$#
	if [ $COUNT -gt 1 ] ; then
		#[[ $PACKAGE != *.spm ]] && [[ $PACKAGE != *.src ]] ; then 
		# the user wants an rpm, not an spm
		res=$result
		result=
		c=0
		for i in $res; do
			if [ $SOURCE -eq 0 ] ; then
				[[ $i != *.spm ]] && [[ $i != *.src.rpm ]] && result="$result $i" && c=$((c+1))
			else
				[[ $i == *.spm ]] || [[ $i == *.src.rpm ]] && result="$result $i" && c=$((c+1))
			fi
			test $c -ge $LIMIT && break
		done
		set -- $result
		COUNT=$#
		test $COUNT -ge $LIMIT && fcount=" (first $LIMIT shown):" || fcount=":"
		if [ $COUNT -gt 1 ] ; then
			echo "More than one package matching your input was found$fcount"
			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##./CD1}
	PACKET=${PACKET##./} # remove leading ./
	if [ -z $PACKET ] ; then
		echo "Package 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 package(s), you can find them in $TMP"
	if [ $FOUFOUND -eq 1 ] ; then
		echo "Please run fou4s to check if the installed package is up-to-date. You can also"
		echo "use fou4s -i --only --all rpmname to install an rpm that is available via YOU".
	fi
fi
