#!/bin/bash

###by plinej
###pupdvdtool-0.4 December 2006
###updated February 2007
###This is the dvd author gui

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

rm -f /tmp/author.txt
rm -f /tmp/authorlog.txt
rm -f /tmp/pupdvdtool-cbox1.txt
rm -f /tmp/pupdvdtool-cbox2.txt
rm -f /tmp/pupdvdtool-cbox3.txt
rm -f /tmp/pupdvdtool-name.txt

echo DvdauthorはDVDファイル構造に > /tmp/author.txt
echo DVD対応mpg/vobファイルを書きます。 >> /tmp/author.txt
echo >> /tmp/author.txt
echo 著者ボタンをクリックしたあと、 >> /tmp/author.txt
echo 入力ファイルかディレクトリを促されます。 >> /tmp/author.txt

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

	<frame シングルファイルを書きますか、多重化ファイルを書きますか?>
  <hbox>
    <radiobutton active=\"true\">
      <label>シングル</label>
      <variable>CHECKBOX</variable>
    </radiobutton>
    
    <radiobutton active=\"false\">
      <label>多重化</label>
      <variable>CHECKBOX2</variable>
    </radiobutton>
	</hbox>
  </frame>
    
    <frame 毎5分ごとに章を追加しますか?>
    	<hbox>
    <checkbox>
      <label>チェックでyes</label>
      <variable>CHECKBOX4</variable>
      <default>true</default>
    </checkbox>
    </hbox>
    </frame>
    
    <frame 書いたDVDをディレクトリに入力ファイルとして保存しますか?>
    <hbox>
    <checkbox>
      <label>チェックでyes</label>
      <variable>CHECKBOX3</variable>
      <default>true</default>
    </checkbox>
    </hbox>
    </frame>
    
    <hbox>
      <text><label>出力DVDの名前を入力して下さい:</label></text>
      <entry>
        <variable>WORD</variable>
      <action>echo \$WORD | tee /tmp/pupdvdtool-name.txt</action>
      </entry>
    </hbox>
    
    <hbox>
      <button>
       <input file stock=\"gtk-edit\"></input>
        <label>著者</label>
	<action>echo \$CHECKBOX | tee /tmp/pupdvdtool-cbox1.txt</action>
	<action>echo \$CHECKBOX2 | tee /tmp/pupdvdtool-cbox2.txt</action>
	<action>echo \$CHECKBOX3 | tee /tmp/pupdvdtool-cbox3.txt</action>
	<action>echo \$CHECKBOX4 | tee /tmp/pupdvdtool-cbox4.txt</action>
	<action type=\"exit\">Exit by button</action>
      </button>
    </hbox>

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

  <hbox>     

    <button>
       <input file stock=\"gtk-cdrom\"></input>
      <label>pupdvdtool</label>
      <action>touch /tmp/pupdvdtool-exit.txt</action>
      <action>touch /tmp/pupdvdtool-exit2.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=AUTHORGUI --center

if [ -f /tmp/pupdvdtool-exit.txt ]; then
EXIT=""
rm -f /tmp/authorlog.txt
rm -f /tmp/pupdvdtool-cbox1.txt
rm -f /tmp/pupdvdtool-cbox2.txt
rm -f /tmp/pupdvdtool-cbox3.txt
rm -f /tmp/pupdvdtool-name.txt
rm -f /tmp/pupdvdtool-exit.txt
if [ -f /tmp/pupdvdtool-exit2.txt ]; then
rm -f /tmp/pupdvdtool-exit2.txt
pupdvdtool
fi
exit 0
fi

CHECKBOX4=`cat /tmp/pupdvdtool-cbox4.txt`
rm -f /tmp/pupdvdtool-cbox4.txt

if ! [ -d $HOME/.pupdvdtool ]; then
mkdir $HOME/.pupdvdtool
fi
if ! [ -f $HOME/.pupdvdtool/authordir ]; then
echo / > $HOME/.pupdvdtool/authordir
fi
if ! [ -f $HOME/.pupdvdtool/authordirsave ]; then
echo / > $HOME/.pupdvdtool/authordirsave
fi
AUTHORDIR=`cat $HOME/.pupdvdtool/authordir`
AUTHORDIRSAVE=`cat $HOME/.pupdvdtool/authordirsave`
if ! [ -d "$AUTHORDIR" ]; then
AUTHORDIR="/"
fi
if ! [ -d "$AUTHORDIRSAVE" ]; then
AUTHORDIRSAVE="/"
fi

if [ "`cat /tmp/pupdvdtool-cbox1.txt`" = true ]; then
FILE=`Xdialog --title "Please choose a file to author" --fselect "$AUTHORDIR" 0 0 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
echo "`dirname "$FILE"`" > $HOME/.pupdvdtool/authordir
fi

if [ "`cat /tmp/pupdvdtool-cbox2.txt`" = true ]; then
DIR=`Xdialog --title "Choose directory of files to author" --dselect "$AUTHORDIR" 0 0 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
echo "$DIR" > $HOME/.pupdvdtool/authordir
fi

if [ "`cat /tmp/pupdvdtool-cbox3.txt`" = false ]; then
DIR2=`Xdialog --title "Select your save to directory" --dselect "$AUTHORDIRSAVE" 0 0 2>&1`
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
echo "$DIR2" > $HOME/.pupdvdtool/authordirsave
fi

if [ "`cat /tmp/pupdvdtool-cbox1.txt`" = true ]; then
if [ "`cat /tmp/pupdvdtool-cbox3.txt`" = true ]; then
DIR=`dirname  "$FILE"`/
fi
fi

if [ "`cat /tmp/pupdvdtool-cbox3.txt`" = true ]; then
DIR2="$DIR"
fi

