set -o vi

shopt -s cdspell
shopt -s globstar
shopt -s autocd

alias c='cp -vi'
alias m='mv -vi'

alias h=history
alias l='ls --color=auto -g -G --group-directories-first -r -t'
alias lh='ls --color=auto -g -G --group-directories-first -r -t -h'
alias l1="ls --color=auto -1"

alias v='vim'

alias u='cd ..'
alias uu='cd ../..'
alias uuu='cd ../../..'
alias uuuu='cd ../../../..'
alias uuuuu='cd ../../../../..'

alias yd='yt-dlp'
alias ydv='yt-dlp -f 18'
alias ydlq='yt-dlp -f 160+140'
alias ydlq2='yt-dlp -f 160+249'
alias ydmq='yt-dlp -f 134+249'
alias yda='yt-dlp -f 140'
alias ydF='yt-dlp -F'
alias ydb='yt-dlp -f bv+140'
alias ydS='yt-dlp -S "+height:360"'

alias sdc='cd ~/storage/sdcard'
alias sdo='cd ~/storage/downloads'
alias sdr='cd ~/storage/shared'

alias sbrc='source ~/.bashrc'
alias vbrc='vim ~/.bashrc'

alias rdfind_hard='rdfind -deleteduplicates true'
alias findduplicates=rdfind
alias find_duplicates=rdfind

alias ffmpegnb='ffmpeg -hide_banner'
alias ffprobenb='ffprobe -hide_banner'
alias ffprobe_getresolution='ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0'
alias ffprobe_getduration='ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -sexagesimal'

