#!/bin/bash
# simple gui for Handbrake.  
DVD=/dev/dvd # change if needed.
NAME="Movie" #default name
# Puppy has different versions of gtkdialog
GTKDIALOG=""
if [ "`which gtkdialog3`" != "" ]; then
GTKDIALOG=gtkdialog3
elif [ "`which gtkdialog`" != "" ]; then
GTKDIALOG=gtkdialog
elif [ "$GTKDIALOG" = "" ]; then
echo "you need to install gtkdialog"
exit 0
fi

# Find a terminal
TERMINAL=""
if [ "`which rxvt`" != "" ]; then
TERMINAL=rxvt
elif [ "`which urxvt`" != "" ]; then
TERMINAL=urxvt
elif [ "`which mrxvt`" != "" ]; then
TERMINAL=mrxvt
elif [ "`which aterm`" != "" ]; then
TERMINAL=aterm
elif [ "`which materm`" != "" ]; then
TERMINAL=materm
elif [ "`which xterm`" != "" ]; then
TERMINAL=xterm
elif [ "`which Eterm`" != "" ]; then
TERMINAL=Eterm
elif [ "`which sakura`" != "" ]; then
TERMINAL=sakura
elif [ "`which konsole`" != "" ]; then
TERMINAL=konsole
elif [ "`which gnome-terminal`" != "" ]; then
TERMINAL=gnome-terminal
elif [ "`which xfce4terminal`" != "" ]; then
TERMINAL=xfce4terminal
elif [ "`which xfce4term`" != "" ]; then
TERMINAL=xfce4term
elif [ "`which xfce4-term`" != "" ]; then
TERMINAL=xfce4-term
fi

# Make the config file if it's not there.
if [ ! -e  $HOME/.hbgui ] ; then
echo "AUDIOENCODER=copy:ac3" >> $HOME/.hbgui
echo "FORMAT=mkv" >> $HOME/.hbgui
echo "SUBTITLE=none" >> $HOME/.hbgui
echo "VIDEOENCODER=x264" >> $HOME/.hbgui
echo "IPOD=" >> $HOME/.hbgui
echo "DVDNAV=" >> $HOME/.hbgui
fi


. $HOME/.hbgui
rm /tmp/menua ; rm /tmp/lsdvdcut ; rm /tmp/HBparse ; rm /tmp/HBparse1 ; rm /tmp/HBparse-tmp 
export MAIN_DIALOG='
 <vbox>
  <text use-markup="true">
    <label>"<b>Welcome to the Light-weight HandBrakeCLI GUI!</b>"</label>
  </text>
  <text wrap="true" width-chars="40">
    <label>Do you want Handbrake to scan the DVD titles? This can take a while and may fail. Many newer DVDs do not use title one or the longest title. Use VLC to fine the number of the main title.</label>
  </text>
  <hbox>
   <button yes>
    </button>
    <button no>
    </button>
  <button cancel></button>
  </hbox>
 </vbox>
 '
I=$IFS; IFS=""
     for STATEMENTS in  $($GTKDIALOG --program MAIN_DIALOG); do
       eval $STATEMENTS
     done
     IFS=$I

if [ "$EXIT" = "Cancel" ]; then
exit
fi 
if [ "$EXIT" = "Yes" ]; then

HandBrakeCLI -i /dev/dvd -t 0 >& /tmp/HBparse

function Parse()
  {
  while read LINE
  do
      if [ "$(echo $LINE | grep duration)" = "" ] && [ ! -e /tmp/HBparse-tmp ] ;then  
      echo $LINE" " > /tmp/HBparse-tmp
	  else
	  if [ "$(echo $LINE | grep duration)" != "" ] ; then
	  cat /tmp/HBparse-tmp | tr -d '\n'  >> /tmp/HBparse1
	  echo $LINE" " >> /tmp/HBparse1
	  else
	  echo $LINE" " > /tmp/HBparse-tmp
	  fi
	  fi
  done
  }
cat /tmp/HBparse | egrep 'scanning title|duration is' | cut -d " " -f 2-10 | sed 's/scan:/  /g'  | Parse
LSDVDERR=0
else
LSDVDERR=1
fi

cat /tmp/HBparse1 | sed 's/^/<item> /g' | sed 's/$/ <\/item>/g' > /tmp/lsdvdcut

if [ "$LSDVDERR" != "1" ] ; then
if [ ! -e /tmp/HBparse1 ] ; then
export MESSAGE='
 <vbox>
  <text>
    <label>There does not seem to be a DVD in the drive!</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
