#!/bin/bash

###by plinej
###pupdvdtool-0.4 December 2006
###This is the dvd rip gui

if [ "`which vobcopy`" = "" ]; then
Xdialog --title "MESSAGE BOX" --msgbox "vobcopyがパスにありません。
このプログラムは終了します" 0 0
exit 0
fi

rm -f /tmp/pupdvdtool-word2.txt
rm -f /tmp/pupdvdtool-word.txt
rm -f /tmp/vobcopy.txt
rm -f /tmp/vobcopylog.txt

echo 特定のタイトルを選択しない、あるいはミラーコピーを > /tmp/vobcopy.txt
echo 選択しない場合は、vobcopy はDVDのメインタイトルを >> /tmp/vobcopy.txt
echo 取り出すだけです。手動でタイトルを選ぶ必要がある場合、>> /tmp/vobcopy.txt
echo vobcopy は時々、メインタイトルが本当にそのケースでは>> /tmp/vobcopy.txt
echo ないかと考えます。>> /tmp/vobcopy.txt
echo >> /tmp/vobcopy.txt
echo ミラーリングコピーはDVDの正確な解読されたコピーを作り、 >> /tmp/vobcopy.txt
echo メニューには手をつけません。 >> /tmp/vobcopy.txt
echo DVDを圧縮する必要のない時だけ選択して下さい。 >> /tmp/vobcopy.txt
echo >> /tmp/vobcopy.txt
echo DVDをマウントする必要はありません。選択された >> /tmp/vobcopy.txt
echo ドライブにディスクを挿入し、上のリッピングボタンを >> /tmp/vobcopy.txt
echo クリックするだけです。 >> /tmp/vobcopy.txt
echo >> /tmp/vobcopy.txt
echo リッピングボタンをクリックしたあと、 >> /tmp/vobcopy.txt
echo 保存するディレクトリを促されます。 >> /tmp/vobcopy.txt

PROBEDRIVES=""
for ONEDRIVE in `probedisk | grep -i dvd | cut -f 1 -d '|'`
do
 PROBEDRIVES="$PROBEDRIVES<item>$ONEDRIVE</item>"
done

export RIPGUI="
<window title=\"pupdvdtool rip gui \"icon-name=\"gtk-cdrom\">
<hbox>
<vbox>

    <hbox>
    <text><label>DVDドライブを選択して下さい</label></text>
    <combobox>
     <variable>PROBECOMBO</variable>
      $PROBEDRIVES
    </combobox>
    </hbox>
    
    <frame ミラーコピーをする場合は不要です>
    <hbox>
      <text><label>ファイルに名前を入力して下さい:</label></text>
      <entry>
        <variable>WORD</variable>
      <action>echo \$WORD | tee /tmp/pupdvdtool-word.txt</action>
      </entry>
    </hbox>
    </frame>
    
    <frame 特定のタイトルをリッピングしたい時だけ記入して下さい>
    <hbox>
      <text><label>リッピングするタイトルナンバー:</label></text>
      <entry>
        <variable>WORD2</variable>
      <action>echo \$WORD2 | tee /tmp/pupdvdtool-word2.txt</action>
      </entry>
    </hbox>
    </frame>
    
    <frame リッピングしたファイルを自動的に圧縮しますか?>
    <hbox>
    <checkbox>
    <label>チェックでyes</label>
    <variable>CHECKBOX2</variable>
    <default>false</default>
    </checkbox>
    </hbox>
    </frame>
    
    <hbox>
    <checkbox>
      <label>チェックでミラーコピー</label>
      <variable>CHECKBOX</variable>
      <default>false</default>
    </checkbox>
    
      <button>
      <input file stock=\"gtk-dialog-info\"></input>
      <label>DVD-情報</label>
        <action>touch /tmp/vobcopy-info.txt</action>
      <action>echo \$PROBECOMBO | tee /tmp/pupdvdtool-probecombo.txt</action>
	<action type=\"exit\">Exit by button</action>
      </button>
    
      <button>
      <input file stock=\"gtk-cdrom\"></input>
      <label>リッピング</label>
        <action>touch /tmp/pupdvdtool-rip.txt</action>
      <action>echo \$CHECKBOX | tee /tmp/pupdvdtool-checkbox.txt</action>
      <action>echo \$CHECKBOX2 | tee /tmp/pupdvdtool-checkbox2.txt</action>
      <action>echo \$PROBECOMBO | tee /tmp/pupdvdtool-probecombo.txt</action>
        <action type=\"exit\">Exit by button</action>
      </button>
    </hbox>

<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/vobcopy.txt</input> 
      <width>120</width>
      <height>160</height>
    </edit>
