#! /bin/bash

if [ -f /tmp/pschedule-loading ] && [ "$2" = "-a" ]; then exit; fi #Pschedule calls this at <action> in tables, which means it would check 5 times during startup if we hadn't prevented it with the -a switch. See xml code in func_new

if [ "$1" = "-new" ]; then
	MINUTE=`cat /tmp/pschedule-cronminute`
	HOUR=`cat /tmp/pschedule-cronhour`
	DAY=`cat /tmp/pschedule-cronday`
	MONTH=`cat /tmp/pschedule-cronmonth`
	WEEKDAY=`cat /tmp/pschedule-cronweekday`
	echo "$MINUTE{ð®SSSđþ}$HOUR{ð®SSSđþ}$DAY{ð®SSSđþ}$MONTH{ð®SSSđþ}$WEEKDAY" > /tmp/pschedule-forloop
else
	echo -n > /tmp/pschedule-treeitems
	cat /var/local/cron/crontabs/$USER 2>/dev/null | grep -v "^#" | sed -e "s/ /{ð®SSSđþ}/g" | sed -e "s/*/{ð®STARđþ}/g" > /tmp/pschedule-forloop #150401
fi

N=1
for I in `cat /tmp/pschedule-forloop`; do
	I=`echo "$I" | sed -e 's/{ð®SSSđþ}/ /g'` #used later to check for advanced cron
	CRONCOMMAND=`echo "$I" | sed -e "s/{ð®STARđþ}/*/g"`
	MINUTE=`echo "$CRONCOMMAND" | cut -d " " -f 1`
	HOUR=`echo "$CRONCOMMAND" | cut -d " " -f 2`
	DAY=`echo "$CRONCOMMAND" | cut -d " " -f 3`
	MONTH=`echo "$CRONCOMMAND" | cut -d " " -f 4`
	WEEKDAY=`echo "$CRONCOMMAND" | cut -d " " -f 5`
	COMMAND=`echo "$CRONCOMMAND" | cut -d " " -f 6- | cut -d "#" -f 2-` #jake_take
	PREMONTH=""
	PREDAY=""
	PRETIME=""
	TIME_DELIMINATOR=":"
	
	#TIME
	if [ "$MINUTE" = "*" ]; then MINUTE="xx"; fi
	if [ `echo $MINUTE | wc -c` = 2 ]; then MINUTE=0"$MINUTE"; fi #place a zero in front when only 1 digit
	if [ "$HOUR" = "*" ]; then HOUR="xx"; fi
	if [ `echo $HOUR | wc -c` = 2 ]; then HOUR=0"$HOUR"; fi #place a zero in front when only 1 digit
	if [ "$HOUR" = "xx" ]; then
		if [ "$MINUTE" != "xx" ]; then
			PRETIME="$(gettext 'Every hour at') "
		fi
		if [ "$MINUTE" = "xx" ]; then
			PRETIME="$(gettext 'Every minute')"
			TIME_DELIMINATOR=""
			MINUTE=""
			HOUR=""
		fi
	fi
	if [ "$HOUR" != "xx" ] && [ "$MINUTE" = "xx" ]; then
		PRETIME="$(gettext 'Every minute during the hour') "
		TIME_DELIMINATOR=""
		MINUTE=""
	fi	
	#DAY
	if [ "$DAY" != "*" ]; then
		DAY=" $(gettext 'the') $DAY."
		PREDAY=" -"
		PREMONTH=" $(gettext 'of')"
	else
		DAY=""
		if [ "$MONTH" != "*" ] && [ "$WEEKDAY" = "*" ]; then
			PREDAY=" -"
			DAY=" $(gettext 'any day')"
			PREMONTH=" $(gettext 'in')"
		fi
	fi
	if [ "$WEEKDAY" != "*" ] && [ "$MONTH" != "*" ]; then
		PREDAY=" $(gettext 'in')"
	fi
	if [ "$WEEKDAY" != "*" ] && [ "$DAY" != "" ]; then PREDAY=", $(gettext 'if its on') "; fi
	case $WEEKDAY in
		0)		WEEKDAY=" - $(gettext 'Sundays')";;
		1)		WEEKDAY=" - $(gettext 'Mondays')";;
		2)		WEEKDAY=" - $(gettext 'Tuesdays')";;
		3)		WEEKDAY=" - $(gettext 'Wednesdays')";;
		4)		WEEKDAY=" - $(gettext 'Thursdays')";;
		5)		WEEKDAY=" - $(gettext 'Fridays')";;
		6)		WEEKDAY=" - $(gettext 'Saturdays')";;
		"*")	WEEKDAY="";;
	esac
		case "$MONTH" in
		1)		MONTH=" $(gettext 'January')";;
		2)		MONTH=" $(gettext 'February')";;
		3)		MONTH=" $(gettext 'March')";;
		4)		MONTH=" $(gettext 'April')";;
		5)		MONTH=" $(gettext 'May')";;
		6)		MONTH=" $(gettext 'June')";;
		7)		MONTH=" $(gettext 'July')";;
		8)		MONTH=" $(gettext 'August')";;
		9)		MONTH=" $(gettext 'September')";;
		10)		MONTH=" $(gettext 'October')";;
		11)		MONTH=" $(gettext 'November')";;
		12)		MONTH=" $(gettext 'December')";;
		"*")	MONTH=""
				[ "$DAY" ] && MONTH=" $(gettext 'any month')"
				;;
	esac


	#strip $COMMAND to max 25 chars
	if [ `echo "$COMMAND" | wc -c` -gt 25 ]; then
		COMMAND="`echo "$COMMAND" | cut -c 1-22`..."
	fi
	echo "$(gettext 'Next run'):     $PRETIME$HOUR$TIME_DELIMINATOR$MINUTE$WEEKDAY$PREDAY$DAY$PREMONTH$MONTH" > /tmp/pschedule-next #for new_task


	#if cron contains , / - will it be set to 'Advanced schedule'
	if [ "`echo "$I" | cut -d " " -f 1-5 | grep -E "/|,|-"`" ]; then
		echo "$N:$COMMAND | $(gettext 'Advanced schedule') | $CRONCOMMAND" >> /tmp/pschedule-treeitems # for main window #jake_take
	else
		echo "$N:$COMMAND | $PRETIME$HOUR$TIME_DELIMINATOR$MINUTE$WEEKDAY$PREDAY$DAY$PREMONTH$MONTH | $CRONCOMMAND" >> /tmp/pschedule-treeitems # for main window #jake_take
	fi
	N=$(($N+1))
done
