#!/bin/bash
# rcrsn51 2012-07-01
# jamesbond Jan 2013, 2015 - fixes
#131117-2015-10-23 L18L internationalisation

export TEXTDOMAIN=fatdog

# 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

TITLE="$(gettext 'Savefile Argument Builder')"
[ $(id -u) -ne 0 ] && exec gtksu "$TITLE" "$0" "$@"

export MAINDIALOG='
<window title="'$TITLE'">
<vbox>
  <text><label>'$(gettext "Use this applet if you have created a savefile but your bootloader cannot find it.")' </label></text>
  <frame '$(gettext "Instructions")'>
    <text xalign="0"><label>'$(gettext "1. Mount the required partition(s).")'</label></text>
    <text xalign="0"><label>'$(gettext "2. Move your savefile to the correct location.")'</label></text>
  </frame>
  <frame '$(gettext "3. Drag your savefile into this box")'>
    <hbox>
      <entry><variable>SAVEFILE</variable></entry>
    </hbox>
  </frame>
  <frame '$(gettext "4. Select the device type")'>
    <hbox>
      <radiobutton> 
        <variable>HDD</variable>
        <label>'$(gettext "Hard Drive (device)")'</label>
      </radiobutton>
      <text space-expand="true" space-fill="true"><label>""</label></text>
      <radiobutton>    
        <variable>USB</variable>
        <label>'$(gettext "USB (uuid)")'</label>
      </radiobutton> 
    </hbox>
  </frame>
  <hbox>
    <text><label>'$(gettext "Next step?")'</label></text>
    <button yes></button>
    <button cancel></button>
  </hbox>
</vbox>
</window>
'
export REPORTDIALOG='
<window title="'$TITLE'">
<vbox>
  <text><label>'$(gettext "Here is your savefile argument:")'</label></text>
  <entry><input>cat /tmp/savefile-argument.txt</input></entry>
  <frame '$(gettext "Instructions")'>  
    <text xalign="0"><label>'$(gettext "1. Leave this window open.")'</label></text>
    <text xalign="0"><label>'$(gettext "2. Use the Ctrl-A, Ctrl-C and Ctrl-V keys to copy and paste it into your GRUB menu.lst file.  Add it to the end of the \"kernel\" line.")'</label></text>
    <text xalign="0"><label>'$(gettext "3. For a USB syslinux.cfg file, add it to the \"append\" line.")'</label></text>
  </frame>
  <hbox><button use-stock="true" label="gtk-quit"></button></hbox>
</vbox>
</window>
'

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program MAINDIALOG); do
  eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "Cancel" -o "$EXIT" = "abort" ] && exit 1

# foolproof way to split find savedevice and savefile path
mountpoint= prevpath=
savefile=${SAVEFILE#/}
until [ "$prevpath" = "$savefile" ]; do 
	mountpoint=${mountpoint}/${savefile%%/*}
	prevpath=$savefile
	savefile=${savefile#*/}
	#echo $mountpoint -- $fullpath
	busybox mountpoint -q $mountpoint && break
done
partition=$(busybox mountpoint -n $mountpoint | awk '{print $1}')

if [ ${partition%/*} ] && [ ${partition%/*} == "/dev/mapper" ]; then # LVM, translate to /dev/dm-x
	dmdev=$(ls -l /dev/mapper 2> /dev/null | awk -v dmdev=${partition##*/} '$10 == dmdev {print "/dev/dm-" $6; exit; }')
	[ "$dmdev" ] || dmdev=$(ls -l /dev/mapper 2>/dev/null | awk -v dmdev=${partition##*/} '$(NF-2) == dmdev {sub(/.*\//,"",$NF); print "/dev/" $NF; exit;}')
	partition=$dmdev
fi

if [ "$USB" = "true" ]; then
	uuid=$(blkid -s UUID -o value $partition)
	if [ -z "$uuid" ]; then
		Xdialog --title " " --msgbox "$(gettext 'I could not identify the UUID of this savefile!')" 0 0
		exit 1
	fi
fi

if [ "$HDD" = "true" ]; then
  ARGUMENT="savefile=direct:device:"${partition#/dev/}":"$savefile
else
  ARGUMENT="savefile=ram:uuid:"$uuid":"$savefile" waitdev=5"
fi

echo $ARGUMENT > /tmp/savefile-argument.txt

I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program REPORTDIALOG); do
  eval $STATEMENTS
done
IFS=$I
