if [ $# -lt 3 ]; then 
 echo -e " Enter at least the path, a title number and an audio id! \n (You can enter a subtitle id and a custom filename as well)"
 exit 1
fi

#if [ $# -lt 5 -a "$4" =~ "[0-9]" ]
if [ -n "$5" ]; then # -n tests to see if the argument is non empty
 NAME=$5
else
 NAME=${PWD##*/}
fi

SUBS1='-vobsubout '
SUBS2=' -vobsuboutindex 0 -sid'
SUBSNAME=${NAME}_$4

if [ $# -lt 4 ]; then
 SUBS1=''
 SUBS2=''
 SUBSNAME=''
fi

#if [ "$4" =~ "[0-9]"]; then echo "blah"
#else
# SUBS1=''
# SUBS2=''
# SUBSNAME=''
#fi

#if [ $# -lt 5 -a "$4" =~ "[0-9]" ]

mv divx2pass.log divx2pass.log.old
mv divx2pass.first divx2pass.first.old
mv divx2pass.mbtree divx2pass.mbtree.old
mv divx2pass.mbtree.first divx2pass.mbtree.first.old
rm divx2pass.temp
rm divx2pass.mbtree.temp
mv ${NAME}.avi ${NAME}.avi.old

mencoder dvd://$2 -dvd-device $1 -v -vf pp=ci -ovc x264 -x264encopts bitrate=1400:subq=7:frameref=8:me=umh:bframes=6:b_adapt:b_pyramid=normal:weight_b:partitions=all:qcomp=0.8:keyint=500:trellis=1:threads=auto:turbo=1:direct-pred=auto:8x8dct:pass=1 -o /dev/null -oac copy $SUBS1 $SUBSNAME $SUBS2 $4
cp divx2pass.log divx2pass.log.first 
cp divx2pass.log.mbtree divx2pass.mbtree.first
mencoder dvd://$2 -dvd-device $1 -vf pp=ci -ovc x264 -x264encopts bitrate=1400:subq=7:frameref=8:me=umh:bframes=6:b_adapt:b_pyramid=normal:weight_b:partitions=all:qcomp=0.8:keyint=500:trellis=1:threads=auto:turbo=1:direct-pred=auto:8x8dct:pass=3 -o $NAME.avi -oac mp3lame -lameopts cbr:br=192:aq=0 -aid $3 

# for slow pcs: -x264encopts bitrate=1600:keyint=600:frameref=6:bframes=4:b_adapt:b_pyramid=normal:weight_b:subq=5