exit
fi 
fi
# Find the DVD Name
if [ "$LSDVDERR" != "1" ] ; then
NAME="$(cat /tmp/HBparse | grep "DVD Title:" | cut -d " " -f 4 | sed "s/'/_/g")"
fi
#Build the GUI.
echo "export MAIN_DIALOG='
<window title=\"Handbrake DVD Ripper\">

<vbox>
  <notebook labels=\"Quick|Title Select|Options\">
<vbox>
  <text wrap=\"true\" width-chars=\"50\">
    <label>The Title Select tab lets you choose the title to rip. The default selection is the longest if you scanned for titles. The Options tab lets you choose the output folder and various other options. Make sure the output folder has plenty of free space. Some DVDs have several titles that are about the same size, in this case play the DVD with VLC. After clicking Play from the DVD menu, click on Playback and Title to find the number of the main title.</label>
  </text>
  <text use-markup=\"true\">
    <label>\"<b>Click OK to start ripping!</b>,\"</label>
  </text>
  <frame  Uncheck this box if the DVD is badly scratched.>
    <checkbox>
	<label>Used libdvdnav. Handbrake will read the DVD like a DVD player.</label>
      <default>true</default>
      <variable>DVDNAVbool</variable>
	</checkbox>
  </frame>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox> 
 </vbox>" >> /tmp/menua 

if [ "$LSDVDERR" = 1 ] ; then

echo "   <vbox>
  <text wrap=\"true\" width-chars=\"50\">
    <label>You will have to determine the title number that you want to rip. To do this play the DVD with VLC. After clicking Play from the DVD menu, click on Playback and Title in VLC to find the number of the main title. You will also need to fill in the output file name on the options tab.</label>
  </text>
  
<frame  Enter the title number to RIP.>
  <entry>
    <variable>TITLE</variable>
  </entry>
  </frame>
</vbox>" >> /tmp/menua

else
echo "  <vbox>
    <list>
	<height>200</height>
	<width>650</width>
	<variable>LIST</variable>" >> /tmp/menua
cat /tmp/lsdvdcut >> /tmp/menua
echo " </list>
      </vbox>" >> /tmp/menua
fi  

echo "	<vbox>
   <frame>
    <vbox>
  <hbox>
    <text>
      <label>Audio Encoder:</label>
    </text>
    <combobox>
      <variable>AUDIOENCODER</variable>
      <item>$AUDIOENCODER</item>
	  <item>copy:ac3</item>
      <item>copy:dts</item>
      <item>vorbis</item>
      <item>lame</item>
	  <item>faac</item>
    </combobox>
  </hbox>
  </vbox>

  </frame>

  <frame>
    <vbox>
  <hbox>
    <text>
      <label>Video Encoder:</label>
    </text>
    <combobox>
      <variable>VIDEOENCODER</variable>
      <item>$VIDEOENCODER</item>
      <item>x264</item>
      <item>ffmpeg</item>
      <item>theora</item>
    </combobox>
  </hbox>
  </vbox>

  </frame>
  <frame>
    <vbox>
  <hbox>
    <text>
      <label>Output Format:</label>
    </text>
    <combobox>
      <variable>FORMAT</variable>
	  <item>$FORMAT</item>
	  <item>mkv</item>
      <item>mp4</item>
      <item>avi</item>
      <item>ogm</item>
    </combobox>
  </hbox>
  </vbox>
 </frame>
 <frame>
    <vbox>
  <hbox>
    <text>
      <label>Subtitles:</label>
    </text>
    <combobox>
	      <variable>SUBTITLE</variable>
	  <item>$SUBTITLE</item>  
      <item>none</item>
      <item>1</item>
      <item>2</item>
	  <item>3</item>
    </combobox>
  </hbox>
  </vbox>
 </frame>
 <frame>
    <vbox>
  <hbox>
  <text use-markup=\"true\">
    <label>\"<b>The Ipod selection will over-ride the settings above.   </b>\"</label>
  </text>
    <text>
      <label>IPod-Iphone-PSP:</label>
    </text>
    <combobox>
	      <variable>IPOD</variable>
	  <item>$IPOD</item>  
      <item>no</item>
      <item>Ipod</item>
      <item>Iphone-IpodTouch</item>
	  <item>PSP</item>
    </combobox>
  </hbox>
  </vbox>
 </frame>
  <frame Choose the destination directory.>
    <hbox>
      <entry accept=\"directory\">
        <label>Select a Directory</label>
		<input>echo $FILE_DIRECTORY</input>
        <variable>FILE_DIRECTORY</variable>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <action type=\"fileselect\">FILE_DIRECTORY</action>
      </button>
    </hbox>

  </frame>
