#!/bin/ksh

WGET=${WGET:-"wget -nv --passive-ftp --glob=on -O /dev/fd/1"}

url="$1"
case $url in
	ftp://*|http://*)
	print `gettext "Using url "`$1
	;;
	/*)
		if [ -d $url ] ; then
			print `gettext "Using plain directory "`$url
			print `gettext "No need to update catalog"`
			print `gettext "Directory is rescaned each time install window is opened"`
			# good exit
			exit 0
		else
			print "ERROR: directory does not exist: $url"
		fi
	;;
	*)
	print "ERROR: need ftp url. Got $url"
	exit 1
	;;
esac

print `gettext "Attempting to download catalog file. Please be patient..."`

SITE=${url##*//}
SITE=${SITE%%/*}
OSREV=`uname -r`
CPU=`uname -p`

TMPFILE=$DYNAMICDIR/catalog.$$

touch $TMPFILE

if [ -h $TMPFILE ] ; then
	print `gettext "ERROR: hacking attempt on tempfile detected"`
	exit 1
fi


fullurl=$url/$CPU/$OSREV/catalog

$WGET $fullurl >$TMPFILE

if [ $? -ne 0 ] ; then
	print `gettext "ERROR: catalog file download failed"`
	exit 1
fi

print "#PKGADM $url" >/var/pkg-get/catalog-$SITE
if [ $? -ne 0 ] ; then
	print `gettext "ERROR: could not write catalog"`
	exit 1
else
	cat $TMPFILE >>/var/pkg-get/catalog-$SITE
fi
rm $TMPFILE

print `gettext "Catalog download for $SITE successful."`


