#!/bin/bash -e

error()
{
    echo >&2 "apt-zip/methods/$(basename $0) ERROR: $*"
    exit 1
}

warn()
{
    echo >&2 "apt-zip/methods/$(basename $0) WARNING: $*"
}

###############
# write header
#

cat <<EOF
#!/bin/sh
# This file was auto-generated by apt-zip-list ${APTZIPVERSION}
# Method: wget - Options: ${OPTIONS}
err(){
	echo >&2 "Fetching \$1 failed (\$2)"
}
check(){
        [ ! -r "\$1" ] && return 1
	[ "\`which md5sum\`" ] && 
		if [ "\`md5sum \$1 | cut -d' ' -f1\`" = \$2 ]
		then return 0
		else err \$1 "wrong MD5"; return 1
		fi
	[ "\`which gzip\`" ] &&
		if ar p \$1 data.tar.gz | gzip -t
		then return 0
		else err \$1 "wrong contents"; return 1
		fi
	return \$3
}
EOF

###############
# Differences between gnutar and solaris
#
if [ "$OPTION_SOLARISTAR" != 0 ] ; then
	TAR=${OPTION_SOLARISTAR} 
	TAREXTRACT=xnf 
	TARTEST=tnqif
	TARADD=rnf
elif [ "$OPTION_GNUTAR" != 0 ] ; then
	TAR=${OPTION_GNUTAR}
	TAREXTRACT=xf
	TARTEST=tf
	TARADD=rf
else
	TAR=${OPTION_TAR}
	TAREXTRACT=xf
	TARTEST=tf
	TARADD=rf
fi

[ "$OPTION_RESTART" = 0 -a "$OPTION_GNUTAR" != 0 -a  "$OPTION_SOLARISTAR" = 0 ] && warn "RESTART without SOLARISTAR is currently awfully slow"
###############
# Stuff dependant on RESTART option
#

if [ "$OPTION_RESTART" = 0 ]
then
    cat <<EOF
do_wget() {
	wget -t3 -nv -O \$2 \$1
	check \$2 \$4 \$?
	return \$?
}
EOF
else
    if [ "$OPTION_GNUTAR" != 0 ]
    then
       OLDTARFILE=old-$APTZIPTARFILE
       cat <<EOF
               [ -r $APTZIPTARFILE ] &&
               if [ -r $OLDTARFILE ]
               then
                       $OPTION_GNUTAR Af $OLDTARFILE $APTZIPTARFILE
               else
                       mv $APTZIPTARFILE $OLDTARFILE
               fi
               do_wget() {
                       if [ -r $OLDTARFILE ] && $TAR $TARTEST $OLDTARFILE \$2 >/dev/null 2>&1
                       then
                               $TAR $TAREXTRACT $OLDTARFILE \$2
                               if check \$2 \$4 \$? ; then echo "Already got \$2"; return 0; fi
                       fi
                       wget -t3 -nv -O \$2 \$1
                       check \$2 \$4 \$?
                       return \$?
               }
EOF
    elif [ "$OPTION_TAR" = 0 ] ; then
    cat <<EOF
do_wget() {
        if check ../\$2 \$4 \$? ; then echo "Already got \$2"; return 1; fi
	wget -t3 -nv -O \$2 \$1
	check \$2 \$4 \$?
	return \$?
}
EOF
    else # OPTION_GNUTAR == 0 and OPTION_TAR != 0
       error "RESTART only supported with GNUTAR or without TAR"
    fi
fi

###############
# Stuff dependant on TAR option
#

if [ "$OPTION_TAR" = 0 ]
then
    cat <<EOF
getfile(){
	do_wget \$1 \$2 \$3 \$4 &&
	mv \$2 ../
}
mkdir -p partial && cd partial || error "Cannot change to directory ./partial"
EOF
else
    cat <<EOF
       getfile(){
               do_wget \$1 \$2 \$3 \$4 &&
               $TAR $TARADD $APTZIPTARFILE \$2 &&
               rm \$2
       }
       touch foo-stamp && $OPTION_TAR cf $APTZIPTARFILE foo-stamp && rm foo-stamp
EOF
fi

###############
# Feed the data to the core function
#

cat <<-EOF
       while read URL FILE SIZE MD5
       do getfile \$URL \$FILE \$SIZE \$MD5 || true
       done <<EOP
EOF

###############
# The data itself
#
cat

###############
# End-Of-Packages marker
#
echo "EOP"
