#!/bin/bash
#
# Alarm clock for PCLinuxOS
#
# Don't miss important times and events. Turn your computer 
# into the perfect wake up system. Set the alarm and get the 
# Pizza out of the oven in perfect time.
#
# Author: D.M-Wilhelm (Leiche)
# Email: meisssw01 at gmail.com
# Licence: GPL
# First build: May Wed 11 2011
# Last build: Jul Sun 10 2011
# fixed icon display in systray, move zenity,
# based now on yad.
#
Encoding=UTF-8
#########################################################
# i18n - Internationalization - Internationalisierung   #
#########################################################
export TEXTDOMAIN=alert_clock
export TEXTDOMAINDIR="/usr/share/locale"
#########################################################
# define some variables - Definierung einiger Variablen #
#########################################################
TITLE=alert_clock
VERSION=0.39
ICON=/usr/share/icons/wecker.png
SETTIME=`cat $HOME/.config/alert-clock/alarm | grep settime | cut -d "-" -f2 |sed -e s/://g`
SEETIME=`cat $HOME/.config/alert-clock/alarm | grep settime | cut -d "-" -f2`
ALTIME=`date +%H%M`

if [ -d ~/.config/alert-clock ]; then
	echo "directory is set"
	else 
	mkdir $HOME/.config/alert-clock
fi
if [ `ps -e | grep -c alert_clock`  -gt 2 ] ; then
		if [ `cat $HOME/.config/alert-clock/alarm | grep -c settime` -gt 0 ] ; then
			yad --image=$ICON \
	--title=$TITLE --timeout 30 --window-icon=$ICON \
	--text=$"   		  <b>- Alert-Clock are set!-</b> \n
	<b>You will be alert at `cat $HOME/.config/alert-clock/alarm | grep settime | cut -d "-" -f2` </b> \n
	Do you want to cancel the alert?"
			if [ $? = "0" ]; then 
				killall alert_clock
				SET=$(yad --entry --entry-text="00:29" --title="alert_clock" \
						--window-icon="/usr/share/icons/wecker.png" \
						--image="/usr/share/icons/wecker.png" \
						--text $"Enter Time to wake up! \
						\nTime must enter as sample <i>08:30</i> \
						\nActually time is set of <b>$SEETIME</b> \
						\n<b>Notice:</b> to abort click on cancle!")
		if [ $? = "0" ]; then 
			echo settime-$SET > $HOME/.config/alert-clock/alarm
			
			killall alert_clock
			exec alert_clock
		fi
				else
				exit 0
			fi	
		else
	yad --image=$ICON \
	--title=$TITLE --timeout 30 --window-icon=$ICON \
	--text=$"   		  <b>- Alert-Clock are running!-</b> \n
	<b>You will be alert at `cat ~/.config/alert-clock/timestamp`</b> \n
	Do you want to cancel the alert?"
			if [ $? = "0" ]; then 
				killall alert_clock
				else
				exit 0
			fi	
	fi
