#!/bin/bash

###by plinej
###pupdvdtool-0.4 December 2006
###This is the audio & m2v gui

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

if ! [ -d $HOME/.pupdvdtool ]; then
mkdir $HOME/.pupdvdtool
fi
if ! [ -f $HOME/.pupdvdtool/audioguidir ]; then
echo > $HOME/.pupdvdtool/audioguidir
fi
#if [ -f "`cat $HOME/.pupdvdtool/audioguidir`" ]; then
#THISFILE=`cat $HOME/.pupdvdtool/audioguidir`
#echo `dirname "$THISFILE"` > $HOME/.pupdvdtool/audioguidir
#fi
if ! [ -d "`cat $HOME/.pupdvdtool/audioguidir`" ]; then
echo > $HOME/.pupdvdtool/audioguidir
fi

rm -f /tmp/pupdvdtool-file.txt
rm -f /tmp/pupdvdtool-word.txt
rm -f /tmp/pupdvdtool-cbox.txt
rm -f /tmp/pupdvdtool-audio.txt
rm -f /tmp/audioexec
rm -f /tmp/pupdvdtool-ffmpeg.txt
rm -f /tmp/pupdvdtool-ffmpeg2.txt
rm -f /tmp/pupdvdtool-exit.txt

echo pupdvdtoolのこの部分はffmpegを使ってwav、ac3、mp3 > /tmp/pupdvdtool-audio.txt
echo と mp2間のオーディオフォーマットの変換をします。 >> /tmp/pupdvdtool-audio.txt
echo もしこれらのオーディオフォーマットの一つがあれば >> /tmp/pupdvdtool-audio.txt
echo ビデオファイルから音声を取り出します。 >> /tmp/pupdvdtool-audio.txt
echo 最後にする事はビデオファイルからm2vを >> /tmp/pupdvdtool-audio.txt
echo 取り出す事です。 >> /tmp/pupdvdtool-audio.txt
echo >> /tmp/pupdvdtool-audio.txt
echo 作業用のディレクトリか一つのファイル >> /tmp/pupdvdtool-audio.txt
echo を選択できます。もしディレクトリを選択すると >> /tmp/pupdvdtool-audio.txt
echo 作業したいファイルタイプ、例えば「mp3」を >> /tmp/pupdvdtool-audio.txt
echo 拡張子名ボックスに記入する必要があります。 >> /tmp/pupdvdtool-audio.txt

cd /

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

    <frame どちらか一方を選択して下さい>
	<frame ファイルのディレクトリを選択し、拡張子を入力>
	<hbox>
      <entry accept=\"directory\">
        <label>ディレクトリを選択</label>
        <variable>DIRENTRY</variable>
			<input>cat \$HOME/.pupdvdtool/audioguidir</input>
      <action>echo \$DIRENTRY | tee /tmp/pupdvdtool-file.txt</action>
      <action>echo \$DIRENTRY | tee \$HOME/.pupdvdtool/audioguidir</action>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <variable>FILE_BROWSE_DIRECTORY</variable>
        <action type=\"fileselect\">DIRENTRY</action>
      </button>
  </hbox>
    <hbox>
      <text><label>ファイル拡張子を入力:</label></text>
      <entry>
        <variable>WORD</variable>
      <action>echo \$WORD | tee /tmp/pupdvdtool-word.txt</action>
      </entry>
    </hbox>
  </frame>
	<frame または、シングルファイルを選択>
    <hbox>
      <entry accept=\"filename\">
        <label>存在するファイルを選択</label>
        <variable>ENTRY1</variable>
			<action>echo \$ENTRY1 | tee /tmp/pupdvdtool-file.txt</action>
      <action>echo \$ENTRY1 | tee \$HOME/.pupdvdtool/audioguidir</action>
      </entry>
      <button>
        <input file stock=\"gtk-file\"></input>
        <variable>FILE_BROWSE_FILENAME</variable>
        <action type=\"fileselect\">ENTRY1</action>
      </button>
    </hbox>
  </frame>
  </frame>
    
    <frame 新しいファイルを同じディレクトリに入力ファイルとして保存しますか?>
    <hbox>
    <checkbox>
      <label>チェックでyes</label>
      <variable>CHECKBOX</variable>
      <default>true</default>
    </checkbox>
    </hbox>
    </frame>
    
    <hbox>
      <text><label>選択したオーディオを変換</label></text>
      <button>
       <input file stock=\"gtk-convert\"></input>
        <label>変換</label>
      <action>echo \$CHECKBOX | tee /tmp/pupdvdtool-cbox.txt</action>
      <action>touch /tmp/audioconvert.txt</action>
      <action type=\"exit\">Exit by button</action>
      </button>
    </hbox>
    
    <hbox>
      <text><label>選択したビデオからオーディオを抜き出す</label></text>
      <button>
       <input file stock=\"gtk-undo\"></input>
        <label>抜き出し</label>
      <action>echo \$CHECKBOX | tee /tmp/pupdvdtool-cbox.txt</action>
      <action>touch /tmp/audioextract.txt</action>
      <action type=\"exit\">Exit by button</action>
      </button>
    </hbox>
    
    <hbox>
      <text><label>選択したビデオからm2vを抜き出す</label></text>
      <button>
       <input file stock=\"gtk-undo\"></input>
        <label>抜き出し</label>
      <action>echo \$CHECKBOX | tee /tmp/pupdvdtool-cbox.txt</action>
      <action>touch /tmp/m2vextract.txt</action>
      <action>EXIT=EXIT</action>
      </button>
    </hbox>

