#!/bin/bash

NumberToCard () {
	##Rank=$(qalc -t "mod($1,13)");
	##Suit=$(qalc -t "($1 - $Rank)/13");
	if [[ $1 -eq 52 ]]
	then
		echo "🙂";
	elif [[ $1 -eq 53 ]]
	then 
		echo "🙃";
	else
		let Rank=$1%13;
		let Suit=($1 - $Rank)/13;

		case $Rank in

		0)
			Rank="A";
			;;
		10)
			Rank="J";
			;;
		11)
			Rank="Q";
			;;
		12)
			Rank="K";
			;;
		*)
			let Rank=1+$Rank;
			;;
		esac

		case $Suit in
		0)
			Suit="♣";
			;;
		1)
			Suit="♡";
			;;
		2)
			Suit="♠";
			;;
		3)
			Suit="♢";
			;;
		esac

		##echo "Rank: $Rank Suit: $Suit;"
		echo "$Rank$Suit";
	fi
	
}

case $1 in
	"")
		echo -e "card-calendar.sh accepts an argument: all, [today], number, magic\n"
		echo -n "The card of the current week is $(date +%V) = "
		NumberToCard $(date +%V)
		;;

	"all")
		# Print all the weeks;
		# The ISO Week Number goes 1 to 53.
		for i in {0..53};
			do 
			echo -n "$i=";
			NumberToCard $i;
		done;
	echo "printed all weeks" 
	;;

	"today") 
		echo -e -n "Today's week is $(date +%V) = $(NumberToCard $(date +%V)).\n";
		;;

	"number")
		echo -e -n "The magic card of week $2 is $(NumberToCard $2).\n";
		;;

	"magic")
		echo -e -n "The magic card is $(NumberToCard $(date +%V)).\n";
		;;
esac


