#!/bin/sh
#www.ircle.com/cgi-bin/download.cgi

Redirect()
{
        echo -e "Content-type: text/html\r"
        echo -e "\r"
        echo -e "<HTML><HEAD><META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=http://www.ircle.com/download.php\"></HEAD><HTML>"
	exit
}

ref=${HTTP_REFERER}

if [[ $ref != "" ]]; then
	res=`expr $ref : ".*www\.ircle\.com"`
	if (( $res == 0 )); then
		Redirect
	fi
fi


# 68KPPCOSX type
# USFRDEUKIT land
# $1 is LLVVV[M] waar L=language, V=version, M optioneel mirror (A..Z)

arg=$1
arg=`echo -n $arg | tr [:lower:] [:upper:]`

dir="/home/ircle/www/geheim/"
# file op de server
fileS="ircle"
# file zoals voor de user gepresenteerd
fileU="ircle"
version="3.1.2"
suf1=".sit.bin"
suf2=".dmg"
errorpage="/download.php"
stats="/home/ircle/downloadstats/"

UpdateCounter() {
	if [[ $REQUEST_METHOD == "GET" ]]; then
		if (( ${#arg} < 7 )); then
			f=$stats$arg
			declare -i counter
			if [[ -e $f ]]; then
				read counter<$f
				counter=$counter+1
				printf "%6d\n" $counter >$f
			else
				printf "%6d\n" 1 >$f
			fi
		fi
	fi
}

ErrorPage() {
        echo -e "Content-type: text/html\r"
        echo -e "\r"
        echo -e "<HTML><HEAD><META HTTP-EQUIV=\"refresh\" CONTENT=\"1; URL=$errorpage\"></HEAD><HTML>"
}

t=${arg:0:2}
if   [[ $t == "US" ]]; then
	lang="English"
elif [[ $t == "FR" ]]; then
	lang="Francais"
elif [[ $t == "DE" ]]; then
	lang="Deutsch"
elif [[ $t == "UK" ]]; then
	lang="British"
elif [[ $t == "IT" ]]; then
	lang="Italiano"
elif [[ $t == "DK" ]]; then
	lang="Dansk"
else
	lang="unknown"
fi


t=${arg:2:3}
if   [[ $t == "68K" ]]; then
	os="68k"
	suf=$suf1
elif [[ $t == "PPC" ]]; then
	os="PPC"
	suf=$suf1
elif [[ $t == "OSX" ]]; then
	os=""
	suf=$suf2
else
	os=""
	suf=""
fi

t=${arg:5:1}
#mirrors
if [[ $t == "A" ]]; then
#	mirror="http://www.mirrors.ausmac.net/ftp/Communication-SW/Ircle/"
	mirror=""
elif [[ $t == "Y" ]]; then
	mirror=""
	fileS="ircle_4.0"
	version="4.0a5"
elif [[ $t == "Z" ]]; then
	mirror=""
	fileS="ircle_alpha"
	version="3.5a6"
else
	mirror=""
fi

ua=${HTTP_USER_AGENT:0:4}
if [[ $lang == "" || $suf == "" || $ua == "Java" ]]; then
	ErrorPage
else
	if [[ $mirror == "" ]]; then
		if [[ $os == "" ]]; then
			download=$dir${fileS}_OS_X$suf
			filename=$fileU\ $version$suf
		else
			download=$dir${fileS}_${lang}_$os$suf
			filename=$fileU\ $version\ $lang\ $os$suf
		fi

		if [[ -e $download ]]; then
			# echo $download
			FILESIZE=`ls -l $download |awk '{print $5}'`

			echo -e "Content-Disposition: attachment; filename=\"$filename\"\r"
			echo -e "Pragma: no-cache\r"
			echo -e "Cache-Control: no-cache, must-revalidate\r"
			echo -e "Content-type: application/octet-stream\r"
			echo -e "Accept-Ranges: none\r"
			echo -e "Content-Length: $FILESIZE\r"
			echo -e "\r"
			cat $download
			er=$?
			if [[ $er > 0 ]]; then
				dat=`date +%Y-%m-%d\ %H:%M:%S`	
				echo $dat: $REMOTE_ADDR "error" $er "downloading" $filename>>$stats/errors
			else
				UpdateCounter
			fi
		else
			ErrorPage
		fi
	else
		if [[ $os == "" ]]; then
			download=${file}_OS_X$suf
		else
			download=${file}_${lang}_$os$suf
		fi

		UpdateCounter
		
		echo -e "Content-type: text/html\r"
		echo -e "\r"
		echo -e "<HTML><HEAD><META HTTP-EQUIV=\"refresh\" CONTENT=\"1; URL=${mirror}${download}\"></HEAD><HTML>"
	fi
fi