</frame>

  <hbox>   

    <button>
       <input file stock=\"gtk-cdrom\"></input>
      <label>pupdvdtool</label>
      <action>pupdvdtool &</action>
      <action type=\"exit\">Exit by button</action>
    </button>

    <button>
      <input file stock=\"gtk-quit\"></input>
      <label>終了</label>
      <action>touch /tmp/exitripguinow</action>
      <action type=\"exit\">Exit by button</action>
    </button>

  </hbox>

</vbox>
</hbox>
</window>
"
gtkdialog3 --program=RIPGUI --center

if [ -f /tmp/exitripguinow ]; then
rm -f /tmp/exitripguinow
EXIT=""
exit 0
fi

CHECKBOX=`cat /tmp/pupdvdtool-checkbox.txt`
rm -f /tmp/pupdvdtool-checkbox.txt
CHECKBOX2=`cat /tmp/pupdvdtool-checkbox2.txt`
rm -f /tmp/pupdvdtool-checkbox2.txt

if [ "$CHECKBOX2" = true ]; then
SHRINK2SIZE=`Xdialog --title "INPUT BOX" --inputbox "Enter your desired shrink size.\n\
4775 works good for me." 0 0 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
echo "$SHRINK2SIZE" > /tmp/pupdvdtool-shrink.txt
fi

DEV=`cat /tmp/pupdvdtool-probecombo.txt`
MNT=`cat /tmp/pupdvdtool-probecombo.txt | sed -e 's/dev/mnt/g'`
rm -f /tmp/pupdvdtool-probecombo.txt

if [ -f /tmp/vobcopy-info.txt ]; then
vobcopy -i $DEV -I 2> /tmp/vobcopy-info.txt
defaulttexteditor /tmp/vobcopy-info.txt &
./ripgui &
sleep 1
rm -f /tmp/vobcopy-info.txt
exit 0
fi

if ! [ -d $MNT ]; then
mkdir $MNT
fi
mount $DEV $MNT

if [ -f /tmp/pupdvdtool-rip.txt ]; then
rm -f /tmp/pupdvdtool-rip.txt
if ! [ -d $HOME/.pupdvdtool ]; then
mkdir $HOME/.pupdvdtool
fi
if ! [ -f $HOME/.pupdvdtool/ripdir ]; then
echo / > $HOME/.pupdvdtool/ripdir
fi
RIPDIR=`cat $HOME/.pupdvdtool/ripdir`
if ! [ -d "$RIPDIR" ]; then
RIPDIR="/"
fi
DIR=`Xdialog --title "Select your save directory" --dselect "$RIPDIR" 0 0 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
echo "$DIR" > $HOME/.pupdvdtool/ripdir
if [ -f /tmp/pupdvdtool-word2.txt ]; then
rxvt -bg orange --geometry 79x10 -e vobcopy -i $MNT -o $DIR -n `cat /tmp/pupdvdtool-word2.txt` -t `cat /tmp/pupdvdtool-word.txt` -l 2>&1 | tee /tmp/vobcopylog.txt
fi
if ! [ -f /tmp/pupdvdtool-word2.txt ]; then
if [ "$CHECKBOX" = true ]; then
rxvt -bg orange --geometry 79x10 -e vobcopy -i $MNT -m -o $DIR -l 2>&1 | tee /tmp/vobcopylog.txt
fi
fi
if ! [ -f /tmp/pupdvdtool-word2.txt ]; then
if [ "$CHECKBOX" = false ]; then
rxvt -bg orange --geometry 79x10 -e vobcopy -i $MNT -o $DIR -t `cat /tmp/pupdvdtool-word.txt` -l 2>&1 | tee /tmp/vobcopylog.txt
fi
fi

umount $MNT

Xdialog --title "MESSAGE BOX" --msgbox "vobcopyは完了しました、
リッピングされたファイルができ上がりました。
次に、vobcopy エラーログの結果を示すテキスト
ファイルが開きます(もしあれば)" 0 0 &

echo >> /tmp/vobcopylog.txt
echo This is the vobcopy error log >> /tmp/vobcopylog.txt
echo If it is blank there were no errors >> /tmp/vobcopylog.txt
defaulttexteditor /tmp/vobcopylog.txt &

if [ "$CHECKBOX2" = true ]; then
OLDDIR=`pwd`
cd "$DIR"
TEMPFILE=`cat /tmp/pupdvdtool-word.txt`
FILE=`ls "$TEMPFILE"*.vob`
echo "$DIR" > $HOME/.pupdvdtool/shrinkdir
echo "$DIR""$FILE" > /tmp/pupdvdtool-file.txt
rm -f /tmp/pupdvdtool-word2.txt
rm -f /tmp/pupdvdtool-word.txt
rm -f /tmp/vobcopy.txt
/usr/local/apps/pupdvdtool/shrinkgui -automatic &
exit 0
fi
pupdvdtool

fi

rm -f /tmp/pupdvdtool-word2.txt
rm -f /tmp/pupdvdtool-word.txt
rm -f /tmp/vobcopy.txt

exit 0

