#!/bin/sh
#(c) copyright Barry Kauler 2011, bkhome.org
#Licence 2011 GPL V3 (/usr/share/doc/legal)
#$1 is optional name of an sfs file passed on commandline, download it. ex: devx.sfs
#$1 optional prefix, sub-directory on the official download URL. ex: wary-5.2/devx.sfs
#110402 first version.
#110422 DISTRO_VERSION variable now has dotted format. note, also now using full dotted version# in puppy filenames.
#110813 bug fix, sfs file has '_' in name.
#120202 BK: internationalized.
#120204 more gettext.
#120224 bugfix for racy.
#120224 add slacko support.
#120323 replace 'xmessage' with 'pupmessage'.
#120325 add dir for precise sfs's.
#121025 add case retroprecise.
#121103 file DISTRO_SPECS now has variable DISTRO_DB_SUBNAME.
#130128 also exclude udf and audiocd.

export TEXTDOMAIN=sfsget
export OUTPUT_CHARSET=UTF-8

SFS_FILE=''; SFS_DIR=''
if [ $1 ];then
 [ "`echo "$1" | grep '\.sfs$'`" = "" ] && exit 1
 SFS_FILE="`basename $1`"
 SFS_DIR="dirname $1"
fi

. /etc/rc.d/PUPSTATE #has PUP_HOME
. /etc/DISTRO_SPECS #has DISTRO_BINARY_COMPAT, DISTRO_COMPAT_VERSION DISTRO_DB_SUBNAME
. /root/.packages/DISTRO_PET_REPOS #has PET_REPOS, PACKAGELISTS_PET_ORDER

if [ "`echo -n "$DISTRO_VERSION" | grep '\.'`" = "" ];then #110422
 #convert version to abreviated-dotted, ex1: 142 to 1.4.2  ex2: 150 to 1.5
 for ONECNT in 1 2 3
 do
  ONEDIGIT="`echo -n $DISTRO_VERSION | cut -c $ONECNT`"
  case $ONECNT in
   1)   DOTVER=$ONEDIGIT  ;;
   2)   DOTVER="${DOTVER}.${ONEDIGIT}"  ;;
   3)   [ "$ONEDIGIT" != "0" ] && DOTVER="${DOTVER}.${ONEDIGIT}"  ;;
  esac
 done
else
 DOTVER="$DISTRO_VERSION"
fi

