#!/bin/bash

# Title......: get2 -The rewrite!
# Description: my personal download manager
# Author.....: Mitchell Johnston - uid 0
# Contact....: johnstonm401@gmail.com
# Updated....: Sat 09 Mar 2024 03:44:57 PM CST
#----------------------------------

# Sections: Changes variables functions setup main
# use '#' in vi/vim to jump to word under cursor

: ' Changes
Fri Feb 23 2024 Added version() and changed "files" to "mkindex"
Sun Oct 29 2023 added random color output for fun
'

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
DOW=$(date +%a)                            # day of week: Thu
TODAY=$(date +%m/%d)                       # month/day: 03/25
DOM=$(date +%d)                            # day of month: 25
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script

# Colors - uncomment if needed
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
ROWS=$(tput lines)
COLS=$(tput cols)

# functions
#----------------------------------

ren(){ ## fix names (rename)
	# Use: ren {file}
	[ "$DEBUG" == "1" ] && set -x
	[ "$(pwd)" == "$HOME" ] && return
	for FILE in "$@"
	do
		[ -z "$FILE" ] && return
        NEW=$(echo "$FILE"|tr [A-Z] [a-z] | sed 's/[[:blank:]]/-/g;s/[^[:alnum:].-]/-/g' |tr -cs '[:alnum:]'| sed 's/\-\./.'/);
		[ "$FILE" != "$NEW" ] && mv -v "$FILE" "$NEW" 2>/dev/null
	done
}

bl(){ ## write a blank line
	# Use: bl
	[ "$DEBUG" == "1" ] && set -x
    echo ""
}

html(){ ## mark up coda using vim syntax 
	# Use: html {file}
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}

log(){ ## creates a basic log entry, to system default log.
	# Use: log {entry}  
	[ "$DEBUG" == "1" ] && set -x
	logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
	# Use: pause  {optional number of seconds} or "-nt" for no time out 
	[ "$DEBUG" == "1" ] && set -x
	[ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
	then
		read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
		read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

xtitle(){ ## set window title
	# Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

version(){ ## display version and change history
    grep -E '^# Updated' $0
    bl
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
	vim $0
	sed -i -e "7s/.*/# Updated....: $(date)/" $0
	log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    cp $0 /var/gopher/scripts
	exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
$NAME {option}

A youtube-dl wrapper for URLs from the Internet.

{URLs}   # One or more URLs passed as an argument. Should be quoted as they tend to have reserved charicters.
-a       # Add file to ~/Downloads/get.txt, does not download.
-d       # Used when called from Vim, which I have assigned <F3> in my .vimrc

If no option in passed, then it will look for get.txt in the current directory and parse the URLs, one per line. If a file named unnamed.txt is found, it will rename it, and do likewise.

- Supports most video sites including Rumble (took some effort)
- Will download mp3, mp4, avi, jpg, jpeg, png, and webm files using wget
- Can be configured with vim/gvim to use as front end (see below)

Add the 3 lines below to your .vimrc to intagrate it (change the location of "get" path):
" This calls my download manager. I make a list in vim, hit F3 and it
" downloads them. It will then terminate vim/gvim.
map <F3> <ESC>:w ~/Downloads/get.txt<CR>:term ++close /home/mitch/bin/get -d<CR>

END
exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi

# listing of required apps
command -v wget >/dev/null || sudo apt install wget -yyq 
command -v ffmpeg >/dev/null || sudo apt install wget -yyq 
command -v exiftool >/dev/null || sudo apt install libimage-exiftool-perl -qyy
command -v youtube-dl >/dev/null || sudo pip install --upgrade youtube_dl
command -v lm >/dev/null || echo "Missing lm, download from http://crn.hopto.org/unix/lm"


# main
#--------------------------- 
[ "$DEBUG" == 1 ] && set -x

# This allows me to use it from inside vim, no matter what directory I am in.
# <F3> in vim will write the current buffer to this file, open a term, and call "get -d".
[ "$1" == "-d" ] && VIM=kill
[ "$1" == "-d" ] && cd /home/mitch/Downloads && shift 1

# used to at a file to the get list
[ "$1" == "-a" ] && echo "$2" >> ~/Downloads/get.txt && exit

# Setup file list "get.txt"
[ -f untitled.txt ] && mv untitled.txt get.txt # ren untitled to get
while [ "$#" -gt 0 ]
do
	echo $1 >>get.txt
	shift 1
done
TC="$(wc -l<get.txt)"
CC=1
for INPUT in $(cat get.txt)
do
	echo "${BG}Getting:${N} $CC ${BG}of${N} $TC"
	tput setaf $((RANDOM % 256)) # Set a random foreground color
	log "$CC of $TC: $INPUT"
	if [[ "$INPUT" == *"rumble"* ]]
	then
		youtube-dl --console-title --no-check-certificate -f mp4-480p/webm-480p/mp4-360p/mp4-720p/mp4-1080p $(curl -s "$INPUT" | tr -d '\n'|awk -F "embedUrl" '{print $2}'|awk -F '"' '{print $3}') 
		echo "${N}"
	elif [ "$(echo "$INPUT" | grep -c -E 'mp4|mp3|avi|webm|jpg|jpeg|png')" -eq 0 ]
	then
		URL="$(echo "$INPUT"|cut -d'&' -f 1)" # Just grab content root, not any list
		youtube-dl  --console-title -f mp4 --output "%(title)s.%(ext)s" "$URL"  |tee /tmp/$$.vid 
		echo "${N}"
	else
		FILE="${INPUT##*/}"
		xtitle "Get: $FILE"
		wget "$INPUT" ||pause
		echo "${N}"
	fi
	if [ -f /tmp/$$.vid ]
	then
		TITLE=$(grep  Destination /tmp/$$.vid| cut -d: -f2)
		gio trash /tmp/$$.vid
		log "$CC of $TC: $TITLE" 
	else
		log "$CC of $TC" 
	fi
	let "CC = CC + 1"
done 
gio trash get.txt

/home/mitch/bin/rn * >/dev/null

lm
exiftool -n -q -p '${Duration;our $sum;$_=ConvertDuration($sum+=$_) }' ./*.mp4 2>/dev/null| tail -n1
#[[ $(pwd) == *"www"* ]] && mkindex # if downloading to site a directory, run mkindex and post ASAP
log "$TC Download(s) completed"
[ "$VIM" = "kill" ] && kill $PPID
exit 0