#!	/bin/sh

# Copyright (c) 2005 The SCO Group, Inc. All Rights Reserved.

URL=http://www.sco.com/
VERSION=6.0.0

BUNDLES=$HOME/bundles
BIN=$HOME/bin
DOC=$HOME/doc
RC=$BIN/.kstuff.rc
CATALOG=$BIN/catalog
SITE_PREFIX=developers/hdk/testsuites/
KSTUFF_PREFIX=${SITE_PREFIX}OpenServer_6/kstuff/
VERS_LOC=${URL}${KSTUFF_PREFIX}/${VERSION}
SCRAPER_LOC=${URL}${SITE_PREFIX}
DOCS="
	kstuff.1.txt
	kstuff.cmpflgs.txt
	README.html
	README.txt
	kstuff_kdb.txt
"
TAR=${VERS_LOC}/kstuff.tar

enter() {
	INPUT=
	while [[ -z "$INPUT" ]]
	do
		echo "Enter ${1}: \c" >&2
		read INPUT x
	done
	echo $INPUT
}

question() {
	while [ 1 ]
	do
		echo "${1}? \c" >&2
		read ans
		case "$ans" in
			[yY]*) return 0 ;;
			[nN]*) return 1 ;;
			*) echo 'Please answer "y" or "n".' >&2 ;;
		esac
	done
}

_mkdir() {
	dir=$1
	if [ ! -d $dir ]; then
		mkdir -p $dir
	fi
}

_download() {
	if [ $1 = "-s" ]; then
		opts="-s -S"
		src=$2
		dest=$3
	else
		src=$1
		dest=$2
		echo
		echo "Beginning download of: `basename $dest`"
		opts="-#"
	fi
	/bin/curl $opts -o $dest $src || {
		echo
		echo Download of $src failed.
		rm -f $dest
		exit 2
	}
}

_vers_to_title() {
	echo "$@" | awk '
		$1 ~ /^osr/ { title = "OpenServer"; vers = substr($1, 4) }
		$1 ~ /^uw/ { title = "UnixWare"; vers = substr($1, 3) }
		{
			n = split(vers, ver, "mp")
			j = length(ver[1])
			for (i = 1; i <= j; ++i) {
				if (i == 1)
					sep = " "
				else
					sep = "."
				title = title sep substr(ver[1], i, 1)
			}
			if (n == 2)
				title = title " MP" ver[2] 
			print title
		}
	'
}

_get_bundle_names() {
	awk '
		BEGIN {
			pattern1 = "<a href=\""
			len = length(pattern1)
			pattern2 = ".Z\">"
		}
		{
			i = index($0, pattern1)
			j = index($0, pattern2)
			if (i == 0 || j == 0)
				next
			bundle = substr($0, i + len, j - i - len + 2)
			n = split(bundle, path, "/")
			if (path[n - 1] != "bundles")
				next
			n = split(path[n], bun, ".")
			if (n != 3)
				next
			release[bun[1]] = release[bun[1]] " " bundle
		}
		END {
			for (rel in release) {
				printf "_load_bundles %s %s; \n", \
					rel, release[rel]
			}
		}
		
	' $1
}

_load_bundles() {
	rel=$1
	shift
	TITLE="`_vers_to_title $rel`"
	case $RESPONSE_TYPE in
	1)
		echo "     $TITLE"
		return
		;;
	2) 
		if question "Do you wish to download bundles for $TITLE"
		then
			echo $rel
		fi
		;;
	3) 
		echo $KTYPES | grep $rel > /dev/null 2> /dev/null || {
			return
		}
		for pathname in $@; do
			base=`basename $pathname`
			ktype=`echo $base | cut -d. -f2`
			_download ${URL}${pathname} ${BUNDLES}/${base}
			cat << --eof-- >> $CATALOG
::$BUNDLES/${rel}.${ktype}.Z:1:$TITLE $ktype Kernel
--eof--
		done
		;;
	esac
}

if [ ! -x /bin/curl ]; then
	echo "Cannot download. /bin/curl is missing." >&2
	echo "SETUP is intended for use on UW714 and OSR6 systems." >&2
	exit 1
fi

# make local directories
_mkdir $BUNDLES
_mkdir $BIN
_mkdir $DOC

# download the kstuff.tar and unwind it in the user's bin directory
cd $BIN
_download -s ${TAR} ${BIN}/kstuff.tar
cd $BIN
echo "Downloading tools and documentation ... \c"
tar xf kstuff.tar
rm -f kstuff.tar

# setup .kstuff.rc and an empty catalog
echo "-d $CATALOG" > $BIN/.kstuff.rc
echo "\c" > $CATALOG

# downlaod docs into the user's doc directory
for doc in $DOCS; do
	_download -s ${VERS_LOC}/${doc} ${DOC}/${doc}
done
echo "Done."
PRINT_ONLY=1

echo

#
# Get a list of available bundles from the server
#
echo "Checking on available bundles ... \c"
cd ${BUNDLES}
_download -s ${SCRAPER_LOC} .bundle_list
echo "Done."
BUNDLE_NAMES="`_get_bundle_names .bundle_list`"
rm -f .bundles_list
RESPONSE_TYPE=1
echo
echo Found bundles for the following kernels:
eval "$BUNDLE_NAMES"
echo

#
# Ask for permission to download for each type
#
RESPONSE_TYPE=2
echo
KTYPES=`eval "$BUNDLE_NAMES"`

#
# Download bundles
#
RESPONSE_TYPE=3
eval "$BUNDLE_NAMES"
