#!/bin/sh

#
#	Convenience script to get extra things from the web,
#	such as ICC profiles, firmware, etc.
#

#
#       Report an error and exit
#
PROGNAME=$0
error() {
	echo "`basename $PROGNAME`: $1" >&2
	exit 1
}

#
#	Download a .EXE file from the web, unzip it, and extract the
#	files we want
getexe() {
    url="$1"
    exefile="$2"
    what="$3"
    
    wget -O $exefile "$url/$exefile" || error "Couldn't download $url/$exefile"
    unzip -o $exefile "$what"
    rm $exefile
}

getone() {
    case "$1" in
    lj1000|1000)
	getexe \
	    ftp://ftp.hp.com/pub/softlib/software1/lj1488/lj-1145-2 \
	    lj1488en.exe \
	    sihp1000.img
	;;
    new1000)
	getexe \
	    ftp://ftp.hp.com/pub/softlib/software1/COL1032/lj-1230-2 \
	    lj1000hostbased-en.exe \
	    sihp1000.img
	;;
    lj1005|1005)
	getexe \
	    ftp://ftp.hp.com/pub/softlib/software1/COL2222/lj-10067-1 \
	    lj1005hostbased-en.exe \
	    sihp1005.img
	;;
    2200dl|2200)
	getexe \
	    ftp://ftp.minolta-qms.com/pub/crc/out_going/win2000 m22dlicc.exe \
	    "*.icm"
	;;
    2300dl|2300)
	getexe \
	    ftp://ftp.minolta-qms.com/pub/crc/out_going/win m23dlicc.exe \
	    "*.icm"
	;;
    cpwl|pageworks)
	getexe \
	    ftp://ftp.minolta-qms.com/pub/crc/out_going/windows cpplxp.exe \
	    "*.IC_"
	for i in C*.IC_
	do
	    base=`basename $i .IC_`
	    mv $base.IC_ $base.ic_
	    ./msexpand $base.ic_
	    rm -f $base.ic_
	done
	;;
    update)
	url=http://home.mn.rr.com/richardsons/foo2zjs
	file=foo2zjs.tar.gz
	wget -O $file $url/$file || error "Couldn't download $url/$exefile"
	mv getweb getweb.old
	HERE=`pwd`
	cd .. 
	tar zxf $HERE/$file
	cd $HERE
	echo "The tarball is extracted and the current directory is up to date."
	echo -e "Remove the tarball (y/n)? \c"
	read ans
	if [ "$ans" = y ]; then
	    rm -f $file
	fi
	;;
    "")
	error "Try: $0 1000, $0 1005, $0 2200, $0 2300, pageworks or $0 all"
	;;
    *)
	error "Don't know how to get extra stuff for printer $1"
	;;
    esac
}

if [ $# = 0 ]; then
    error "Try: $0 1000, $0 1005, $0 2200, $0 2300, or $0 all"
fi

for i in $*
do
    case "$1" in
    all)
	getone 1000
	getone 1005
	getone 2200
	getone 2300
	;;
    *)	
	getone $i
	;;
    esac
done