fi		
#####################
# about - Über      #
#####################
function About () 
{ 
	yad --title=$"About" \
	--window-icon="/usr/share/icons/wecker.png" \
	--text $" <b>Alarm clock</b> for <b>PCLinuxOS</b>

 Don't miss important times and events. Turn your computer 
 into the perfect wake up system. Set the alarm and get the 
 Pizza out of the oven in perfect time.
 Or switch it to alarm clock, and set a time to wake up!

 <b>Author:</b> D.M-Wilhelm
 <b>Version:</b> 0.39
 <b>Licence:</b> GPL
 <b>Copyright</b> 2011-2025
 
 <b>Thanks goes to:</b> Neal Brooks for testing and suggestion."
		
} 
export -f About	
########################
# exit - beenden       #
########################
function Exit () 
{ 
	yad --title="alert_clock" \
	--window-icon="/usr/share/icons/wecker.png" \
	--text $"Sure to abort alarm clock?"
	if [ $? = "0" ]; then 
		killall alert_clock
	fi
} 
export -f Exit
#########################################################
# check if choose alarm clock - Checken ob Weckmodus    #
# gesetzt ist.                                          #
#########################################################
if [ -f $HOME/.config//alert-clock/alarm ]; then
#########################################################
# going in alarm clock modus - gehe in Wecker Modus     #
#########################################################

	function Switch ()
	{
		yad --text $"You want switch to alert clock?"\
		--title="alert_clock" --window-icon="/usr/share/icons/wecker.png"
		if [ $? = 0 ]; then
			rm -rf $HOME/.config/alert-clock/alarm
			killall alert_clock
			exec alert_clock
		fi		
	}
	export -f Switch
##########################################
# Enter the time - Gib eine Uhrzeit an   #
##########################################
	function Settime () 
	{ 
		SEETIME=`cat $HOME/.config/alert-clock/alarm | grep settime | cut -d "-" -f2`
		SET=$(yad --entry --entry-text="00:29" --title="alert_clock" \
		--window-icon="/usr/share/icons/wecker.png" \
		--image="/usr/share/icons/wecker.png" \
		--text $"Enter Time to wake up! \
				\nTime must enter as sample <i>08:30</i> \
				\nActually time is set of <b>$SEETIME</b> \
				\n<b>Notice:</b> to abort click on cancle!")
		if [ $? = "0" ]; then 
			echo settime-$SET > $HOME/.config/alert-clock/alarm
			
			killall alert_clock
			exec alert_clock
		fi
	} 
	export -f Settime
#####################
#progress - Prozess #
#####################
	exec 3> >(yad --notification --image=$ICON --listen --icon-size="128" \
	--command='yad --title '"$TITLE"' \
	--window-icon='$ICON' --button="gtk-close:0" \
	--width=200 --form --field '$"Switch:btn"' \
	--field '$"Settime:btn"' --field '$"About:btn"' \
	--field '$"Exit:btn"' \
	"sh -c Switch" "sh -c Settime" "sh -c About" "sh -c Exit"' \ 
	)
	echo tooltip: $"Alarm clock will wake you up at $SEETIME!" >&3
	while [ $SETTIME -ne $ALTIME ]
	do
	sleep 3
	ALTIME=`date +%H%M`
	done
	exec 3>&-
######################################
#check wich sound - auf Audio prüfen #
######################################
	if [ -f $HOME/.config/alert-clock/alert ]; then
		SOUND="$HOME/.config/alert-clock/alert"
		else
		SOUND='/usr/share/alert_clock/alarm.ogg'
	fi
###############################
#alert output - Alarm Ausgabe #
###############################
	(mplayer -loop 0 "$SOUND") | yad --title $"$TITLE"" $VERSION" \
     --button="gtk-ok:0" \
     --width 300 --image=$ICON \
     --window-icon=$ICON \
     --text=$"<b>Time to wake up!!</b>"
    yad --title $"$TITLE"" $VERSION" \
     --width 300 --image=$ICON \
     --window-icon=$ICON \
     --text=$"<b>Should start alert clock in 60 seconds new?</b>"
     if [ $? = 0 ]; then
		sleep 60
		exec alert_clock
	else
		exit
	fi	

else

#####################################
#question alert clock - Frage Alarm #
#####################################
	function menu 
	{
	COUNTDOWN=$(yad --entry --text $"Enter minutes...!" \
					--title="$TITLE"" $VERSION" --window-icon=$ICON \
					--image=$ICON \
					--button=$"Switch:4" \
					--button=$"Change:2" \
					--button=$"Test:3" \
					--button="gtk-ok:0" \
					--button="gtk-close:1" \
				)
	ret=$?

	[[ $ret -eq 1 ]] && exit 0
##############################
#change sound - Sound ändern #
##############################
	if [[ $ret -eq 2 ]]; then
		CHANGE=$(yad --title="$TITLE"" $VERSION" --window-icon=$ICON \
				--file --width=600 --height=500 \
				--text=$"<b>Choose your own audio file as alert!</b>
________________________________________________")
		if [ -z "$CHANGE" ];then 
			exec alert_clock
			exit 0
		else
			rm -rf $HOME/.config/alert-clock/alert sleep 1
			ln -s "$CHANGE" $HOME/.config/alert-clock/alert
			yad --title $"$TITLE"" $VERSION" \
				--button="gtk-ok:0" \
				--width 300 \
				--window-icon=$ICON \
				--text=$"Your own sound is set!!"
		fi
	menu		
	fi
############################
#Test sound - Klang testen #
############################
	if [[ $ret -eq 3 ]]; then
		if [ -f $HOME/.config/alert-clock/alert ]; then
			SOUND="$HOME/.config/alert-clock/alert"
		else
			SOUND='/usr/share/alert_clock/alarm.ogg'
		fi
		mplayer "$SOUND" | yad --title $"$TITLE"" $VERSION" \
						   --button="gtk-ok:0" \
						   --width 300 \
						   --window-icon=$ICON \
						   --text=$"Exit sound test!!"
		killall mplayer
		menu                 
	fi
###########################################
# switch to alarm - umschalten auf Wecker #
###########################################
	if [[ $ret -eq 4 ]]; then
		yad --text $"You want switch to alarm clock?"\
			--title="$TITLE"" $VERSION" --window-icon=$ICON
		if [ $? = 0 ]; then
			echo settime-00:29 > $HOME/.config/alert-clock/alarm
			exec alert_clock
			exit 0
		fi
	fi		
	}
	menu
#####################
#progress - Prozess #
#####################
	if [ "$COUNTDOWN" = "" ];then
		exit
	else
		echo you enter "$COUNTDOWN" minutes
		TIMER=$(echo $(($COUNTDOWN*60)))
		TASK1=$(date +%T -s "+$TIMER seconds" 2>/dev/null)
		echo $TASK1 > ~/.config/alert-clock/timestamp
		exec 3> >(yad --notification --image=$ICON --listen --icon-size="128" \
		--command='yad --title '"$TITLE"' \
		--window-icon='$ICON' --button="gtk-close:0" \
		--width=200 --form --field '$"About:btn"' --field '$"Exit:btn"' \
		"sh -c About" "sh -c Exit"' \ 
		)
		echo tooltip:$"Alarm clock was set to $COUNTDOWN minutes and notifiers at $TASK1!" >&3
		while [ $TIMER -ge 1 ]
		do
		sleep 1
		TIMER=$[$TIMER-1]
		done
		exec 3>&-
######################################
#check wich sound - auf Audio prüfen #
######################################
		if [ -f $HOME/.config/alert-clock/alert ]; then
			SOUND="$HOME/.config/alert-clock/alert"
		else
			SOUND='/usr/share/alert_clock/alarm.ogg'
		fi
###############################
#alert output - Alarm Ausgabe #
###############################
		(mplayer -loop 0 "$SOUND") | yad --title $"$TITLE"" $VERSION" \
										 --button="gtk-ok:0" \
									     --width 300 --image=$ICON \
									     --window-icon=$ICON \
									     --text=$"<b>Time is over!!</b>"
		exit;
	fi
fi
exit
