#!/bin/sh

if [ "x$1" = "x-h" -o "x$1" = "x--help" ] ;then
    echo "$0 [-h|--help] [-C DIR] [-size] urls";
    echo "\tuse -h|--help to display this and exit";
    echo "\tuse -C to change the default directory ~/movie/ to DIR";
    echo "\tuse -size to auto remove uncompelete downloaded file,and redownload them";
    echo "\tthe arguments' order should be same as above mentioned";
    exit;
fi

get_url_size(){
	( wget -U "Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.8.131 Version/11.10" $1 -O - |dd of=/dev/zero bs=1 count=1 2>&1 ) 2>&1 |head -n 5 |tail -n 1|perl -pe 's/^\D+//;s/ .+\n$//;'
	}
get_size(){
    ls -l $1|cut -d ' ' -f 5|tr -d '\n'
}
consider_file_size=0

DIR=~/movie/;
[ "x$1" = "x-C" ] && shift && DIR=$1 && shift;
cd $DIR;

[ "x$1" = "x-size" ] && consider_file_size=1 && shift
temp_file=$(mktemp down_flash-tempfile.XXXXX)
wget -q 'http://www.flvcd.com/parse.php?flag=&format=high&kw='"$1"'&sbt=%BF%AA%CA%BCGO%21' -O $temp_file;
content=$(wget "184.82.41.44/downloads" --post-file $temp_file -O - -q |base64 -d);
rm $temp_file;
[ "x$content" = "xNIL" ] && echo 'geted NIL\nexit' && exit 1;
name=$(echo $content|cut -d ' ' -f 1|sed 's/\///g');

[ -e $name ] ||mkdir $name;
cd $name;
total=$(($(echo $content|wc -w)/2));

/bin/echo -e "\e[31m\e[1m$name\e[0m==================\n";
j=0;

r=0;
echo $content|cut -d ' ' -f 2-|xargs -n2 echo|
while read i;
do
    j=$((j+1))
    /bin/echo -e "\e]2;$j/$total\a";
    url=$(echo $i|cut -d ' ' -f 1);
    file=$(echo $i|cut -d ' ' -f 2);
    # echo "[$url\t$file]";
    if [ $consider_file_size -gt 0 -a -e $file ] ;then
	echo "checking $file....";
	remote_size=$(get_url_size $url);
	local_size=$(get_size $file);
	if [ $remote_size -gt $local_size ] ;then
	    echo "rm $file";
	    rm $file;
	    wget -t 1 -T 3 -U "Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.8.131 Version/11.10" -nc  $url -O $file ||r=$((1+r));
	fi
    else
	wget -t 1 -T 3 -U "Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.8.131 Version/11.10" -nc  $url -O $file||r=$((1+r));
    fi
    /bin/echo -e "\e[31m\e[1m$name\e[0m==================\n";
done;

/bin/echo -e "\e]2;down_flash finished\a";
exit $r;