#!/bin/sh

#Title: Puppy Download Manager
#Purpose: Learn gtkdialog and bash, make something bigger than Pwsget
#Licence: GPL3
#Author: puppy_apprentice (so far, contributions and errors correction appreciate and welcome)

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

TMPDIR=/tmp/gtkdialog/examples/"`basename $0`"
mkdir -p "$TMPDIR"

export TMPDIR
export download_list=`cat /root/download_list`
DOWNPLACE="$DEST"

funcClear() {
	echo -n "" > "$TMPDIR"/inputfile1
}

export -f funcClear

funcClear

echo -e "\nApplication directory: $PWD\nDownloads directory: $DEST\n" > "$TMPDIR"/info

funcStart_() {
	echo cat "$TMPDIR"/info
}

export -f funcStart_

funchelp() {
echo "testtttt" > "$LINKS1"
}

export -f funchelp

funcdowndir() {
	echo "$DEST" > "$TMPDIR"/info
}

export -f funcdowndir

funcAddFromClip() {
	if [ -s "$TMPDIR"/inputfile1 ]
	then
		echo -e -n "\n$(xclip -o)" >> "$TMPDIR"/inputfile1
	else
		echo -n "$(xclip -o)" >> "$TMPDIR"/inputfile1
	fi
}

export -f funcAddFromClip

funcAddFromLink() {
	if [ -s "$TMPDIR"/inputfile1 ]
	then
		echo -e -n "$LINKS1" >> "$TMPDIR"/inputfile1
	else
		echo -n "$LINKS1" >> "$TMPDIR"/inputfile1
	fi
}

export -f funcAddFromLink

funcLoadFromFile() {
	cat "$PWD/download_list" > "$TMPDIR"/inputfile1
}

export -f funcLoadFromFile

funcPrepareFile() {
	cat "$PWD/download_list" | tr -d '||' > "$TMPDIR"/temp
	cat "$TMPDIR"/temp > download_list
}

export -f funcPrepareFile

funcRemoveSelItem() {
	echo -n "$(grep -vx "$1" "$TMPDIR"/inputfile1)" > "$TMPDIR"/temp
	cat "$TMPDIR"/temp > "$TMPDIR"/inputfile1
}

export -f funcRemoveSelItem

funcMoveItemUp() {
	echo "$1" > "$TMPDIR"/temp
	echo -n "$(grep -vx "$1" "$TMPDIR"/inputfile1)" >> "$TMPDIR"/temp
	cat "$TMPDIR"/temp > "$TMPDIR"/inputfile1
}

export -f funcMoveItemUp

funcMoveItemDown() {
	echo -n "$(grep -vx "$1" "$TMPDIR"/inputfile1)" > "$TMPDIR"/temp
	echo -en "\n$1" >> "$TMPDIR"/temp
	cat "$TMPDIR"/temp > "$TMPDIR"/inputfile1
}

export -f funcMoveItemDown