#SFS_DIR0 is the release-dir, SFS_DIR1 as general collection of sfs's...
#MAJNUM="`echo -n $DISTRO_VERSION | cut -c 1`"
MAJNUM="`echo -n "$DOTVER" | cut -f 1 -d '.'`" #110422
case $DISTRO_FILE_PREFIX in
 qrky)
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep '/quirky'`"
  SFS_DIR0="quirky-${DOTVER}"
  SFS_DIR1='sfs'
 ;;
 racy) #120224
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep '/quirky'`"
  SFS_DIR0="racy-${DOTVER}"
  SFS_DIR1='sfs'
 ;;
 wary)
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep '/quirky'`"
  SFS_DIR0="wary-${DOTVER}"
  SFS_DIR1='sfs'
 ;;
 lupu) #playdayz' Lucid Puppy
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep -v '/quirky'`"
  SFS_DIR0="puppy-${DOTVER}"
  SFS_DIR1='pet-packages-lucid'
  SFS_DIR2="sfs_modules-${MAJNUM}"
 ;;
 fd64) #kirk's fatdog64
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep '/fatdog'`"
  SFS_DIR0="sfs/${MAJNUM}00"
 ;;
 slacko) #01micko's spup
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep -v '/quirky'`"
  SFS_DIR0="puppy-${DOTVER}"
  SFS_DIR1="pet_packages-${DISTRO_DB_SUBNAME}" #121103
 ;;
 *precise*) #Ubuntu Precise Pangolin 12.04. 121025 add retroprecise. 121103
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep '/quirky'`"
  SFS_DIR0="${DISTRO_FILE_PREFIX}-${DOTVER}" #121103
  SFS_DIR1="pet_packages-${DISTRO_DB_SUBNAME}" #121103
 ;;
 *)
  x_REPOS="`echo -n "$PET_REPOS" | tr ' ' '\n' | grep -v '/quirky'`"
  SFS_DIR0="${DISTRO_FILE_PREFIX}-${DOTVER}" #121103
  SFS_DIR1="pet_packages-${DISTRO_DB_SUBNAME}" #121103
 ;;
esac
SFS_REPOS0="`echo -n "$x_REPOS" | tr -s ' ' | tr ' ' '\n' | sort --key=1 --field-separator="|" --unique | cut -f 1,2 -d '|'`"
#ex: ibiblio.org|http://distro.ibiblio.org/pub/linux/distributions/quirky
SFS_REPOS="`echo -n "$SFS_REPOS0" | tr '\n' ' '`"
echo "$SFS_REPOS0" > /tmp/petget_repos #used by /usr/local/pteget/testurls.sh

#this is how can probe remote site...
## wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept '*.sfs' -S http://distro.ibiblio.org/quirky/sfs/ 2>&1 | grep '\.sfs$' | rev | cut -f 1 -d '/' | rev
#openoffice-3.1.1-sfs4.sfs
#opera-11.00-1055.i386-SHS.sfs
#samba-3.5.5-lucid-sfs4.sfs
#skype_static-2.1.0.81.sfs
#xaralx_imagemagick-0.7r1785-6.3.7.sfs

#probe remote site...
yaf-splash -placement center -bg yellow -close never -fontsize large -text "$(gettext 'Please wait, probing primary remote site...')" &
YPID=$!
echo -n '' > /tmp/sfsget-probe
echo -n '' > /tmp/sfsget-probe1
echo -n '' > /tmp/sfsget-probe2
xSFS_FILE="$SFS_FILE"
[ ! "$SFS_FILE" ] && xSFS_FILE='*.sfs'
PRIMARYREPO="`echo "$SFS_REPOS0" | grep 'distro\.ibiblio\.org' | head -n 1 | cut -f 2 -d '|'`"
if [ "$SFS_DIR" ];then
 SFS_DIR0="$SFS_DIR"
 wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept "${xSFS_FILE}" -S ${PRIMARYREPO}/${SFS_DIR}/  2>&1 | grep '\.sfs$' | rev | cut -f 1 -d '/' | rev > /tmp/sfsget-probe
else
 wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept "${xSFS_FILE}" -S ${PRIMARYREPO}/${SFS_DIR0}/  2>&1 | grep '\.sfs$' | rev | cut -f 1 -d '/' | rev > /tmp/sfsget-probe
 [ "$SFS_DIR1" ] && wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept "${xSFS_FILE}" -S ${PRIMARYREPO}/${SFS_DIR1}/  2>&1 | grep '\.sfs$' | rev | cut -f 1 -d '/' | rev > /tmp/sfsget-probe1
 [ "$SFS_DIR2" ] && wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept "${xSFS_FILE}" -S ${PRIMARYREPO}/${SFS_DIR2}/  2>&1 | grep '\.sfs$' | rev | cut -f 1 -d '/' | rev > /tmp/sfsget-probe2
fi
sync
kill $YPID

if [ ! -s /tmp/sfsget-probe ];then
 if [ ! -s /tmp/sfsget-probe1 ];then
  if [ ! -s /tmp/sfsget-probe2 ];then
   yaf-splash -placement center -bg red -close box -fontsize large -text "$(gettext 'Either your Internet connection is not working, or the primary repository is down. Aborting SFS Downloader')"
   exit 1
  fi
 fi
fi

if [ ! "$SFS_FILE" ];then
 #ask which sfs to download...
 SFSBUTTONS=''
 for ONESFS in `cat /tmp/sfsget-probe /tmp/sfsget-probe1 /tmp/sfsget-probe2 | sort -u | tr '\n' ' '`
 do
  SFSBUTTONS="${SFSBUTTONS}<radiobutton><label>${ONESFS}</label><variable>RADIO_URL_${ONESFS}</variable></radiobutton>"
 done
 export SFS_DIALOG="<window title=\"$(gettext 'SFS Downloader: choose file')\" icon-name=\"gtk-about\" window_position=\"1\">
 <vbox>
  <text><label>$(gettext 'Please choose an SFS file that you would like to download:')</label></text>
  <frame>
   ${SFSBUTTONS}
  </frame>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox>
 </vbox>
 </window>"
 RETPARAMS="`gtkdialog3 --program=SFS_DIALOG`"
 [ "`echo "$RETPARAMS" | grep '^EXIT' | grep 'OK'`" = "" ] && exit 1
 SFS_FILE="`echo "$RETPARAMS" | grep 'RADIO_URL_' | grep '"true"' | cut -f 1 -d '=' | cut -f 3-9 -d '_'`" #110813 bug fix.
fi
[ "`grep "$SFS_FILE" /tmp/sfsget-probe`" != "" ] && SFS_DIR="$SFS_DIR0"
[ ! "$SFS_DIR" ] && [ "$SFS_DIR1" ] && [ "`grep "$SFS_FILE" /tmp/sfsget-probe1`" != "" ] && SFS_DIR="$SFS_DIR1"
[ ! "$SFS_DIR" ] && [ "$SFS_DIR2" ] && [ "`grep "$SFS_FILE" /tmp/sfsget-probe2`" != "" ] && SFS_DIR="$SFS_DIR2"

#decide where to download to...
#where the save-file is, or if PUPMODE=5 then ask...
DLPART=''; RUNBOOTMGR=''
if [ -L /mnt/home ];then
 DLMOUNT="/mnt/home"
 DLMOUNT1="`readlink /mnt/home`"
 REGEXd=" ${DLMOUNT1}$"
 DLPART="`df | grep "$REGEXd" | cut -f 1 -d ' ' | grep '/dev/' | cut -f 3 -d '/'`" #ex: sda1
fi
if [ "$DLPART" ];then
 RUNBOOTMGR='yes'
 cd $DLMOUNT
else
 DLPARTS="`probepart -m | grep -v -E 'none|iso9660|swap|udf|audiocd' | tr '\n' ' '`" #130128 also exclude udf and audiocd.
 DLBUTTONS=""
 [ "$DLPARTS" = "" ] && DLBUTTONS="<text use-markup=\"true\"><label>\"<b>$(gettext 'NO PARTITIONS FOUND')</b>\"</label></text>"
 for ONEDLSPEC in $DLPARTS
 do
  DEVNODE="`echo -n "$ONEDLSPEC" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
  DEVFS="`echo -n "$ONEDLSPEC" | cut -f 2 -d '|'`"
  DEVSIZEM="`echo -n "$ONEDLSPEC" | cut -f 3 -d '|'`"
  DLBUTTONS="${DLBUTTONS}<radiobutton><label>${DEVNODE} $(gettext 'filesystem:') ${DEVFS} $(gettext 'size:') ${DEVSIZEM}MB</label><variable>RADIO_URL_${DEVNODE}</variable></radiobutton>"
 done
 export DL_DIALOG="<window title=\"$(gettext 'SFS Downloader: destination')\" icon-name=\"gtk-about\" window_position=\"1\">
 <vbox>
  <text><label>$(gettext 'This is a first boot of Puppy and you have not yet saved a session. You want to download') ${SFS_FILE}, $(gettext "and this needs to be downloaded to the same partition in which you will be creating the session save-file. Then, at next boot, Puppy will load the session and will also see the SFS file and be able to load that also (a 'zdrv' will load automatically, others need to be manually selected in te BootManager -- see System menu)")</label></text>
  <text><label>$(gettext 'Here is a list of suitable partitions. Whichever one you choose, at shutdown you will be asked to save the session and be sure to choose the same partition. Well, even if you download it to the wrong place, you can move it afterward! Click the one you want:')</label></text>
  <frame>
   ${DLBUTTONS}
  </frame>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox>
 </vbox>
 </window>"
 RETPARAMS="`gtkdialog3 --program=DL_DIALOG`"
 [ "`echo "$RETPARAMS" | grep '^EXIT' | grep 'OK'`" = "" ] && exit 1
 DLPART="`echo "$RETPARAMS" | grep 'RADIO_URL_' | grep '"true"' | cut -f 1 -d '=' | cut -f 3 -d '_'`"
 REGEXd="/dev/${DLPART} "
 DLMOUNT="`mount | grep "$REGEXd" | cut -f 3 -d ' '`"
 if [ "$DLMOUNT" = "" ];then
  mkdir -p /mnt/${DEVNODE}
  REGEXe="/dev/${DLPART}|"
  DLFS="`echo "$DLPARTS" | grep "$REGEXe" | cut -f 2 -d '|'`"
  mount -t $DLFS /dev/$DEVNODE /mnt/$DEVNODE
  if [ $? -ne 0 ];then
   pupmessage -bg red -center "$(gettext 'ERROR: Could not mount') /dev/${DEVNODE}, $(gettext 'aborting')"
   exit 1
  fi
  DLMOUNT="/mnt/$DEVNODE"
 fi
 cd $DLMOUNT