<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/pupdvdtool-audio.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>touch /tmp/pupdvdtool-exit.txt</action>
      <action type=\"exit\">Exit by button</action>
    </button>

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

  </hbox>

</vbox>
</hbox>
</window>
"

gtkdialog3 --program=AUDIOGUI --center

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

CHECKBOX=`cat /tmp/pupdvdtool-cbox.txt`
rm -f /tmp/pupdvdtool-cbox.txt

if ! [ -f /tmp/pupdvdtool-file.txt ]; then
pupdvdtool
exit 0
fi

TMPFILE=`cat /tmp/pupdvdtool-file.txt`
rm -f /tmp/pupdvdtool-file.txt

if [ "$CHECKBOX" = false ]; then
if ! [ -f $HOME/.pupdvdtool/audiodir ]; then
echo / > $HOME/.pupdvdtool/audiodir
fi
AUDIODIR=`cat $HOME/.pupdvdtool/audiodir`
if ! [ -d "$AUDIODIR" ]; then
AUDIODIR="/"
fi
AUDIODIR=`cat $HOME/.pupdvdtool/audiodir`
DIR2=`Xdialog --title "Select your save directory" --dselect "$AUDIODIR" 0 0 2>&1`
echo "$DIR2" > /root/.pupdvdtool/audiodir
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
fi

######audio conversion part

if [ -f /tmp/audioconvert.txt ]; then
rm -f /tmp/audioconvert.txt

choice1=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select what format you want your new audio to be." 400x200 10 "mp3" "" ON "mp2" "select this or ac3 for dvd audio" off "ac3" "select this or mp2 for dvd audio" off "wav" "" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choice7=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Do you want your "$choice1" file/s to be stereo or mono?" 350x150 10 "stereo" "" ON "mono" "" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
if [ "$choice7" = stereo ]; then
choicestereo="-ac 2"
else
choicestereo="-ac 1"
fi
if [ "$choice1" != ac3 ]; then
if [ "$choice1" != mp2 ]; then
choice2=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your sampling rate in Hz." 320x250 10 "8000" "" off "22050" "" off "32000" "" off "44100" "select this for cd burning" ON "48000" "select this for dvd audio" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choicear=`echo -ar "$choice2"`
fi
fi
if [ "$choice1" = ac3 ]; then
choicear=`echo -ar 48000`
fi
if [ "$choice1" = mp2 ]; then
choice3=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your sampling rate in Hz." 320x200 10 "22050" "" off "32000" "" off "44100" "select this for cd burning" ON "48000" "select this for dvd audio" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choicear=`echo -ar "$choice3"`
fi
if [ "$choice1" = mp3 ]; then
choice4=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your desired bitrate." 250x350 10 "32" "" off "64" "" off "96" "" off "128" "" off "160" "" off "192" "" ON "224" "" off "256" "" off "320" "" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choiceab=`echo -ab "$choice4"`
fi
if [ "$choice1" = mp2 ]; then
choice5=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your desired bitrate." 250x250 10 "32" "" off "64" "" off "96" "" off "128" "" off "160" "" ON 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choiceab=`echo -ab "$choice5"`
fi
if [ "$choice1" = ac3 ]; then
choice6=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your desired bitrate." 250x320 10 "128" "" off "160" "" off "192" "" ON "224" "" off "256" "" off "320" "" off "384" "" off "448" "" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choiceab=`echo -ab "$choice6"`
fi

