#!/bin/sh

if [ "`which ctorrent`" = "" ]; then
Xdialog --title "MESSAGE BOX" --msgbox  "ctorrentをインストールする必要があります" 0 0
exit 0
fi

if [ "`which whoami`" != "" ]; then
WHOAMI=`whoami`
else
WHOAMI=root
fi

rm -f /tmp/$WHOAMI-pupctorrent*

if ! [ -d $HOME/pupctorrent ]; then
mkdir $HOME/pupctorrent
fi
if ! [ -d $HOME/pupctorrent/trackers ]; then
mkdir $HOME/pupctorrent/trackers
fi
if ! [ -d $HOME/pupctorrent/torrents ]; then
mkdir $HOME/pupctorrent/torrents
fi
if ! [ -d $HOME/.pupctorrent ]; then
mkdir $HOME/.pupctorrent
fi
if ! [ -f $HOME/.pupctorrent/down ]; then
echo 500 > $HOME/.pupctorrent/down
fi
if ! [ -f $HOME/.pupctorrent/up ]; then
echo 70 > $HOME/.pupctorrent/up
fi
if ! [ -f $HOME/.pupctorrent/port ]; then
echo 6881 > $HOME/.pupctorrent/port
fi
if ! [ -f $HOME/.pupctorrent/exithr ]; then
echo 24 > $HOME/.pupctorrent/exithr
fi
if ! [ -f $HOME/.pupctorrent/exitratio ]; then
echo > $HOME/.pupctorrent/exitratio
fi
if ! [ -f $HOME/.pupctorrent/cache ]; then
echo 16 > $HOME/.pupctorrent/cache
fi
if ! [ -f $HOME/.pupctorrent/size ]; then
echo 16 > $HOME/.pupctorrent/size
fi
if ! [ -f $HOME/.pupctorrent/ipl ]; then
echo > $HOME/.pupctorrent/ipl
fi
#if ! [ -f $HOME/.pupctorrent/max ]; then
#echo 10 > $HOME/.pupctorrent/max
#fi
#if ! [ -f $HOME/.pupctorrent/min ]; then
#echo 1 > $HOME/.pupctorrent/min
#fi
if ! [ -f $HOME/.pupctorrent/dir ]; then
echo / > $HOME/.pupctorrent/dir
fi
if ! [ -f $HOME/.pupctorrent/pupctorrenttorrfile ]; then
echo > $HOME/.pupctorrent/pupctorrenttorrfile
fi

echo -CD0202-Ctorrent > /tmp/$WHOAMI-pupctorrent-progs
echo -AZ2504-Azureus >> /tmp/$WHOAMI-pupctorrent-progs

PROG=""
for ONEPROG in `cat /tmp/$WHOAMI-pupctorrent-progs`
do
 PROGS="$PROGS<item>$ONEPROG</item>"
done

cd /

export Pupctorrent="
<window title=\"Pupctorrent-0.9 \"icon-name=\"gtk-network\">
<hbox>
<vbox>
    
	<frame torrentファイルを選択>
    <hbox>
      <entry accept=\"filename\">
        <label>既存のファイルを選んで下さい</label>
        <variable>FILE</variable>
			<input>cat \$HOME/.pupctorrent/pupctorrenttorrfile</input>
			<action>echo \$FILE | tee \$HOME/.pupctorrent/pupctorrenttorrfile</action>
			<action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -f &</action>
      </entry>
      <button>
        <input file stock=\"gtk-file\"></input>
        <variable>FILE_BROWSE_FILENAME</variable>
			<action type=\"fileselect\">FILE</action>			
      </button>
    </hbox>
  </frame>

	<frame (保存先/シードする)ディレクトリを選択して下さい>
	<hbox>
      <entry accept=\"directory\">
        <label>ディレクトリの選択</label>
        <variable>DIR</variable>
			<input>cat \$HOME/.pupctorrent/dir</input>
			<action>echo \$DIR | tee \$HOME/.pupctorrent/dir</action>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <variable>FILE_BROWSE_DIRECTORY</variable>
        <action type=\"fileselect\">DIR</action>
      </button>
  </hbox>
  </frame>
    
    <frame 最大ブロックサイズは128KBです>
    <hbox>
    <vbox>
      <text><label>キャッシュサイズをMBで</label></text>
      <entry>
        <variable>CACHE</variable>
        <input>cat \$HOME/.pupctorrent/cache</input>
      <action>echo \$CACHE | tee \$HOME/.pupctorrent/cache</action>
      <action>refresh:CACHE</action>
      </entry>
		</vbox>
		
		<vbox>
      <text><label>ブロックサイズをKBで</label></text>
      <entry>
        <variable>SIZE</variable>
        <input>cat \$HOME/.pupctorrent/size</input>
      <action>echo \$SIZE | tee \$HOME/.pupctorrent/size</action>
      <action>refresh:SIZE</action>
      </entry>
      </vbox>
      
		</hbox>    
    
    <hbox>
    <vbox>
      <text><label>最高ダウンロードレート:</label></text>
      <entry>
        <variable>DOWN</variable>
        <input>cat \$HOME/.pupctorrent/down</input>
      <action>echo \$DOWN | tee \$HOME/.pupctorrent/down</action>
		<action>refresh:DOWN</action>
      </entry>
      </vbox>

		<vbox>
      <text><label>最高アップロードレート:</label></text>
      <entry>
        <variable>UP</variable>
        <input>cat \$HOME/.pupctorrent/up</input>
      <action>echo \$UP | tee \$HOME/.pupctorrent/up</action>
      <action>refresh:UP</action>
      </entry>
      </vbox>
      </hbox>
      
      <hbox>
    <vbox>
      <text><label>聞くポート</label></text>
      <entry>
        <variable>PORT</variable>
        <input>cat \$HOME/.pupctorrent/port</input>
      <action>echo \$PORT | tee \$HOME/.pupctorrent/port</action>
      <action>refresh:PORT</action>
      </entry>
    </vbox>      
      
    <vbox>
      <text><label>最初にダウンロードするファイル</label></text>
      <entry>
        <variable>NUM</variable>
      <action>refresh:NUM</action>
      </entry>
		</vbox>     
      
	</hbox>
	</frame>
		    
    <frame シード後、終了>

    <hbox>
    <radiobutton active=\"true\">
      <label>時間数:            </label>
      <variable>RADIOBUTTON1</variable>
		<action>if true enable:EXITHR</action>
      <action>if false disable:EXITHR</action>
    </radiobutton>
    <radiobutton active=\"false\">
      <label>商(UL/DL):</label>
      <variable>RADIOBUTTON2</variable>
		<action>if true enable:EXITRATIO</action>
      <action>if false disable:EXITRATIO</action>
    </radiobutton>
    </hbox>
    
    <hbox>
      <entry>
        <variable>EXITHR</variable>
        <input>cat \$HOME/.pupctorrent/exithr</input>
      <action>echo \$EXITHR | tee \$HOME/.pupctorrent/exithr</action>
      <action>refresh:EXITHR</action>
      </entry>

      <entry>
        <variable>EXITRATIO</variable>
			<visible>disabled</visible>
        <input>cat \$HOME/.pupctorrent/exitratio</input>
      <action>echo \$EXITRATIO | tee \$HOME/.pupctorrent/exitratio</action>
      <action>refresh:EXITRATIO</action>
      </entry>
      </hbox>

		</frame>
		
    <frame ctorrentを別のプログラムに偽装する(Peer ID)>
    <hbox>
    <combobox>	
     <variable>PROGCOMBO</variable>
      $PROGS
    </combobox>
    </hbox>
    </frame>"
		