fi

REPOBUTTONS=""
for ONEREPOSPEC in $SFS_REPOS
do
 URL_TEST="`echo -n "$ONEREPOSPEC" | cut -f 1 -d '|'`"
 URL_FULL="`echo -n "$ONEREPOSPEC" | cut -f 2 -d '|'`"
 REPOBUTTONS="${REPOBUTTONS}<radiobutton><label>${URL_TEST}</label><variable>RADIO_URL_${URL_TEST}</variable></radiobutton>"
done

DLRET=1
while [ $DLRET -ne 0 ];do
 export DEPS_DIALOG="<window title=\"$(gettext 'SFS Downloader: host site')\" icon-name=\"gtk-about\" window_position=\"1\">
<vbox>
 <text><label>$(gettext 'You have chosen to download this SFS file:')</label></text>
 <text use-markup=\"true\"><label>\"<b>${SFS_FILE}</b>\"</label></text>
 <text><label>$(gettext 'It will be downloaded to the top-level (/) in partition') /dev/${DLPART}.</label></text>
 <text><label>$(gettext 'Please choose which URL you would like to download it from:')</label></text>

 <frame>
  ${REPOBUTTONS}
 </frame>
 
 <hbox>
  <button>
   <label>$(gettext 'Test URLs')</label>
   <action>/usr/local/petget/testurls.sh</action>
  </button>
  <button>
   <label>$(gettext 'Download SFS file')</label>
   <action type=\"exit\">BUTTON_SFS_DOWNLOAD</action>
  </button>
  <button cancel></button>
 </hbox>
