#!/bin/sh

# cdda2flac TARGET_FOLDER
#
# Script to rip the inserted audio compact disc (CD) to a folder of
# FLAC-compressed music files with original quality.
#
# Provide the wanted target folder as command line argument. It will be created
# during execution.
cdda2flac() {
	folder="$1"
	folder="$(realpath "${folder}")"
	echo "Saving to ${folder}"

	mkdir "${folder}"
	return_path="$(pwd)"
	cd "${folder}"

	cdparanoia -B

	find . -type f -iname '*.wav' -exec flac --delete-input-file {} \;

	cd "${return_path}"

	echo "\nDone! Files are saved to '${folder}'."
}

# cheat PROGRAM
#
# cheat gets the cheat sheet from https://cheat.sh about the given PROGRAM.
cheat() {
	curl "https://cheat.sh/${1}"
}

# radio STATION_NAME
#
# radio translates the STATION_NAME into a web radio URL via a user-provided
# TSV file and starts to play the station's audio stream with mpv.
radio() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/radio.tsv"
	data_abs="${data_repo}/${data_rel}"

	# read station from command line arguments
	station="$1"

	# set a handler for keyboard interrupts
	trap 'exit 0' 2 15

	# check dependency and warn user if necessary
	if ! command -v mpv > /dev/null 2>&1
	then
		echo 'You have to install mpv (e.g. sudo pacman -S mpv mpv-mpris)'
		exit 1
	fi

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# translate sender name to URL
	address="$(grep "${station}" "${data_abs}" | awk -F '\t' '{print $2}')"
	if [ "${address}" = "" ]
	then
		echo "Could not translate station ${station} to URL."
		exit 1
	fi

	# stream from address
	echo "Streaming ${station} from ${address}."
	mpv "${address}"
}

# showdot SOURCE [COMPILATION_ARGS]
#
# showdot displays a GraphViz source file (*.dot) with an image viewer. The
# source code is converted into a temporary image file on the fly.
showdot() {
	# process arguments
	code=$1
	shift

	file=$(mktemp)

	# compile *.dot code to *.png
	if ! dot "$@" -T png -o "${file}" "${code}"
	then
		echo "Failed to compile with GraphViz."
		exit 1
	fi

	# launch image viewer
	if ! sxiv "${file}"
	then
		echo "Failed to display generated image with sxiv."
		exit 1
	fi

	rm "${file}"
}

# tv STATION_NAME
#
# tv translates the STATION_NAME into a tv stream URL via a user-provided TSV
# file and starts to play the station's stream with mpv.
tv() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/tv.tsv"
	data_abs="${data_repo}/${data_rel}"

	# read station from command line arguments
	station="$1"

	# set a handler for keyboard interrupts
	trap 'exit 0' 2 15

	# check dependency and warn user if necessary
	if ! command -v mpv > /dev/null 2>&1
	then
		echo 'You have to install mpv (e.g. sudo pacman -S mpv mpv-mpris)'
		exit 1
	fi

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# translate sender name to URL
	address="$(grep "${station}" "${data_abs}" | awk -F '\t' '{print $2}')"
	if [ "${address}" = "" ]
	then
		echo "Could not translate station ${station} to URL."
		exit 1
	fi

	# stream from address
	echo "Streaming ${station} from ${address}."
	mpv "${address}"
}

# url
#
# url opens a menu program with URL names saved in a user-provided TSV file and
# opens the selected URL with a browser.
url() {
	# constants
	data_repo="${HOME}/.local/share/mydata"
	data_rel="tables/urls.tsv"
	data_abs="${data_repo}/${data_rel}"

	# check if data file exists
	if ! [ -f "${data_abs}" ]
	then
		echo "Data file '""${data_abs}""' not found."
		exit 1
	fi

	# select a menu program or panic
	if command -v bemenu > /dev/null 2>&1
	then
		menu='bemenu'
	elif command -v dmenu > /dev/null 2>&1
	then
		menu='dmenu'
	elif command -v sxmo_dmenu.sh > /dev/null 2>&1
	then
		menu='sxmo_dmenu.sh'
	else
		echo 'You have to install bemenu, sxmo_dmenu.sh or dmenu!'
		exit 1
	fi

	# check firefox dependency and warn user if necessary
	if ! command -v firefox > /dev/null 2>&1
	then
		echo 'You have to install firefox.'
		exit 1
	fi

	# let user select the URL
	closer="[close menu]"
	options="$(awk -F '\t' '{print $2}' < "${data_abs}" | sed "$ a ${closer}")"
	key="$(echo "${options}" | ${menu} -i -l 10)"

	# close if user selected the close entry or stopped menu with ESC
	if [ "${key}" = "${closer}" ] || [ "${key}" = "" ]
	then
		exit 0
	fi

	# search for the selected key in data file
	url=""
	while read -r line
	do
		if [ "$(echo "${line}" | awk -F '\t' '{print $2}')" = "${key}" ]
		then
			if [ "${url}" = "" ]
			then
				url="$(echo "${line}" | awk -F '\t' '{print $1}')"
			else
				# warn if multiple URLs have this key
				echo "More than one URL in data file matching selected key!" 1>&2
				break
			fi
		fi
	done < "${data_abs}"

	# open selected URL in browser
	firefox "${url}"
}