####This doesn't seem to work right

#	 <frame Listen for connection on specified ip>
#    <hbox>
#      <text><label>leave blank for all ip's</label></text>
#      <entry>
#        <variable>IPL</variable>
#        <input>cat \$HOME/.pupctorrent/ipl</input>
#      <action>echo \$IPL | tee \$HOME/.pupctorrent/ipl</action>
#      <action>refresh:IPL</action>
#      </entry>
#     </hbox>
#    </frame>  

Pupctorrent="$Pupctorrent

 <frame torrent作成 - 存在をチェック - メタ情報を見る> 
  <hbox>   
  
   <button>
      <input file stock=\"gtk-yes\"></input>
      <label>作成</label>
      <action>pupcreatetorrent &</action>
    </button> 

    <button>
       <input file stock=\"gtk-info\"></input>
      <label>チェック</label>
      <action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -c &</action>
    </button>
  
    <button>
       <input file stock=\"gtk-info\"></input>
      <label>メタ情報</label>
      <action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -x &</action>
    </button>"

if [ "`which transmissioncli`" != "" ]; then
Pupctorrent="$Pupctorrent
    <button>
       <input file stock=\"gtk-info\"></input>
      <label>スクレープ</label>
      <action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -scrape &</action>
    </button>"
fi
Pupctorrent="$Pupctorrent
  </hbox>
  </frame>
  
 <frame torrentをシード - torrentをダウンロード - プログラムの終了> 
  <hbox> 
  
    <button>
       <input file stock=\"gtk-network\"></input>
      <label>シード</label>
      <action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -e &</action>
      <action>touch /tmp/$WHOAMI-pupctorrent-seed</action>
    </button>
  
    <button>
       <input file stock=\"gtk-network\"></input>
      <label>ダウンロード</label>
      <action>/usr/local/apps/Network/Pupctorrent/pupctorrentexec -e &</action>
    </button>
    
    <button>
      <input file stock=\"gtk-help\"></input>
      <label>ヘルプ</label>
      <action>defaulthtmlviewer /usr/local/apps/Network/Pupctorrent/help.html &</action>
    </button>

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

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

gtkdialog3 --program=Pupctorrent --center

rm -f /tmp/$WHOAMI-pupctorren*

exit 0

#   <hbox>
#      <text><label>Max number of peers</label></text>
#      <entry>
#        <variable>MAX</variable>
#        <input>cat \$HOME/.pupctorrent/max</input>
#      <action>echo \$MAX | tee \$HOME/.pupctorrent/max</action>
#      <action>refresh:MAX</action>
#      </entry>
#		</hbox>
		
#    <hbox>
#      <text><label>Minimum number of peers</label></text>
#      <entry>
#        <variable>MIN</variable>
#        <input>cat \$HOME/.pupctorrent/min</input>
#      <action>echo \$MIN | tee \$HOME/.pupctorrent/min</action>
#      <action>refresh:MIN</action>
#      </entry>
#		</hbox>