</vbox>
</window>
" 
 RETPARAMS="`gtkdialog3 --program=DEPS_DIALOG`"

 [ "`echo "$RETPARAMS" | grep 'BUTTON_SFS_DOWNLOAD'`" = "" ] && exit 1

 URL_BASIC="`echo "$RETPARAMS" | grep 'RADIO_URL_' | grep '"true"' | cut -f 1 -d '=' | cut -f 3 -d '_'`"
 DOWNLOADFROM="`cat /tmp/petget_repos | grep "$URL_BASIC" | head -n 1 | cut -f 2 -d '|'`"

 ####DOWNLOAD IT#####
 download_file ${DOWNLOADFROM}/${SFS_DIR}/${SFS_FILE}
 ####################

 DLRET=$?
done #while loop

if [ "$RUNBOOTMGR" = "yes" ];then
 yaf-splash -placement top -bg green -close box -fontsize large -timeout 20 -text "$(gettext "The SFS file has downloaded. You can now choose it in the BootManager (but not a 'zdrv' SFS, that will automatically load), then reboot for it to load")" &
 bootmanager extrasfs
else
 yaf-splash -placement center -bg green -close box -fontsize large -text "$(gettext "The SFS file has been downloaded, however you have to save the session (at shutdown) then on next bootup the BootManager will run and offer the SFS file (a 'zdrv' SFS will automatically load).")" &
fi

exit 0
###END###
