#!/bin/bash

# This to create .dat files from our fortunes

case $1 in

	clean)

		rm -fv *.dat
		rm -fv prolinux
		rm -fv *~

	;;

	help | "")

		echo -e "Fortunes::ProLinux!\
		\n\n'mkdat clean' to clean up\
		\n'mkdat cookies' to build\
		\n'mkdat install' to install\n"

	;;

	cookies | build)

		x=0

		for GSCHMARRI in $(ls ????.??); do 
			y=$(sudo strfile $GSCHMARRI $GSCHMARRI.dat | grep There | gawk '{print $3}')
			echo -ne "$GSCHMARRI  contains:  $y\tcookies\n"
			x=$(($x+$y))
		done

		rm -f prolinux

		for GSCHMARRI in $(ls ????.??); do
			cat $GSCHMARRI >> prolinux
		done

		strfile prolinux prolinux.dat >/dev/null

		echo -e "\noverall  cookies:   $x"

	;;

	install)

		if [[ $EUID != 0 ]]; then
			echo "not running as root, exiting"
			exit 0
		fi

		echo -e "Choose destination:\
		\n\nA /usr/share/games/fortunes/\
		\nB /usr/share/fortunes/\
		\nC custom directory\
		\n\nA B or C?"

		read DESTINATION

		case $DESTINATION in

			A)

				mkdir -p /usr/share/games/fortunes/
				cp -v prolinux{,.dat} /usr/share/games/fortunes/

			;;

			B)

				mkdir -p /usr/share/fortunes/
				cp -v prolinux{,.dat} /usr/share/fortunes/

			;;

			C)

				echo -e "\nInsert Path:"
				read XPATH

				mkdir -p $XPATH
				cp -v prolinux{,.dat} $XPATH

			;;

			a | b | c)

				echo -e "\nGroßbuchstaben wurden fei noch nicht abgeschafft!\n"
				ret=1
			;;

		esac

		[[ $ret != 1 ]] &&
			{ echo -e "\nTry out the cookies: 'fortune prolinux'\n" ; }
	
	;;
esac
