#!/bin/sh

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

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

rm -f /tmp/pupdvdtool-exit.txt
rm -f /tmp/pupdvdtool-file.txt
rm -f /tmp/pupdvdtool-file2.txt
rm -f /tmp/pupdvdtool-dir.txt
rm -f /tmp/pupdvdtool-mplex.txt

echo pupdvdtoolのこの部分は、m2vと選択された > /tmp/pupdvdtool-mplex.txt
echo オーディオファイルをシングルmpgに多重化します。 >> /tmp/pupdvdtool-mplex.txt
echo >> /tmp/pupdvdtool-mplex.txt
echo もし出力ファイルをm2vファイルと同じ >> /tmp/pupdvdtool-mplex.txt
echo ディレクトリに置きたいならば、出力ディレクトリを >> /tmp/pupdvdtool-mplex.txt
echo 選ぶ必要はありません。 >> /tmp/pupdvdtool-mplex.txt

cd /

export MULTIPLEXGUI="
<window title=\"pupdvdtool multiplex gui \"icon-name=\"gtk-cdrom\">
<hbox>
<vbox>
    
	<frame m2vファイルの選択>
    <hbox>
      <entry accept=\"filename\">
        <label>存在するファイルを選択</label>
        <variable>ENTRY1</variable>
			<action>echo \$ENTRY1 | tee /tmp/pupdvdtool-file.txt</action>
      </entry>
      <button>
        <input file stock=\"gtk-file\"></input>
        <variable>FILE_BROWSE_FILENAME</variable>
        <action type=\"fileselect\">ENTRY1</action>
      </button>
    </hbox>
  </frame>
  
	<frame オーディオファイルの選択>
    <hbox>
      <entry accept=\"filename\">
        <label>存在するファイルを選択</label>
        <variable>ENTRY2</variable>
			<action>echo \$ENTRY2 | tee /tmp/pupdvdtool-file2.txt</action>
      </entry>
      <button>
        <input file stock=\"gtk-file\"></input>
        <variable>FILE_BROWSE_FILENAME</variable>
        <action type=\"fileselect\">ENTRY2</action>
      </button>
    </hbox>
  </frame>
  
	<frame 出力ディレクトリの選択>
	<hbox>
      <entry accept=\"directory\">
        <label>ディレクトリの選択</label>
        <variable>ENTRY3</variable>
      <action>echo \$ENTRY3 | tee /tmp/pupdvdtool-dir.txt</action>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <variable>FILE_BROWSE_DIRECTORY</variable>
        <action type=\"fileselect\">ENTRY3</action>
      </button>
  </hbox>
  </frame>
    
    <hbox>
      <text><label>多重化m2v と選択したオーディオをmpgにする</label></text>
      <button>
       <input file stock=\"gtk-yes\"></input>
        <label>多重化</label>
      <action type=\"exit\">Exit by button</action>
      </button>
    </hbox>

<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/pupdvdtool-mplex.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=MULTIPLEXGUI --center

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

M2V=`cat /tmp/pupdvdtool-file.txt`
if [ "`grep -i m2v /tmp/pupdvdtool-file.txt`" = "" ]; then
Xdialog --title "MESSAGE BOX" --msgbox "m2v file was not selected." 0 0
pupdvdtool
exit 0
fi
rm -f /tmp/pupdvdtool-file.txt
AUDIO=`cat /tmp/pupdvdtool-file2.txt`
rm -f /tmp/pupdvdtool-file2.txt
if [ -f /tmp/pupdvdtool-dir.txt ]; then
DIR=`cat /tmp/pupdvdtool-dir.txt`/
rm -f /tmp/pupdvdtool-dir.txt
else
DIR=`dirname "$M2V"`/
fi

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

TMPDIR=`dirname "$M2V"`/
cd $TMPDIR
M2VFILE=`basename $M2V`
new_file=`echo $M2VFILE|sed "s/ /_/g"`
if [ "$M2VFILE" != "$new_file" ]; then
mv "$M2VFILE" "$new_file"
M2VFILE="$new_file"
fi
M2V="$TMPDIR""$M2VFILE"

TMPDIR=`dirname "$AUDIO"`/
cd $TMPDIR
AUDIOFILE=`basename $AUDIO`
new_file=`echo $AUDIOFILE|sed "s/ /_/g"`
if [ "$AUDIOFILE" != "$new_file" ]; then
mv "$AUDIOFILE" "$new_file"
AUDIOFILE="$new_file"
fi
AUDIO="$TMPDIR""$AUDIOFILE"

MPG=`basename $M2V | sed "s/.m2v/.mpg/g" | sed "s/.M2V/.mpg/g"`

rxvt -bg orange --geometry 60x10 -e ffmpeg -vcodec copy -i "$M2V" -acodec copy -i "$AUDIO" -target "$choice1"-"$choice2" "$DIR""$MPG"

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