# recursive, (case) insensitive grep
function rigrep {
	if [[ $# -ne 2 ]]; then
		echo $0: error - expected 2 arguments, found $#
		return
	fi
	FINDCMD="find -iname '*"
	FINDCMD+="$2"
	FINDCMD+="*' -print0 | "
	FINDCMD+="xargs -0 grep -i '"
	FINDCMD+="$1"
	FINDCMD+="'"
	#echo FINDCMD=$FINDCMD
	eval "$FINDCMD"
}

function rm_empty_dirs {
	find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 rmdir --ignore-fail-on-non-empty
}

# func to invoke rm, with log of removed files
function delete {
	if [[ $# -lt 1 ]]; then
		echo "error: expected one or more arguments (file names) but found none"
		return
	fi
	local DELETE_LOG=delete.log
	local DELETE_ERR=delete.err
	if [[ ! -e "$DELETE_LOG" ]]
	then
		touch "$DELETE_LOG"
	fi
	if [[ ! -w "$DELETE_LOG" ]]
	then
		echo "error: Cannot write logfile; aborting without removals."
		return
	fi
	# move the log because it mayunintentionally be among the
	# filenames given as arguments; this could be due to expansion
	# of '*' for example
	mv "$DELETE_LOG" "$TMPDIR"
	# now perform the actual removal
	rm -v "$@" 2>"$DELETE_ERR" | tee -a "$TMPDIR/$DELETE_LOG"
	if [[ -e "$DELETE_ERR" ]]
	then
		# show errors but filter out the (possible) line about the
		# logfile itself
		grep -v "rm: cannot remove '$DELETE_LOG'" "$DELETE_ERR"
		rm "$DELETE_ERR"
	fi
	# move the logfile back
	mv "$TMPDIR/$DELETE_LOG" .
	#echo "Succesful deletions logged in '$DELETE_LOG'"
}

function fgall {
	JOBNUMS=$(jobs | sed -n -e \
		'/\[\([0-9]\+\)\]/{s/.*\[\([0-9]\+\)\].*/\1/;p}')
	[[ -z "$JOBNUMS" ]] && echo "No jobs."
	for i in $JOBNUMS; do
		fg %$i
	done
}

function mcd {
	if [[ $# -ne 1 ]]; then
		echo ERROR: expected 1 argument, found $#. Add quotemarks if you want \"$@\" treated as a single argument.
		return
	fi
	if [[ -d "$1" ]]; then
		echo "Directory exists; cd'ing into."
	elif [[ -e "$1" ]]; then
		echo "ERROR: A non-directory by that name already exists; cannot mkdir."
		return
	else
		mkdir -p "$1"
		echo "Created directory '$1'."
	fi
	cd "$1"
}

function flatten_this_epub_dir {
	find -iname 'metadata.opf' -delete
	find -iname '*.jpg' -delete
	find -iname '*.txt' -delete
	find -iname '*.opf' -delete
	find -type f -exec mv -vn {} . \;
	rmdir --ignore-fail-on-non-empty */
}

function stats {
	FMT="%n (%F, %s bytes)\n  m=%y\n  c=%z\n"
	#SUB1='s/=\(....\)-\(..\)-\(..\) /=\3\/\2\/\1/ '
  SUB1='s/=....-..-.. /=/'
	SUB2='s/\.......... +..00//'
	if [[ $# -eq 0 ]]
	then
		stat --printf "$FMT" * | sed -e "$SUB1" -e "$SUB2"
	else
		stat --printf "$FMT" "$@" | sed -e "$SUB1" -e "$SUB2"
	fi
}

function vcd() {
	RANGER=$(which ranger)
	if [[ -z "$RANGER" ]]
	then
		echo "error: ranger not found"
		return
	fi
	TMPFILE="$TMPDIR/ranger.tmp"
  "$RANGER" --choosedir="$TMPFILE" --show-only-dirs
  TARGET_DIR=$(cat "$TMPFILE")
	cd "$TARGET_DIR"
	rm "$TMPFILE"
}

source ~/.dotfiles/godir/godir.bash
source ~/.dotfiles/scripts/ffcut.bash

INIT_DIR=storage/downloads
[[ -d "$INIT_DIR" ]] && cd "$INIT_DIR" 

PS1='\[\e[0;32m\]\w\[\e[0m\] \[\e[0;97m\]\n\$\[\e[0m\] '

# append certain dirs to PATH
pathdirs=(
~/.dotfiles/scripts
~/bin
)
for pathdir in "${pathdirs[@]}"
do
	if [[ -d "$pathdir" && ! "$PATH" =~ "$pathdir" ]]; then
		PATH=$PATH:"$pathdir"
	fi
done

function seen()
{
	if [[ $# -lt 1 ]]; then
		echo error: Expected 1 or more arguments, found $#
		return
	fi
	[[ ! -d "seen" ]] && mkdir -p "seen"
	mv -v -t "seen" "$@"
}

function r() {
	local size
	local sizeh
	for f in "$@"; do
		if [[ ! -f "$f" ]]; then
			echo "r: cannot remove '$f', no such file, skipping"
			continue
		fi
		size=$(stat -c "%s" "$f")
		sizeh=$(numfmt --to iec "$size")
		echo -n "size=$sizeh, "
		rm -vi "$f" 
	done
}

function safermdir()
{
	if [[ $# -ne 1 ]]; then
		echo error: Expected 1 argument, found $#
		return
	fi
	[[ ! -e "$1" ]] && echo "error: '$1' does not exist" && return
	[[ ! -d "$1" ]] && echo "error: '$1' is not a directory" && return
	du -sh "$1"
	local N=$(find "$1" -type f | wc -l)
	echo "contains $N files"
	confirm
	if [[ $? -eq 1 ]]; then
		rm -rfv "$1"
	else
		echo "Aborted, nothing removed."
	fi
}	

function confirm()
{
	read -p "Are you sure? " input
	case "$input" in
		y|Y|yes|YES ) return 1;;
	esac
	return 0
}

source ~/.dotfiles/scripts/ffreenc.bash

function extract_aac_from_mp4() {
	if [[ $# -lt 1 ]]; then
		echo error: Expected 1 or more arguments, found $#
		return
	fi
	for i in "$@"
	do
		echo "$i" 
    ffmpeg -hide_banner -loglevel error -i "$i" -c:a copy "${i%.mp4}.aac"
		if [[ $? -ne 0 ]]
		then
			echo ERROR
		else
			echo OK
		fi
  done
}

# convenient values
YOUTUBE='https://www.youtube.com/watch?v'

# show reminder file if present
[ -e ~/reminders.txt ] && cat ~/reminders.txt	
