#!/bin/bash -e

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

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

print_header()
{
cat $1 <<-EOF
	:: This file was auto-generated by apt-zip-list ${APTZIPVERSION}
	:: Method: wget-dos - Options: ${OPTIONS}
EOF
}

add_check()
{
cat >>${SCRIPT}.bat <<-EOF
	set to_check=$1
	goto check
	:$1_searched
EOF
[ "$WARNINSTALLED" ] && WARNINSTALLED+=" and "
WARNINSTALLED+="$1"
}

###############
# Stuff dependant on TAR option
# DOS: not yet

###############
# Stuff dependant on restart option
# DOS: not yet

[ "$OPTION_RESTART" != 0 -o "$OPTION_TAR" != 0 ] && error "Sorry, options not supported (yet)"

###############
# Make the batch file for downloading and checking
#
print_header >${SCRIPT}.utils
cat >>${SCRIPT}.utils <<-EOF
	set file_to_check=%2
	goto check_file
:get_file
	wget -t3 -nv -O partial\\%2 %1
	set file_to_check=partial\\%2
	goto check_file
:file_ok
	move partial\\%2 .
EOF

[ "$SLEEPTIME" -ne "0" ] && echo "if %HAS_sleep.exe%==1 sleep $SLEEPTIME" >>${SCRIPT}.utils

cat >>${SCRIPT}.utils <<-EOF
	goto end
:check_file
	if not exist %file_to_check% goto failed
EOF

[ ! "${USECHECKSUMS}" = 'no' ] && cat >>${SCRIPT}.utils <<-EOF
	if "%3"=="" goto checksum_ok
	if "%3"=="0" goto checksum_ok
	if %HAS_sha256sum.exe%==1 echo %3 *%file_to_check% | sha256sum -c
	if errorlevel 1 goto failed
:checksum_ok
EOF

cat >>${SCRIPT}.utils <<-EOF
	if "%file_to_check%"=="partial\\%2" goto file_ok
	echo %2 already downloaded.
	goto end
:failed
	if "%file_to_check%"=="%2" goto get_file
	echo Failed to get %2!
:end
EOF

###############
# Make the main batch file which sets all up
#
SCRIPTNAME=${SCRIPT##*/}
WARNINSTALLED=

print_header >${SCRIPT}.bat
echo "@echo off" >>${SCRIPT}.bat

add_check wget.exe
[ ! "${USECHECKSUMS}" = 'no' ] && add_check sha256sum.exe
[ "$SLEEPTIME" -ne "0" ] && add_check sleep.exe

#FIXME: could not make dir?
cat >>${SCRIPT}.bat <<-EOF
	set to_check=
	if %HAS_wget.exe%==0 goto main_failed
	mkdir partial >NUL
	copy ${SCRIPTNAME}.utils u.bat >NUL
	copy ${SCRIPTNAME} main.bat >NUL
	call main.bat
	set file_to_check=
	del u.bat main.bat >NUL
	goto end
:check
	set HAS_tmp=0
	if exist %to_check% set HAS_tmp=1
	for %%d in (%path%) do if exist %%d\%to_check% set HAS_tmp=1
	if %HAS_tmp%==0 echo Warning: %to_check% not found!
	set HAS_%to_check%=%HAS_tmp%
	set HAS_tmp=
	goto %to_check%_searched
:main_failed
	echo Critical error!
:end
	set HAS_wget.exe=
	set HAS_sha256sum.exe=
	set HAS_sleep.exe=
EOF

###############
# Feed the data to the core function
#
print_header
echo "echo Download will be of size: $DWLSIZE in $PACKCOUNT files"

# '%' needs to be escaped twice here, so it will stay as a character inside a filename
if [ ! "${USECHECKSUMS}" = 'no' ]; then
 	awk '{gsub(/%/,"%%%%",$2); printf "call u.bat \"%s\" %s %s\n",$1,$2,$4}'
else
	awk '{gsub(/%/,"%%%%",$2); printf "call u.bat \"%s\" %s\n",$1,$2}'
fi

warn "Make sure you have $WARNINSTALLED installed on the target machine."