<frame Output file name>
    <entry>
    <default>$NAME</default>
    <variable>NAME</variable>
  </entry>

  </frame>

  </vbox>
  </notebook>
</vbox>
</window>
'" >> /tmp/menua

. /tmp/menua
I=$IFS; IFS=""
     for STATEMENTS in  $($GTKDIALOG --program MAIN_DIALOG); do
       eval $STATEMENTS
     done
     IFS=$I

     if [ "$EXIT" = "OK" ]; then
	 if [ "$LSDVDERR" != 1 ] ; then
       TITLE="$(echo $LIST | cut -d " " -f 3)"
     fi
     else
       echo "You pressed the Cancel button."
	   exit
     fi

# if our output file name exsists, add a number to the name.
if [ -e $FILE_DIRECTORY/$NAME.$FORMAT ] ; then
NU="1"
while [ -e $FILE_DIRECTORY/$NAME.$FORMAT ] ; do
   NAME="$NAME$NU"
   NU=$[$NU+1]
  done
fi

if [ "$DVDNAVbool" = "false" ] ; then
DVDNAV="--no-dvdnav"
else
DVDNAV=""
fi

#Debug help
echo "NAME="$NAME.$FORMAT 
echo "TITLE "$TITLE 
echo "AUDIOENCODER "$AUDIOENCODER
echo "FILE_DIRECTORY "$FILE_DIRECTORY
echo "FORMAT "$FORMAT
echo "SUBTITLE "$SUBTITLE
echo "VIDEOENCODER "$VIDEOENCODER
echo "IPOD="$IPOD
echo "DVDNAV="$DVDNAV

#Save our settings for next time.
rm $HOME/.hbgui
echo "AUDIOENCODER="$AUDIOENCODER >> $HOME/.hbgui
echo "FILE_DIRECTORY="$FILE_DIRECTORY >> $HOME/.hbgui
echo "FORMAT="$FORMAT >> $HOME/.hbgui
echo "SUBTITLE="$SUBTITLE >> $HOME/.hbgui
echo "VIDEOENCODER="$VIDEOENCODER >> $HOME/.hbgui
echo "IPOD="$IPOD >> $HOME/.hbgui
echo "DVDNAV="$DVDNAV >> $HOME/.hbgui

if [ "$TITLE" != "" ] ; then 
if [ "$FILE_DIRECTORY" != "" ] ; then
if [ "$IPOD" = "Ipod" ] ; then
$TERMINAL -e HandBrakeCLI $DVDNAV -e x264 -b 700 -a 1 --aencoder faac -B 160 -R 48 -6 dpl2 -f mp4 -I -X 320 -m -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1 -i $DVD -t $TITLE -o $FILE_DIRECTORY/$NAME.mp4
eject
export MESSAGE='
 <vbox>
  <text>
    <label>Done. Your file is "'$FILE_DIRECTORY/$NAME.mp4'"</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
exit
fi
if [ "$IPOD" = "Iphone-IpodTouch" ] ; then
$TERMINAL -e HandBrakeCLI $DVDNAV -e x264 -q 0.589999973773956 -a 1 --aencoder faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 -i $DVD -t $TITLE -o $FILE_DIRECTORY/$NAME.mp4
eject
export MESSAGE='
 <vbox>
  <text>
    <label>Done. Your file is "'$FILE_DIRECTORY/$NAME.mp4'"</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
exit
fi
if [ "$IPOD" = "PSP" ] ; then
$TERMINAL -e HandBrakeCLI $DVDNAV --maxWidth 480 --maxHeight 272 --encoder x264 --x264opts cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:subme=6 --vb 768 --aencoder faac --ab 128 --arate 48 --deinterlace -i $DVD -t $TITLE -o $FILE_DIRECTORY/$NAME.mp4
eject
export MESSAGE='
 <vbox>
  <text>
    <label>Done. Your file is "'$FILE_DIRECTORY/$NAME.mp4'"</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
exit
fi
$TERMINAL -e HandBrakeCLI --large-file $DVDNAV -s $SUBTITLE -e $VIDEOENCODER -f $FORMAT -d --aencoder $AUDIOENCODER -i $DVD -t $TITLE -o $FILE_DIRECTORY/$NAME.$FORMAT
eject
export MESSAGE='
 <vbox>
  <text>
    <label>Done. Your file is "'$FILE_DIRECTORY/$NAME.$FORMAT'"</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
else
export MESSAGE='
 <vbox>
  <text>
    <label>You must specify a folder to save in!</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
fi
else
export MESSAGE='
 <vbox>
  <text>
    <label>You must specify a title to RIP! Thats on the second tab.</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
'
$GTKDIALOG --program MESSAGE
fi