#! /bin/zsh

# simple unit-conversion util, partly made for fun, not much here so far

conversionUnits=$1
inputsToConvert=($argv[2,-1])

function cmToIn() {
	result=$(( 1 / 2.54 * $1 ))
	printf '%.1f cm => %.2f in\n' $1 $result
}

function inToCm() {
	result=$(( 2.54 * $1 ))
	printf '%.2f in => %.1f cm\n' $1 $result
}

function unsupportedUnits() {
	echo "unsupported conversion units given: $conversionUnits"
	exit 1
}

funcRef=exit # not sure what a good default function is, using exit
case $conversionUnits in
	(cm|cm-in) funcRef=cmToIn
		;;
	(in|in-cm) funcRef=inToCm
		;;
	(*) funcRef=unsupportedUnits
		;;
esac

for i in $inputsToConvert; do $funcRef $i; done
