#!/bin/bash

VERSION=2.6.1
DATE="May 04, 2007"
AUTHOR="Rick Saunders (ozzzy1@gmail.com)"

#################################################################
# DVDShrink                                                     #
#                                                               #
# DVDShrink is released under the Gnu Public License            #
# (http://www.gnu.org/copyleft/gpl.html)                        #
#                                                               #
#    See the file 'gpl.txt' included as part of this package    #
#                                                               #
# Overview, features and elucidation                            #
#                                                               #
#    See the file 'README.txt' that is part of this package.    #
#                                                               #
# Send bug reports to the address above.                        #
#                                                               #
#################################################################

PIDFILE=~/.dvdshrink.pid
echo $$ > $PIDFILE   

PROGRAM="DVDSHRINK"

DIRNAME=${0%/*} && FUNCFILE=$DIRNAME/dvdsfunctions
echo $DIRNAME

if [ -e $FUNCFILE -a -r $FUNCFILE ]; then
   . $FUNCFILE
else
   echo "$FUNCFILE does not exist or is not readable! Make sure the file"
   echo "is in the same directory as the script with permissions of at"
   echo "least 666 and rerun the script!"
   echo -en "\nHit any key to exit!     "
   read
   doexit 0
fi

banner               # Place the banner on the console
setdefaultvars       # Set the default values for variables, files, and flags
checktools           # Check for the needed tools
readconfig           # Read the configuration file

NOBUILD=0            # Determine if some functions need to be run

while [ "$1" != "" ]; do
  case $1 in
     -a | --audio)               shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "audio stream" "-a"
                                 else 
                                    AUDIO=$1
                                 fi;;
                                 
     -A | --authoronly)          ((NOBUILD++))
                                 AUTHORONLY=1;;

     -b | --burn)                if [ `testistring "$@" '-B'` -gt 0 -o `testistring "$@" '--noburn'` -gt 0 ]; then
                                    AUTOBURN=0
                                 else
                                    AUTOBURN=1
                                 fi;;

     -B | --noburn)              AUTOBURN=0;;

     -c | --configure)           sconfigure;;
     
     -d | --deletefiles)         if [ `testistring "$@" '-D'` -gt 0 -o `testistring "$@" '--nodeletefiles'` -gt 0 ]; then
                                    RMFILES=0
                                 else
                                    RMFILES=1
                                 fi;;

     -e | --episodes)            shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "episodes per DVD" "-e"
                                 else 
                                    MAXMPGSIZE=$(($MAXMPGSIZE/$1))
                                 fi;;
                                 
     -D | --nodeletefiles)       RMFILES=0;;

     -F | --forceburn)           if [ `testistring "$@" '-B'` -gt 0 -o `testistring "$@" '--noburn'` -gt 0 ]; then
                                    FORCEBURN=0
                                 else
                                    FORCEBURN=1
                                 fi;;

     -i | --isoonly)             ((NOBUILD++))
                                 ISOONLY=1;;
     
     -I | --info)                DVDINFO=1
                                 DISKPROMPT=0;;
     
     -k | --shrinkfactor)        shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "shrink factor" "-k"
                                 else 
                                    MYSHRINK=`Shrink2Integer $1`
                                    SHRINK=1
                                    if [ $MYSHRINK -gt 200 ]; then
                                       echo -e "ERROR: Shrink factor > 2.0\n"
                                       usage
                                       doexit 0
                                    elif [ $MYSHRINK -lt 101 ]; then
                                       MYSHRINK=0
                                       SHRINK=0
                                    fi
                                 fi;;
                                 
     -K | --noshrink)            if [ $MYSHRINK -gt 100 ]; then
                                    SHRINK=1
                                 else
                                    SHRINK=0
                                 fi;;
     
     -l | --deletelogs)          DELETELOGS=1;;
     
     -L | --listall)             TITLE="list"
                                 AUDIO="list"
                                 SUBTITLE="list";;
     
     -m | --mpegonly)            ((NOBUILD++))
                                 MPEGONLY=1;;

     -o | --isodir )             shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "ISO directory" "-o"
                                 else 
                                    ISODIR=$1
                                 fi;;

     -O | --saveiso )            SAVEISO=1;;
                                 
     -M | --bigmpegonly)         BIGMPEGONLY=1
                                 ((NOBUILD++))
                                 SHRINK=0;;
                                 
     -n | --nodiskprompt)        DISKPROMPT=0;;                            
     
     -p | --project)             shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "project name" "-p"
                                 else 
                                    SELPROJ=$1
                                 fi;;
                                 
     -r | --read_device)         shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "read device" "-r"
                                 else 
                                    RDEVICE=$1
                                 fi;;
                                 
     -s | --streamonly)          ((NOBUILD++))
                                 STREAMONLY=1;;
                                 
     -S | --bigstreamonly)       BIGSTREAMONLY=1
                                 ((NOBUILD++))
                                 SHRINK=0;;
                                 
     -t | --title)               shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "title" "-t"
                                 else 
                                    TITLE=$1
                                 fi;;
                                 
     -T | --showtitles)          SHOWTITLES=1
                                 DISKPROMPT=0;;
                                 
     -u | --subtitle)            shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "subtitle" "-u"
                                 else
                                    SUBTITLE=$1
                                    ADDTITLES=1
                                 fi;;
                                 
     -v | --version)             doexit 0;;
     
     -V | --showvalues)          SELSHOW=1;;
     
     -w | --write_device )       shift
                                 testarg $1
                                 if (($BADARG)); then 
                                    argerror "write device" "-w"
                                 else 
                                    WDEVICE=$1
                                 fi;;
                                
     -h | --help)                usage
                                 doexit 0;;
     
     --restart)                  RESTART=1;;
     
     --jumpin)                   RESTART=1
                                 JUMPIN=1;;
     
     --long-help)                usage_long | more
                                 doexit 0;;
                                 
     * )                         echo
                                 echo "ERROR! Invalid option $1"
                                 echo
                                 usage
                                 doexit 0;;
  esac
  shift
done

# If we're not restarting then we have a few things to do
#------------------------------------------------------------
if (( ! $RESTART && ! $JUMPIN )); then
   POINT=0
   if (($DISKPROMPT)); then diskprompt;  fi
   Test4DVD
   getprojname
   if ((`teststring $TITLE 'list'` && ! $SHOWTITLES )); then
      selecttitle
   elif (($SHOWTITLES)); then 
      banner
      echo
      echo -e "   \033[1mVolume ID:\033[0m               "$PROJNAME      
      echo
      showtitles
      pauseprompt
      echo
      rm -rf $WORKDIR
      doexit 0
   fi
   setfiles    # Create the files we need
   tcprobe -i $RDEVICE -T $TITLE -H 200 > $INFOFILE 2>&1
   DINFO=`cat $INFOFILE`
   if ((`teststring $AUDIO 'list'`)); then selectaudio; else getaudiolang $AUDIO; fi
   if ((`teststring $SUBTITLE 'list'`)); then selectsubtitle; fi

   # Check to see if there are indeed ANY subtitles

   OLDIFS=$IFS
   IFS=$'\n'
   UCOUNT=0
   for a in $DINFO; do
      if ((`teststring "$a" "subtitle"`)); then ((UCOUNT++)); fi
   done
   IFS=$OLDIFS
   if ! (( $UCOUNT )); then ADDTITLES=0; fi

   if (($ADDTITLES)); then getsubtitlelang $SUBTITLE; fi
   OLDIFS=$IFS
   IFS=$'\n'
   NTSC=0
   for a in `cat $INFOFILE`; do
      if ((`teststring "$a" " 720x480 "`)); then NTSC=1; fi
   done
   IFS=$OLDIFS
   if (($NTSC)); then TVRES='720,480'; else TVRES='720,576'; fi
   if (($DVDINFO)); then getprojname; dvdinfo; fi
   banner
   STIME=`date +%s`
   if (($SELSHOW)); then showvalues; else showsynopsis; fi
   readchapters
   extractvideo
   requantize
   multiplexvideo
   createsubtitles
   authordvd
   burndvd
else
   VARFILE=$BASEDIR/dvdshrink.var
   if [ -f $VARFILE ]; then
      . $VARFILE
   else
      echo -e "   \033[1mVariable holding file $VARFILE does not exist! Exiting!\033[0m\n"
      doexit 0
   fi
   if [ -f $CMDFILE ]; then 
      if (( $RESTART || $JUMPIN )); then
         POINT=`cat $CMDFILE`
      fi
   else
      echo -e "   \033[1mScript progress file $CMDFILE does not exist! Exiting!\033[0m"
      read
      echo
      doexit 0
   fi
   if (($JUMPIN)); then
      if [ $POINT -lt 7 ]; then 
         POINT=$(($POINT + 1))
      else
         POINT=7
      fi
   fi
   runcommands $POINT
fi

