#!/bin/bash

#simple sfs manager for fatdog 01micko 120618
#requires gtkdialog>=0.80
#131113 L18L internationalisation

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh
# performance tweak - use "C" if there is no localisation
! [ -e $TEXTDOMAINDIR/${LANG%.*}/LC_MESSAGES/$TEXTDOMAIN.mo ] &&
! [ -e $TEXTDOMAINDIR/${LANG%_*}/LC_MESSAGES/$TEXTDOMAIN.mo ] && LANG=C


[ $(id -u) -ne 0 ] && exec gtksu "$(gettext 'SFS Manager')" "$0" "$@"

#set some vars
TMP=/tmp
SFSDEST=$(readlink /mnt/home) # DVD saves is okay - it has /mnt/home symlink
if [ ! $SFSDEST ]; then
	SFSDEST=/tmp/sfs
	mkdir $SFSDEST 2> /dev/null
	xmessage -bg hotpink -c "$(eval_gettext 'Warning: Fatdog is not installed, SFS will be downloaded to $SFSDEST folder.')"
fi

LOADED=/etc/load_sfs
DEFAULTURL=http://distro.ibiblio.org/pub/linux/distributions/fatdog/sfs/700/sfslist
#should we log the downloads?

#splash
xmessage -c -bg yellow -buttons "" "$(gettext 'Please wait')" &
XPID=$!

#setup some temporary config files
curl -Ls $DEFAULTURL > $TMP/sfslist

# sfslist format: filename|md5sum|size MB|description
OIFS=$IFS IFS="|"
while read ALINE; do
	set -- $ALINE
	SFS=$1
	SFSNAME=$(basename $SFS .sfs)
	SIZE=$3
	DESC=$4
	echo "${SFSNAME}|${SIZE}|${DESC}">> $TMP/sfsmgrfilestmp.txt
done < $TMP/sfslist
IFS=$OIFS

#filter logged sfs
while read CLINE; do
	[ -z "$CLINE" ] && continue
	DELLINE=$(basename $CLINE .sfs)
	grep -v $DELLINE $TMP/sfsmgrfilestmp.txt > $TMP/sfsmgrfilestmp1.txt
	mv -f $TMP/sfsmgrfilestmp1.txt $TMP/sfsmgrfilestmp.txt
done < $LOADED

#filter sfs already in /aufs/devsave
ls $SFSDEST|grep sfs$ > $TMP/sfsmgrfiles1.txt
while read DLINE; do
	DELLINE=$(basename $DLINE .sfs)
	grep -v $DELLINE $TMP/sfsmgrfilestmp.txt > $TMP/sfsmgrfilestmp1.txt
	mv -f $TMP/sfsmgrfilestmp1.txt $TMP/sfsmgrfilestmp.txt
done < $TMP/sfsmgrfiles1.txt
mv -f $TMP/sfsmgrfilestmp.txt $TMP/sfsmgrfiles.txt

#kill splash
kill -9 $XPID

#download func
dldfunc()
{
	#repos
	case "$REPO" in
	 "Ibiblio (USA)")URL="http://distro.ibiblio.org/pub/linux/distributions/fatdog/sfs/700/";;
     "AARNET (Australia)")URL="http://mirror.aarnet.edu.au/pub/fatdog/sfs/700/";;
     "UOC (Greece)")URL="http://ftp.cc.uoc.gr/mirrors/linux/fatdog/sfs/700/";;
     "NLUUG (Netherlands)")URL="http://ftp.nluug.nl/ibiblio/distributions/fatdog/sfs/700/";;
    esac
    #echo "getting $SFSCHOICE from $REPO @ $URL" #debug
    #download checksum
	curl -Ls $URL/${SFSCHOICE}.sfs.md5sum.txt > $TMP/sfsmgrchecksum.txt
	[ $? != 0 ] && xmessage -bg hotpink "$(eval_gettext 'ERROR: Failed to download ${SFSCHOICE} checksum')" && exit
	#download
	rxvt -background grey20 -foreground white -title "$(eval_gettext 'downloading ${SFSCHOICE}')" -geometry 80x10  \
	-e wget -t0 --waitretry=5 -4 -P $SFSDEST -c $URL/${SFSCHOICE}.sfs
	[ $? != 0 ] && xmessage -bg hotpink "$(eval_gettext 'ERROR: Failed to download ${SFSCHOICE}')" && exit
	#compare checksums
	DLDCHECK=$(md5sum $SFSDEST/${SFSCHOICE}.sfs|cut -d ' ' -f1)
	REALCHECK=$(cat $TMP/sfsmgrchecksum.txt|cut -d ' ' -f1)
	if [ "$DLDCHECK" != "$REALCHECK" ];then
	  Xdialog --title "$(eval_gettext '${SFSCHOICE} problem')" --yesno "$(eval_gettext 'There is a problem with the ${SFSCHOICE} download') \
	  \n$(gettext 'Do you wish to delete the file and start again? Select "Yes".') \
	  \n$(gettext 'If not you can try again later and the download should resume. \
	  \nNo guarantees! Select "No".')" 0 0
	  case $? in 
	    0)rm -f $SFSDEST/${SFSCHOICE}.sfs && exit ;;
	    *)xmessage -c -bg yellow "$(gettext 'Try your download again, you will need to start over')" && exit ;;
	    esac
	fi
	xmessage -bg lightgreen -c "$(eval_gettext '${SFSCHOICE}.sfs will now be loaded.')"
	load_sfs.sh --load $SFSDEST/${SFSCHOICE}.sfs
	
}

#GUI
export choice="<window title=\"$(gettext 'Choose an SFS')\">
  <vbox>
    <text><label>$(gettext 'Choose your download site')</label></text>
    <comboboxtext>
      <variable>REPO</variable>
      <item>Ibiblio ($(gettext 'USA'))</item>
      <item>AARNET ($(gettext 'Australia'))</item>
      <item>UOC ($(gettext 'Greece'))</item>
      <item>NLUUG ($(gettext 'Netherlands'))</item>
    </comboboxtext>
    <text><label>$(gettext 'These are the sfs files available for download')</label></text>
    <tree>
      <label>$(gettext 'File name') |$(gettext 'Size (MB)')|$(gettext 'Description')</label>
      <variable>SFSCHOICE</variable>
      <input>cat $TMP/sfsmgrfiles.txt</input>
      <action signal=\"button-release-event\">exit:chosen</action>
    </tree>
    <hbox>
      <button cancel></button>
    </hbox>
  </vbox>
</window>"
  
#eval $(gtkdialog -c -p choice -G 420x320)
eval $(gtkdialog -c -p choice -G 780x320)

case $EXIT in 
chosen)rm -f /tmp/sfsmgr* && echo $SFSCHOICE && dldfunc;;
*)rm -f /tmp/sfsmgr* && exit;;
esac
  
#END  
  