if [ -f $TMPFILE ]; then
FILE="$TMPFILE"
if [ "$CHECKBOX" = true ]; then
DIR2=`dirname "$FILE"`/
fi

TMPDIR=`dirname "$FILE"`/
cd "$TMPDIR"
FILEB=`basename $FILE`
EXT=${FILE##*.}
new_file=`echo "$FILEB" | sed "s/ /_/g"`
if [ "$FILEB" != "$new_file" ]; then
mv "$FILEB" "$new_file"
FILEB="$new_file"
fi
FILE="$TMPDIR""$FILEB"
FILE2=`echo $FILEB | sed -e "s/$EXT/$choice1/g"`
echo \#\!\/bin\/sh > /tmp/audioexec
echo >> /tmp/audioexec
echo rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$FILE" "$choicear" "$choicestereo" "$choiceab" "$DIR2""$FILE2" >> /tmp/audioexec
chmod a+x /tmp/audioexec
/tmp/audioexec
rm -f /tmp/audioexec
fi

if [ -d $TMPFILE ]; then
DIR="$TMPFILE"/
if [ "$CHECKBOX" = true ]; then
DIR2="$DIR"
fi
WORD=`cat /tmp/pupdvdtool-word.txt`
rm -f /tmp/pupdvdtool-word.txt
if [ "`echo $WORD | head -c 1`" != \. ]; then
WORD=."$WORD"
fi
EXT="`echo $WORD | sed -e 's/\.//g'`"
cd "$DIR"
ls *"$WORD" | while read FILE
do
new_file=`echo $FILE | sed -e "s/ /_/g"`
if [ "$FILE" != "$new_file" ]; then
mv "$FILE" "$new_file"
FILE="$new_file"
fi
FILE2=`echo $FILE | sed -e "s/$EXT/$choice1/g"`
echo \#\!\/bin\/sh > /tmp/audioexec
echo >> /tmp/audioexec
echo rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$DIR""$FILE" "$choicear" "$choicestereo" "$choiceab" "$DIR2""$FILE2" >> /tmp/audioexec
chmod a+x /tmp/audioexec
/tmp/audioexec
rm -f /tmp/audioexec
done
fi

fi

#####audio extraction part

if [ -f /tmp/audioextract.txt ]; then
rm -f /tmp/audioextract.txt

if [ -f $TMPFILE ]; then
FILE="$TMPFILE"
if [ "$CHECKBOX" = true ]; then
DIR=`dirname "$FILE"`/
else
DIR="$DIR2"
fi

TMPDIR=`dirname "$FILE"`/
cd "$TMPDIR"
FILEB=`basename $FILE`
EXT=${FILEB##*.}
new_file=`echo "$FILEB" | sed "s/ /_/g"`
if [ "$FILEB" != "$new_file" ]; then
mv "$FILEB" "$new_file"
FILEB="$new_file"
fi
FILE="$TMPDIR""$FILEB"

ffmpeg -i "$FILE" 2>/tmp/pupdvdtool-ffmpeg.txt
grep Audio /tmp/pupdvdtool-ffmpeg.txt > /tmp/pupdvdtool-ffmpeg2.txt
if [ "`grep 'Audio: mp2' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILEB | sed -e "s/$EXT/mp2/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR""$FILE2"
fi
if [ "`grep 'Audio: mp3' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILEB | sed -e "s/$EXT/mp3/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR""$FILE2"
fi
if [ "`grep 'Audio: ac3' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILEB | sed -e "s/$EXT/ac3/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR""$FILE2"
fi
if [ "`grep pcm /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILEB | sed -e "s/$EXT/wav/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$FILE" "$DIR""$FILE2"
fi
if [ "`grep mp2 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep mp3 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep ac3 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep pcm /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
Xdialog --title "MESSAGE BOX" --msgbox "ffmpeg can't detect any of the following
        types of audio in 
	    "$FILE":
        ac3, mp2, mp3, or pcm
	    and is exiting now" 0 0
fi
fi
fi
fi
fi
fi

if [ -d $TMPFILE ]; then
DIR="$TMPFILE"/
if [ "$CHECKBOX" = true ]; then
DIR2="$DIR"
fi
WORD=`cat /tmp/pupdvdtool-word.txt`
rm -f /tmp/pupdvdtool-word.txt
if [ "`echo $WORD | head -c 1`" != \. ]; then
WORD=."$WORD"
fi
EXT="`echo $WORD | sed -e 's/\.//g'`"
cd "$DIR"
ls *"$WORD" | while read FILE
do
new_file=`echo $FILE | sed -e "s/ /_/g"`
if [ "$FILE" != "$new_file" ]; then
mv "$FILE" "$new_file"
FILE="$new_file"
fi

ffmpeg -i "$FILE" 2>/tmp/pupdvdtool-ffmpeg.txt
grep Audio /tmp/pupdvdtool-ffmpeg.txt > /tmp/pupdvdtool-ffmpeg2.txt
if [ "`grep 'Audio: mp2' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILE | sed -e "s/$EXT/mp2/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR2""$FILE2"
fi
if [ "`grep 'Audio: mp3' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILE | sed -e "s/$EXT/mp3/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR2""$FILE2"
fi
if [ "`grep 'Audio: ac3' /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILE | sed -e "s/$EXT/ac3/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -y -i "$FILE" -ac 2 -acodec copy "$DIR2""$FILE2"
fi
if [ "`grep pcm /tmp/pupdvdtool-ffmpeg2.txt`" != "" ]; then
FILE2=`echo $FILE | sed -e "s/$EXT/wav/g"`
rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$FILE" "$DIR2""$FILE2"
fi
if [ "`grep mp2 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep mp3 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep ac3 /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
if [ "`grep pcm /tmp/pupdvdtool-ffmpeg2.txt`" = "" ]; then
Xdialog --title "MESSAGE BOX" --msgbox "ffmpeg can't detect any of the following
        types of audio in 
	    "$FILE":
        ac3, mp2, mp3, or pcm
	    and is exiting now" 0 0
fi
fi
fi
fi
done
fi

rm -f tmp/pupdvdtool-ffmpeg.txt
rm -f tmp/pupdvdtool-ffmpeg2.txt

######m2v extraction part

if [ -f /tmp/m2vextract.txt ]; then
rm -f /tmp/m2vextract.txt

choice1=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select ntsc or pal." 200x150 10 "ntsc" "" ON "pal" "" off 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choice2=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your desired quality." 250x180 10 "vcd" "" off "svcd" "" off "dvd" "" ON 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
choice3=`Xdialog --backtitle "" --title "RADIOLIST BOX" --radiolist "Select your desired ratio." 200x150 10 "4:3" "" off "16:9" "" ON  2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi

if [ -f $TMPFILE ]; then
FILE="$TMPFILE"
if [ "$CHECKBOX" = true ]; then
DIR2=`dirname "$FILE"`/
fi

TMPDIR=`dirname "$FILE"`/
cd "$TMPDIR"
FILEB=`basename $FILE`
EXT=${FILE##*.}
new_file=`echo "$FILEB" | sed "s/ /_/g"`
if [ "$FILEB" != "$new_file" ]; then
mv "$FILEB" "$new_file"
FILEB="$new_file"
fi
FILE="$TMPDIR""$FILEB"
FILE2=`echo $FILEB | sed -e "s/$EXT/m2v/g"`

rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$FILE" -target "$choice1"-"$choice2" -aspect "$choice3" -an -y "$DIR2""$FILE2"
fi

if [ -d $TMPFILE ]; then
DIR="$TMPFILE"/
if [ "$CHECKBOX" = true ]; then
DIR2="$DIR"
fi
WORD=`cat /tmp/pupdvdtool-word.txt`
rm -f /tmp/pupdvdtool-word.txt
if [ "`echo $WORD | head -c 1`" != \. ]; then
WORD=."$WORD"
fi
EXT="`echo $WORD | sed -e 's/\.//g'`"
cd "$DIR"
ls *"$WORD" | while read FILE
do
new_file=`echo $FILE | sed -e "s/ /_/g"`
if [ "$FILE" != "$new_file" ]; then
mv "$FILE" "$new_file"
FILE="$new_file"
fi

FILEB=`basename $FILE`
EXT=${FILE##*.}
FILE="$TMPDIR""$FILEB"
FILE2=`echo $FILEB | sed -e "s/$EXT/m2v/g"`

rxvt -bg orange --geometry 60x10 -e ffmpeg -i "$FILE" -target "$choice1"-"$choice2" -aspect "$choice3" -an -y "$DIR2""$FILE2"
done
fi
fi

#####end message

Xdialog --title "MESSAGE BOX" --msgbox "全プロセスはすぐに終了するはずです." 0 0
pupdvdtool
exit 0