funcStart_aria2() {
	if [ "$1" ] 
	then
	echo aria2 --disable-ipv6 "$1" & PID=$!
	echo $(( PID + 1 )) > pid
	else
		echo aria2 -i "$PWD/download_list" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_aria2

funcStart_aria2c() {
	if [ "$1" ] 
	then
	echo aria2c --disable-ipv6 "$1" & PID=$!
	echo $(( PID + 1 )) > pid
	else
		echo aria2c -i "$PWD/download_list" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_aria2c

funcStart_axel() {
	if [ "$1" ] 
	then
		echo axel --alternate "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		cat "$PWD/download_list" | xargs -d '\n' -l1  echo axel -a & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_axel

funcStart_curl() {
	if [ "$1" ] 
	then
		echo curl -L -O -C - "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		cat "$PWD/download_list" | xargs -d '\n' -l1  echo curl -L -O -C - & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_curl

funcStart_youtube_dl_video() {
	if [ "$1" ] 
	then
		echo youtube-dl "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		echo youtube-dl -a "$PWD/download_list" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_youtube_dl_video

funcStart_youtube_dl_mp3() {
	if [ "$1" ] 
	then
		echo youtube-dl -ignore-errors --format bestaudio --extract-audio --audio-format mp3 --audio-quality 160K --output '%\(title\)s.%\(ext\)s' "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		echo youtube-dl -ignore-errors --format bestaudio --extract-audio --audio-format mp3 --audio-quality 160K --output '%\(title\)s.%\(ext\)s' "'$download_list'" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_youtube_dl_mp3

funcStart_youtube_dl_playlist_mp3() {
	if [ "$1" ] 
	then
		echo youtube-dl --ignore-errors --format bestaudio --extract-audio --audio-format mp3 --audio-quality 160K --output '%\(title\)s.%\(ext\)s' --yes-playlist "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		echo youtube-dl --ignore-errors --format bestaudio --extract-audio --audio-format mp3 --audio-quality 160K --output '%\(title\)s.%\(ext\)s' --yes-playlist "'$download_list'" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_youtube_dl_playlist_mp3

funcStart_youtube_dl_playlist_video() {
	if [ "$1" ] 
	then
		echo youtube-dl -i -f mp4 --yes-playlist "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		echo youtube-dl -i -f mp4 --yes-playlist "'$download_list'" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_youtube_dl_playlist_video

funcStart_wget() {
	if [ "$1" ] 
	then
		echo wget -c "$1" & PID=$!
		echo $(( PID + 1 )) > pid
	else
		echo wget -c -i "'$download_list'" & PID=$!
		echo $(( PID + 1 )) > pid
	fi
}

export -f funcStart_wget

funcStop() {
	PID="$(cat pid)"
	kill -INT "$PID" &
}

export -f funcStop

downloaders="
youtube-dl
aria2
aria2c
axel
curl
wget
"

funcComboAddItems() {
for item in $downloaders
do
	if [ -e "$(which $item)" ]
	then
		if [ "$item" == "youtube-dl" ]
		then
			echo "<item>"youtube_dl_video"</item>"
			echo "<item>"youtube_dl_mp3"</item>"
			echo "<item>"youtube_dl_playlist_mp3"</item>"			
			echo "<item>"youtube_dl_playlist_video"</item>"
		else
			echo "<item>"$item"</item>"
		fi
	fi
done
}

MAIN_DIALOG='
<window title="Puppy Download Manager" icon-name="game" default_height="600" default_width="1100">
	<vbox>
		<menubar> 
  <menu>
    <menuitem tooltip-text="'"$(gettext "Puppy Dowload Manager Help")"'">
         <label>"'"$(gettext "PDM Help")"'"</label>
         <action>funchelp</action>
    </menuitem>
    <label>"'"$(gettext "help")"'"</label>
    </menu>
    </menubar>
		<hbox>
			<frame Paste Links Here: >
				<edit editable="true"  wrap-mode="2">
					<variable>LINKS1</variable>
					<height>260</height>
					<width>300</width>
				</edit>
			</frame>
			<tree>
				<label>Sort</label>
				<variable>LIST1</variable>
				<width>400</width>
				<input file>'"$TMPDIR"'/inputfile1</input>
				<output file>'"$PWD"'/download_list</output>
			</tree>
			<vbox>
				<button>
					<input file icon="add"></input>
					<label>Add Links</label>
					<action>funcAddFromLink</action>
					<action function="Refresh">LIST1</action>
					<action>clear:LINKS1</action>
				</button>
				<button>
					<input file icon="paste"></input>
					<label>Add from clipboard</label>
					<action>funcAddFromClip</action>
					<action function="Refresh">LIST1</action>
				</button>
				<button>
					<input file icon="new"></input>
					<label>Load from file</label>
					<action>funcLoadFromFile</action>
					<action function="Refresh">LIST1</action>
				</button>
				<button>
					<input file icon="save"></input>
					<label>Save list to file</label>
					<action function="Save">LIST1</action>
					<action>funcPrepareFile</action>
				</button>
				<button>
					<input file icon="clear"></input>
					<label>Clear list</label>
					<action function="Clear">LIST1</action>
					<action>funcClear</action>
				</button>
				<button>
					<input file icon="remove"></input>
					<label>Remove Selected</label>
					<action>funcRemoveSelItem "$LIST1"</action>
					<action function="Removeselected">LIST1</action>
				</button>
				<button>
					<input file icon="arrow_mini_up"></input>
					<label>Move Up</label>
					<action>funcMoveItemUp "$LIST1"</action>
					<action function="Refresh">LIST1</action>
				</button>
				<button>
					<input file icon="arrow_mini_down"></input>
					<label>Move Down</label>
					<action>funcMoveItemDown "$LIST1"</action>
					<action function="Refresh">LIST1</action>
				</button>
				 <hbox>
  <text><label>'$(gettext 'Folder:')'</label></text>
  <entry accept="directory"><variable>DEST</variable></entry>
  <button>
   <input file stock="gtk-open"></input>
   <action type="fileselect">DEST</action>
   <action>refresh:DEST</action>
   <action>funcdowndir</action>
  </button>
 </hbox>
				<text>
					<label>'$(gettext 'Downloaders:  ')'</label>
				</text>
				<combobox>
					<variable>COMBOBOX</variable>
					'"`funcComboAddItems`"'
				</combobox>
				<button>
					<input file icon="execute"></input>
					<label>Start</label>
					<action function="Save">LIST1</action>
					<action>funcPrepareFile</action>
					<action>refresh:vte0</action>
				</button>
				<button>
					<input file icon="dialog_stop"></input>
					<label>Stop</label>
					<action>funcStop</action>
				</button>
			</vbox>
		</hbox>
			<terminal has-focus="true" current-directory-uri="'$DEST'">
			<height>10</height>
			<variable>vte0</variable>
			<input>funcStart_"$COMBOBOX" "$LIST1"</input>
			<action>echo "vte0=$vte0 - Default child-exited signal"</action>
			<action signal="child-exited">echo "vte0=$vte0 - Resetting and re-executing command"</action>
			<action signal="child-exited">clear:vte0</action>
			<action signal="child-exited">echo "vte0=$vte0 - New PID"</action>
			<action signal="show">echo "vte0=$vte0 - show signal"</action>
		</terminal>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --space-expand=true --space-fill=true --program=MAIN_DIALOG ;;
esac
