#!/bin/bash

## Claire
#downDir=/root/
#script=/root/rapid.sh


## myKubuntu
downDir=/media/data/tmp/
script=~/myScripts/rapid.sh

## SunrayX
#downDir=~/download/
#script=~/download/rapid.sh

## S4U
#downDir=/data/karry/download/
#script=/data/karry/rapid.sh

tmpDir=/tmp
tmpFile=$tmpDir/wget$$
url=$1


## GNU Wget 1.8.2 on Solaris (sunrayX.felk.cvut.cz)
#wget=/usr/local/bin/wget

## GNU Wget 1.11 on Solaris (sunrayX.felk.cvut.cz)
#wget=~/bin/wget

## GNU Wget 1.10.2 on MyKubuntu and vs391.server4u.cz
wget=/usr/bin/wget


## if proxy is used
#export http_proxy=http://proxy.felk.cvut.cz
export http_proxy=""

if [ `hostname | grep sunray` ] ; then
    export http_proxy=http://proxy.felk.cvut.cz
fi



if [ $# -eq 0 ] ; then
    echo "Nebyl zadán žádný parametr. Očekávám linky na vstupu..."
    count=0
    while read param ; do
        if [ -n "$param" ] ; then
            $script $param
            count=$(( $count + 1 ))
        else
            echo "prazdny retezec, nic nestahuju"
        fi
    done
    echo "Končím. celkem bylo zadáno "$count" linků"
    exit
fi


if [ $(echo $url | grep http) ] ; then
    echo "budu stahovat "$1
    echo
else
    echo "nemam co stahovat"
    exit 2
fi

## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## FIRST PAGE

$wget -c -O  $tmpFile $url

secondPage=`cat $tmpFile | grep "form id=\"ff\"" | sed -e 's/.*action="\([^"]*\)".*/\1/'`


## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## SECOND PAGE

rm $tmpFile
echo "jdu nacitat druhou stranku:"
echo $secondPage
echo


#user agent field
USER_AGENT="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20070417 Fedora/2.0.0.3-4.fc7 Firefox/2.0.0.3 MEGAUPLOAD 1.0"

#parse host field
HOST=`echo ${secondPage} | cut -b8- | cut -d'/' -f1`

#header field

header1="Host: ${HOST}"
header2="User-Agent: ${USER_AGENT}"
header3="Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
header4="Accept-Language: en-us,en;q=0.5"
header5="Accept-Encoding: gzip,deflate"
header6="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
header7="Keep-Alive: 300"
header8="Referer: $url"

$wget   --header="${header1}" \
        --header="${header2}" \
        --header="${header3}" \
        --header="${header4}" \
        --header="${header5}" \
        --header="${header6}" \
        --header="${header7}" \
        --header="${header8}" \
        --post-data="dl.start=Free" \
        -O $tmpFile $secondPage

echo
echo druha je stazena...
echo

## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## TEST

echo TEST...

testText1=`zcat $tmpFile | grep "You have reached the download limit for free-users." `
testText2=`zcat $tmpFile | grep "We regret that currently we have no available slots for free users" `
testText3=`zcat $tmpFile | grep "Currently a lot of users are downloading files" `
testText4=`zcat $tmpFile | grep "is already downloading a file" `
testText5=`zcat $tmpFile | grep "There are no more download slots available for free users right now." `

if [[ ( "$testText1" ) || ( "$testText2" ) || ( "$testText3" )  || ( "$testText4" ) || ($testText5) ]] ; then
    echo $testText1
    echo $testText2
    echo $testText3
    echo $testText4
    echo $testText5
    echo "musim cekat... nastavuju cekani na 2 minuty"
    timer=2
    while [ $timer -ge 0 ] ; do
        timer=$(( $timer - 1 ))
        sleep 60
        echo $timer
    done
    $script $@
    exit
else
    echo "...OK"
fi

## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## TIMER

timer=`zcat $tmpFile | grep "var c=" | sed -e "s/var c=\([^;]*\);/\1/" | tr -d "\r\n "`

echo timer je nastaven na: \"$timer\"
#echo "timer nastaven na pevnou hodnotu 100"
#timer=100
timer=$(( $timer + 1 ))

echo $timer


while [ $timer -ge 0 ] ; do
    timer=$(( $timer - 1 ))
    sleep 1
    echo $timer
done


thirdPage=`zcat $tmpFile | grep "<form name=\"dlf\"" | sed -e 's/.*action="\([^"]*\)".*/\1/'`


## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## ------------------------------------------------------------------------------
## MAIN FILE DOWNLOAD

echo "jdu nacitat treti stranku:"
echo $thirdPage
echo


#user agent field
USER_AGENT="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20070417 Fedora/2.0.0.3-4.fc7 Firefox/2.0.0.3 MEGAUPLOAD 1.0"

#parse host field
HOST=`echo ${thirdPage} | cut -b8- | cut -d'/' -f1`

#header field

header1="Host: ${HOST}"
header2="User-Agent: ${USER_AGENT}"
header3="Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
header4="Accept-Language: en-us,en;q=0.5"
header5="Accept-Encoding: gzip,deflate"
header6="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
header7="Keep-Alive: 300"
header8="Referer: $secondPage"

$wget -c  --header="${header1}" \
        --header="${header2}" \
        --header="${header3}" \
        --header="${header4}" \
        --header="${header5}" \
        --header="${header6}" \
        --header="${header7}" \
        --header="${header8}" \
        --directory-prefix=$downDir $thirdPage


echo $tmpFile