cd $DIR

if [ "`cat /tmp/pupdvdtool-cbox2.txt`" = true ]; then
ls -1 | grep -i .mpg | while read files
do
new_file=`echo $files | sed "s/ /_/g" | sed "s/.mpg/.vob/g" | sed "s/.MPG/.vob/g"`
if [ "$files" != "$new_file" ]; then
mv "$files" "$new_file"
fi
done
ls -1 | grep -i .vob | while read files
do
new_file=`echo $files | sed "s/ /_/g" | sed "s/.VOB/.vob/g"`
if [ "$files" != "$new_file" ]; then
mv "$files" "$new_file"
fi
done
fi

if [ "$FILE" != "" ]; then
FILE=`basename $FILE`
ffmpeg -i "$FILE" 2> /tmp/ffmpeg.txt
fi

if [ "`cat /tmp/pupdvdtool-name.txt`" = "" ]; then
echo NEW-DVD > /tmp/pupdvdtool-name.txt
fi

cd "$DIR"

###automatic chapters every 5 minutes ##################################
if [ "$CHECKBOX4" = true ]; then
rm -f /tmp/temp-chapters
rm -f /tmp/temp-chapters2
rm -f /tmp/temp-length
echo "If the following information is incorrect the chapters" > /tmp/temp-length
echo "function probably won't work. This seems to be a problem" >> /tmp/temp-length
echo "with the mpeg headers created by vobcopy." >> /tmp/temp-length
echo >> /tmp/temp-length
if [ "$FILE" = "" ]; then
FILE=`ls -1 | grep -i .vob`
fi
echo "$FILE" | while read FILE
do
FILEINFO=`ffmpeg -i "$FILE" 2>&1`
HOUR=`echo "$FILEINFO" | grep 'Duration:' | sed 's/ //g' | cut -f 2 -d ':' | sed 's/0//g'`
MINUTE=`echo "$FILEINFO" | grep 'Duration:' | sed 's/ //g' | cut -f 3 -d ':'`
if [ "$HOUR" = "" ]; then
TMPHOUR=0
else
TMPHOUR="$HOUR"
fi
echo ""$FILE" is reported by ffmpeg to be "$TMPHOUR" hours and "$MINUTE" minutes" >> /tmp/temp-length
if [ "$HOUR" != "" ]; then
x=1
y=0
z=`expr "$HOUR" + 1`
while [ "$x" != "$z" ]; do
echo ""$y":00:00,"$y":05:00,"$y":10:00,"$y":15:00,"$y":20:00,"$y":25:00,"$y":30:00,"$y":35:00,"$y":40:00,"$y":45:00,"$y":50:00,"$y":55:00" >> /tmp/temp-chapters
y=`expr "$y" + 1`
x=`expr "$x" + 1`
done
fi
if [ "$x" = "" ]; then
x=0
fi
MINUTES=`echo "(( "$MINUTE" / 5 ))" | bc -l | cut -f 1 -d '.'`
if [ "$MINUTES" = "" ]; then
MINUTES=0
fi
echo ""$x":00:00" >> /tmp/temp-chapters
if [ "$MINUTES" != 0 ]; then
a=1
while [ "$a" != "`expr "$MINUTES" + 1`" ]; do
w=`echo "(( "$a" * 5 ))" | bc -l`
if [ "$w" = 5 ]; then
w=05
fi
echo ""$x":"$w":00" >> /tmp/temp-chapters
a=`expr "$a" + 1`
done
fi
echo "-c `cat /tmp/temp-chapters | tr '\n' ',' | sed 's/\(.*\),/\1/'` "$FILE"" >> /tmp/temp-chapters2
rm -f /tmp/temp-chapters
done
FILE=`cat /tmp/temp-chapters2 | tr '\n' ' '`
fi

if [ "$FILE" = "" ]; then
ffmpeg -i `ls -1 | grep '.vob' | sed -n '1p'` 2> /tmp/ffmpeg.txt
FILE=`ls -1 | grep -i .vob | tr '\n' ' '`
fi

####################################################################################

if [ "`grep 'Audio: pcm' /tmp/ffmpeg.txt`" != "" ]; then
Xdialog --wrap --title "YES/NO BOX" --yesno "ビデオにはPCMオーディオが含まれていて、
オーサリング中に破損される事があります。続ける前に、
ffmpeggui でビデオをDVDビデオに変換しておいた方が
良いでしょう。これはオーディオを ac3 に変換します。
とにかく続けるには「yes」を、そうでなければ「no」をクリック。" 0 0
if [ "$?" != 0 ]; then pupdvdtool; exit 0; fi
fi

echo "dvdauthor -o "$DIR2"`cat /tmp/pupdvdtool-name.txt`/ -t "$FILE"" > /tmp/tempauthor
rxvt -bg orange --geometry 79x10 -e `cat /tmp/tempauthor`
echo "dvdauthor -o "$DIR2"`cat /tmp/pupdvdtool-name.txt` -T" > /tmp/tempauthor
rxvt -bg orange --geometry 79x10 -e `cat /tmp/tempauthor`
rm -f /tmp/tempauthor

if [ -f /tmp/temp-length ]; then
defaulttexteditor /tmp/temp-length &
fi

Xdialog --title "MESSAGE BOX" --msgbox "dvdオーサリングは完了しました." 0 0 &

pupdvdtool

rm -f /tmp/author.txt
rm -f /tmp/pupdvdtool-cbox1.txt
rm -f /tmp/pupdvdtool-cbox2.txt
rm -f /tmp/pupdvdtool-cbox3.txt
rm -f /tmp/pupdvdtool-name.txt

exit 0
