#!/bin/bash

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 [ "$1" = -scrape ]; then
if [ "$FILE" != "" ]; then
	gxmessage -borderless -font "sans bold 14" -center -bg "orange" -buttons "" " スクレープ進行中" &
        X1PID=$!
	transmissioncli -s "$FILE" > /tmp/$WHOAMI-pupctorrent-scrape
	kill $X1PID 2>/dev/null
export Puptransmissioncliinfo="	
<window title=\"scrape info \"icon-name=\"gtk-network\">
<hbox>
<vbox>
<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/$WHOAMI-pupctorrent-scrape</input>
      <width>400</width>
      <height>300</height>
    </edit>
</frame>
<hbox>
<button ok></button>
</hbox>
</vbox>
</hbox>
</window>"
gtkdialog3 --program=Puptransmissioncliinfo --center
fi
exit 0
fi

if [ "$1" = -c ]; then
if [ "$FILE" = "" ]; then
exit 0
fi
	cd "$DIR"
	gxmessage -borderless -font "sans bold 14" -center -bg "orange" -buttons "" " チェック進行中" &
        X1PID=$!
	ctorrent -c "$FILE" > /tmp/$WHOAMI-pupctorrent
	kill $X1PID 2>/dev/null
export Pupctorrentinfo="
<window title=\"check torrent \"icon-name=\"gtk-network\">
<hbox>
<vbox>
<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/$WHOAMI-pupctorrent</input>
      <width>400</width>
      <height>500</height>
    </edit>
</frame>
<hbox>
<button ok></button>
</hbox>
</vbox>
</hbox>
</window>"
gtkdialog3 --program=Pupctorrentinfo --center
exit 0
fi

if [ "$1" = -x ]; then
if [ "$FILE" = "" ]; then
exit 0
fi
	ctorrent -x "$FILE" > /tmp/$WHOAMI-pupctorrent
export Pupctorrentinfo="	
<window title=\"torrent metainfo \"icon-name=\"gtk-network\">
<hbox>
<vbox>
<frame 情報:>
    <edit>
      <variable>INFO</variable>
      <input file>/tmp/$WHOAMI-pupctorrent</input>
      <width>400</width>
      <height>500</height>
    </edit>
</frame>
<hbox>
<button ok></button>
</hbox>
</vbox>
</hbox>
</window>"
gtkdialog3 --program=Pupctorrentinfo --center
exit 0
fi

if [ "$1" = -e ]; then
	cd "$DIR"
	FILE2=`basename "$FILE"`
	FILE3=`echo "$FILE2" | sed 's/.torrent//g'`
	if [ "$DOWN" != "" ]; then
	DOWN="-D "$DOWN""
	fi
	if [ "$UP" != "" ]; then
	UP="-U "$UP""
	fi
	if [ "$EXITHR" != "" ]; then
	EXITRATIO=""
	EXITHR="-e "$EXITHR""
	fi
	if [ "$EXITRATIO" != "" ]; then
	EXITRATIO="-E "$EXITRATIO""
	fi
	if [ "$PORT" != "" ]; then
	PORT2="$PORT"
	PORT="-p "$PORT""
	fi
	if [ "$CACHE" != "" ]; then
	CACHE="-C "$CACHE""
	fi
#	if [ "$MAX" != "" ]; then
#	MAX="-M "$MAX""
#	fi
#	if [ "$MIN" != "" ]; then
#	MIN="-m "$MIN""
#	fi
	if [ "$SIZE" != "" ]; then
	SIZE="-z "$SIZE""
	fi
	if [ "$NUM" != "" ]; then
	NUM2=" -n "$NUM" "
	else
	NUM2=" "
	fi
#	if [ "$IPS" = "" ]; then
#	IPS2=" "
#	else
#        IPS2=" -i "$IPS" "
#	fi
	PROG=`echo "$PROGCOMBO" | cut -f 2 -d '-'`
	PROG2="-P -"$PROG"-"
	echo "#!/bin/sh" > /tmp/$WHOAMI-pupctorrentexec
	if [ -f /tmp/$WHOAMI-pupctorrent-seed ]; then
	rm -f /tmp/$WHOAMI-pupctorrent-seed
	echo ctorrent "$DOWN" "$UP" "$EXITHR" "$EXITRATIO" "$PORT" "$CACHE""$NUM2""$SIZE" "$PROG2" -f \"\$FILE\" | tr -s ' ' >> /tmp/$WHOAMI-pupctorrentexec
	else
	echo ctorrent "$DOWN" "$UP" "$EXITHR" "$EXITRATIO" "$PORT" "$CACHE""$NUM2""$SIZE" "$PROG2" \"\$FILE\" | tr -s ' ' >> /tmp/$WHOAMI-pupctorrentexec
	fi
	chmod a+x /tmp/$WHOAMI-pupctorrentexec
	rxvt -bg orange -title "$FILE3"-port-"$PORT2" --geometry 79x10 -e /tmp/$WHOAMI-pupctorrentexec
exit 0
fi

if [ "$1" = -f ]; then
TORRFILE="`cat $HOME/.pupctorrent/pupctorrenttorrfile`"
dirname "$TORRFILE" > $HOME/.pupctorrent/torrdir
exit 0
fi



