#!/bin/sh

#my own humble download accelerator frontend for axel.
#(c) copyright 2004 Barry Kauler www.goosee.com/puppy

#choices: 
# multiple servers or one
# if one, then number connections
# destination path.

COMMANDTAIL="$@"

if [ "$COMMANDTAIL" ];then
 #this means that a url has been passed in on the commandline.
 echo "$COMMANDTAIL" | grep -i "http://"
 if [ ! $? -eq 0 ];then #=0 if found.
  echo "$COMMANDTAIL" | grep -i "ftp://"
  if [ ! $? -eq 0 ];then
   Xdialog --wmclass "puppyaxel" --title "Puppy download accelerator" --no-buttons --icon "/usr/local/lib/X11/pixmaps/warning24.xpm" --infobox "The URL is incomplete. It needs http:// or ftp:// prefix." 0 0 20000 &
  fi
 fi
fi

DOSETUPLOOP="true"
while [ "$DOSETUPLOOP" = "true" ];do
 DOSETUPLOOP="false"

RETSTR=`Xdialog --wmclass "puppyaxel" --title "Puppy Axel download accelerator" --stdout --left --separator "|" --icon "/usr/share/images/ftp64.png" --check "Bypass any proxy server" off --3inputsbox "これはAxelダウンロードアクセラレータのフロントエンドです。\n同じサーバへ複数接続が可能です。\n複数のサーバから同時にダウンロードもできます。\n\n注意: クリップボードからは、CTRL-V でペーストできます。\nftp:// や http:// の接頭辞を忘れずに。\nbypass-proxyチェックボックスはチェックを入れないでおく事を推奨します。" 0 0 "第1サーバURL:" "$COMMANDTAIL" "第2サーバURL (オプション):" "" "第3サーバURL (オプション):" ""`

if [ $? -eq 0 ];then
URL1=`echo -n "$RETSTR" | head -n 1 | cut -f 1 -d "|"`
URL2=`echo -n "$RETSTR" | head -n 1 | cut -f 2 -d "|"`
URL3=`echo -n "$RETSTR" | head -n 1 | cut -f 3 -d "|"`
BYPASSPROXY=`echo -n "$RETSTR" | tail -n 1`
else
 exit
fi

MULTISERVE="$URL2$URL3"
if [ ! "$MULTISERVE" ];then
 MULTICONN=`Xdialog --wmclass "puppyaxel" --title "Puppy Axel download accelerator" --stdout --no-tags --radiolist "サーバーへの一つ以上の同時接続を選んで下さい。\n複数の接続はダウンロードを速くします。しかし、\n遅いダイヤルアップ接続はまだボトルネックであるかもしれません。\n(ブロードバンドを持つ場合だけ、3つ以上を選んで下さい)。\nまた、ダウンロード加速が試みられるならば、\n接続が切断されるサーバもあります。" 0 0 0 "1" "1サーバ接続、加速なし" on "2" "2つ同時接続" off "3" "3つ同時接続" off "4" "4つ同時接続" off`
fi

if [ ! $? -eq 0 ];then
 exit
fi

 DESTDIR="`Xdialog --wmclass "puppyaxel" --title "目的のディレクトリを選択して下さい:" --wizard --no-cancel --help "if you leave the Selection field blank, the destination filename\nwill be the same as the source filename.\nOtherwise, type in the new name for the downloaded file." --stdout --dselect "/root/" 0 0 `"

 RETVAL=$?
 #note, returns 3 if "previous" button clicked.
 if [ $RETVAL -eq 3 ];then #=0 "next".
  DOSETUPLOOP="true"
 else
  if [ ! $RETVAL -eq 0 ];then
   exit
  fi
 fi

done

#now do the actual download...

ls "$DESTDIR" 2>&1 | grep "No such file or directory"
if [ $? -eq 0 ];then
 #this means that dest is not an existing file nor directory, so must
 #have typed a filename into directory selection dialog.
 FULLDEST="$DESTDIR"
else
 file $DESTDIR | grep "directory"
 if [ $? -eq 0 ];then
  #DESTDIR is only a path. get filename off url...
  FILEDEST=`basename "$URL1"`
  FULLDEST="$DESTDIR$FILEDEST"
 else
  #dest filename must already exist.
  FULLDEST="$DESTDIR"
 fi
fi

if [ "$BYPASSPROXY" = "checked" ];then
 BYPASSPROXY="--no-proxy"
else
 BYPASSPROXY=""
fi

echo -e "#!/bin/sh
axel $BYPASSPROXY --alternate --verbose --num-connections=\"$MULTICONN\" --output=\"$FULLDEST\" \"$URL1\" $URL2 $URL3
if [ ! \$? -eq 0 ];then
 echo
 echo \"DOWNLOAD FAILURE!\"
 if [ -f $FULLDEST ];then
  echo \"The file has not been fully downloaded.\"
  echo -n \"Deleting partially downloaded file...\"
  echo \" done\"
 else
  echo \"The file has not been downloaded.\"
 fi
 echo
 echo -n \"Press ENTER key to exit: \"
 read nnn
fi
" > /tmp/axelrun.sh

chmod 755 /tmp/axelrun.sh
rxvt -name puppyaxel -bg orange -geometry 80x10 -title "Puppy Axel download accelerator" -e /tmp/axelrun.sh

