#!/bin/sh

c_red="\033[1;31m"
c_green="\033[1;32m"
c_yellow="\033[1;33m"
c_cyan="\033[1;36m"
c_reset="\033[0m"

down(){
    tput reset
    notify-send "Torrent: Downloading"
    aria2c -d movies --select-file=$2 --seed-time=0 $1 && notify-send "Torrent: Download complete" && rm -f $file || notify-send -u critical "Torrent: Error!!" 
}

trap "rm -f $HOME/.cache/*.torrent;exit 0" INT HUP
[ -z "$*" ] && magnet="$(xclip -out clipboard)" || magnet="$*"
echo "$magnet"
printf "${c_yellow}Download>>\n${c_green}[f]ull torrent\n${c_cyan}[p]artial torrent \n${c_red}[q]uit"
printf "${c_reset}\n\tenter choice:"
read -r ch
case $ch in
    f)
    	down "$magnet";;
    p)
    	aria2c --dir=$HOME/.cache --bt-metadata-only=true --bt-save-metadata=true "$magnet"
    	tput reset
    	aria2c --show-files=true $HOME/.cache/*.torrent 
   	printf "Enter file idx(default=all):"
    	read -r ind
	down "$HOME/.cache/*.torrent" "$ind"
    	rm -f $HOME/.cache/*.torrent;;
    q)
	exit 0;;
esac
