#!/bin/sh
[ $(id -u) -ne 0 ] && exec gtksu "Windows Partition Backup Tool" "$0" "$@"
#Basic GUI for ntfsclone.

cd /usr/share/ntfs-backup
backup()
{
rm part2
rm sh1 
rm /tmp/checklist
probepart | grep ntfs | sed 's/|/ /2g' | sed 's/$/\" off \\/g' | sed 's/^/\"/' | sed 's/|/\" " /' | sed '$s/.$/ 2>\/tmp\/checklist/' >> part2
cat part1 part2 part3 >> sh1
chmod 777 sh1
./sh1
PAR=`cat /tmp/checklist`
if [ "$PAR" = "" ] ; then
exit
fi
FILE=`Xdialog --stdout --title "Please choose an image file name and location." --fselect /backup.img.gz 0 0`

case $? in
	0)
		echo "\"$FILE\" chosen";;
	1)
		echo "Cancel pressed."
		exit;;
	255)
		echo "Box closed."
		exit;;
esac

Xdialog --title "NTFS Backup" --stdout --ok-label "Yes" --cancel-label "No" --yesno "Would you like the image file to be split into smaller parts?  " 0 0

if  [ "$?" = "0" ] ;then  
 
rm /tmp/filesize

Xdialog --title "NTFS backup" \
        --radiolist "Please choose the maximum size for the image files." 400x200 0 \
        "699m"  "699MB The most a CD will store." off \
        "1999m" "1999MB The most Fat16 and NFS will store." ON \
        "3999m" "3999MB The most a DVD-R or Fat32 will store." off 2>/tmp/filesize

retval=$?
SIZE=`cat /tmp/filesize`

case $retval in
  0)
   rm nc
   echo "#!/bin/sh" >> nc
   echo "ntfsclone -s -o - $PAR | gzip -c | split -a 3 -b $SIZE - $FILE" >> nc
   echo 'echo -n "Press the ENTER key to close this window."' >> nc
   echo "read goforit" >> nc
   chmod 777 nc
   rxvt -e ./nc   
   ;;
  1)
   exit;;
  255)
   exit;;
esac
 
else
   rm nc
   echo "#!/bin/sh" >> nc
   echo "ntfsclone -s -o - $PAR | gzip -c > $FILE" >> nc
   echo 'echo -n "Press the ENTER key to close this window."' >> nc
   echo "read goforit" >> nc
   chmod 777 nc
   rxvt -e ./nc  
fi
  Xdialog --title "NTFS Backup" --stdout --infobox "Operation finished." 200x150 90000
}

restore()
{
FILE=`Xdialog --stdout --title "Please choose an image file to restore." --fselect / 0 0`

case $? in
	0)
		echo "\"$FILE\" chosen";;
	1)
		exit;;
	255)
		exit;;
esac

if [ -f $FILE ] ; then 

rm part2
rm sh1 
rm /tmp/checklist
probepart | grep ntfs | sed 's/|/ /2g' | sed 's/$/\" off \\/g' | sed 's/^/\"/' | sed 's/|/\" " /' | sed '$s/.$/ 2>\/tmp\/checklist/' >> part2
cat part1r part2 part3 >> sh1
chmod 777 sh1
./sh1
PAR=`cat /tmp/checklist`
if [ "$PAR" = "" ] ; then
exit
fi
Xdialog --title "NTFS Backup" --stdout --ok-label "Yes" --cancel-label "No" --yesno "Is the image file in multiple parts?  " 0 0

if  [ "$?" = "0" ] ;then  

FILE="$(echo $FILE | sed '$s/...$//')"
   rm nc
   echo "#!/bin/sh" >> nc
   echo "cat $FILE* | gunzip -c | ntfsclone -r -O $PAR -" >> nc
   echo 'echo -n "Press the ENTER key to close this window."' >> nc
   echo "read goforit" >> nc
   chmod 777 nc
   rxvt -e ./nc  

else
   rm nc
   echo "#!/bin/sh" >> nc
   echo "gunzip -c $FILE | ntfsclone -r -O $PAR -" >> nc
   echo 'echo -n "Press the ENTER key to close this window."' >> nc
   echo "read goforit" >> nc
   chmod 777 nc
   rxvt -e ./nc  
fi

  Xdialog --title "NTFS Backup" --stdout --ok-label "OK" --infobox "Operation finished." 200x150 90000
else

 Xdialog --title "NTFS Backup" --stdout --infobox "File does not exist" 200x150 90000
 fi
}



#main

SETUP="`Xdialog --title "NTFS Backup" --stdout --cancel-label "Exit" --separator "|" --help "" --menubox "This utility uses ntfsclone to create/restore zlib compressed
 image files of NTFS partitions. Make sure the NTFS partition 
 that you want to backup or restore is not mounted. Also make 
 sure that the destination drive/partition, where the image file is to 
 be stored/restored from, is mounted and has sufficient space. Your 
 destination folder will need free space greater than 60% of the used
 space on the NTFS partition.  Click help for more 
 information about ntfsclone. 

Please choose from the following:" 500x300 5 Backup "Backup a NTFS partition to a image file." Restore "Restore a NTFS partition from an image file."`"
RETVAL=$?
case $RETVAL in
 0) #ok
  if [ "$SETUP" = "Backup" ] ; then 
  backup
  exit 
  else
  restore
  exit
  fi
  exit
  ;;
 1) #cancel
  exit
  ;;
 2) #help
  defaulttexteditor /usr/share/doc/ntfsclone.txt &
  exit 
  ;;
 *)
  exit
  ;;
esac

