#!/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/
ARCH=i386    # default architecture
INSTALLPKG=0 # install after download
REMOVE=0     # remove after installation
SOURCE=0     # get source rpm
SGVERSION=0.1 # suse-get version number
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 iqrsy -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 ;;
	*)
		PACKAGE=${1//\'} ; shift ;;
	esac
done

#get SuSE release
REL=`cat /etc/SuSE-release | grep VERSION | cut -d ' ' -f 3`

#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
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 "Not found: $PACKAGE"
	exit 1
fi
if [ $INSTALLPKG -eq 1 ] ; then
	cd $TMP
else
	cd $CURDIR
fi
wget $SERVER/$ARCH/$REL/$PACKET
if [ $INSTALLPKG -eq 1 ] ; then
	echo rpm -Uvh $TMP/`basename $PACKET`
	echo rm $TMP/`basename $PACKET`
fi

