#!/bin/sh

# Control the volume
# Depends: pamixer

help() {
	B=$(tput bold)
	U=$(tput smul)
	N=$(tput sgr0)

	cat << EOF
${B}NAME${N}
	volctl - control the volume

${B}SYNOPSIS${N}
	${B}volctl${N} [${U}OPTION${N}] [${U}COMMAND${N}] [<${U}ARG${N}>]

${B}DESCRIPTION${N}
	${B}volctl${N} uses pamixer to change the volume of the system.

	Commands can be truncated, i.e. "${B}volctl t${N}" for "${B}volctl toggle${N}"
	Arguments need to be of numeric value.

${B}OPTIONS${N}
	${B}-h${N}	Display usage message and exit.

${B}COMMANDS${N}
	${B}i${N} <${U}AMOUNT${N}>, ${B}inc${N} <${U}AMOUNT${N}>
		Increase volume by ${U}AMOUNT${N}, default of 2.

	${B}d${N} <${U}AMOUNT${N}>, ${B}dec${N} <${U}AMOUNT${N}>
		Decrease volume by ${U}AMOUNT${N}, default of 2.

	${B}s${N} <${U}LEVEL${N}>, ${B}set${N} <${U}LEVEL${N}>
		Set volume to ${U}LEVEL${N}, default of 0.

	${B}t${N}, ${B}toggle${N}
		Toggle mute.

	${B}m${N}, ${B}mute${N}
		Mute volume.

	${B}u${N}, ${B}unmute${N}
		Unmute volume.

	${B}p${N} <${U}STATE${N}>, ${B}pulsetcp${N} <${U}STATE${N}>
		Set PulseAudio TCP module, possible values: on/off, 1/0, default enable.

	${B}getv${N}, ${B}getvolume${N}
		Get volume level.

	${B}getm${N}, ${B}getmute${N}
		Get mute status.
EOF
}

# Exit if no option is provided
[ "$#" -eq 0 ] && help && exit 1

SCRIPT="$(basename "$0")"

# Option handling
while getopts ':h?' opt; do
	case $opt in
		h)
			help
			exit 0
			;;
		\?)
			echo "$SCRIPT: invalid option '$OPTARG'"
			echo "Try '$SCRIPT -h' for more information."
			exit 1
			;;
	esac
done

pulseTcp()
{
	if test "$1" = "off" || test "$1" = "0"; then
		if pactl list modules short | grep -q module-simple-protocol-tcp; then
			pactl unload-module module-simple-protocol-tcp
		fi
		exit 0
	fi

	"$0" p off

	device="$(pactl list sources short | head -n 1)"
	source="$(echo "$device" | awk '{ print $1; }')"
	format="$(echo "$device" | awk '{ print $4; }')"
	channels="$(echo "$device" | awk '{ print $5 + 0; }')"
	rate="$(echo "$device" | awk '{ print $6 + 0; }')"

	# Load PulseAudio TCP module
	pactl load-module module-simple-protocol-tcp \
		  rate="$rate" format="$format" channels="$channels" source="$source" \
		  record=true port=8000 listen=0.0.0.0 > /dev/null

	# Echo connection info
	ip="$(ip a show eth0 | awk '/inet / { print substr($2, 0, length($2) - 3) }')"
	echo "server: $ip:8000"
	echo "audio:  ${rate}Hz ${channels}ch"
}

# Command handling
shift $((OPTIND - 1))
case "$1" in
	i*)
		NUM=${2:-2}
		pamixer --increase "$NUM"
		;;
	d*)
		NUM=${2:-2}
		pamixer --decrease "$NUM"
		;;
	s*)
		NUM=${2:-0}
		pamixer --set-volume "$NUM"
		;;
	t*)
		pamixer --toggle-mute
		;;
	m*)
		pamixer --mute
		;;
	n*)
		pamixer --unmute
		;;
	p*)
		pulseTcp "$2"
		;;
	getv*)
		pamixer --get-volume
		;;
	getm*)
		pamixer --get-mute
		;;
	*)
		echo "$SCRIPT: invalid command '$1'"
		echo "Try '$SCRIPT -h' for more information."
		exit 1
		;;
esac