#! /bin/bash
#Pschedule
#Copyright 2007
#Sigmund Berglund

#------------------------------
#Pschedule is released under the GNU General Public License (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.
    
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
#See the GNU General Public License homepage for more details. <http://www.gnu.org/licenses/>.
#------------------------------

#Thanks to HairyWill for advanced bash help.

#Translated for Russian by Pro.


VERSION="0.5"
export PROGPATH="/usr/local/pschedule"

#include CROND in new line in .xinitrc if not there
if [ -z `cat $HOME/.xinitrc | grep "crond"` ] > /dev/null 2>&1; then
	LINE=`grep -n 'exec $CURRENTWM' $HOME/.xinitrc | awk -F : '{print $1}' | head -1`
	cp $HOME/.xinitrc $HOME/.xinitrc.bak
	sed '1,'`expr "$LINE" "-" 1`'!d' $HOME/.xinitrc.bak > $HOME/.xinitrc
	echo "crond" >> $HOME/.xinitrc
	sed "$LINE"',$!d' $HOME/.xinitrc.bak >> $HOME/.xinitrc
fi
#---
killall -q crond; crond #restart cron
#Did these really happen. Do user has permission.
TMP=`ps -e`; TMP=`echo "$TMP" | grep " cron"`
if [ -z "$TMP" ]; then #cron is not running
	FRAME="ОШИБКА"; TXT2="Сервис Cron не запущен. Вам требуется запустить Cron от root"; IMG="dialog-error"
	. "$PROGPATH"/box_ok
fi

#build treeitems for 'Add task' --- better have the delay at startup
COUNT=0; ITEMS_HOUR="$ITEMS_HOUR<item>любой</item>"
while [ $COUNT -lt 24 ]; do
	if [ $COUNT -lt 10 ]; then COUNT=0"$COUNT"; fi #always 02 digits
	export ITEMS_HOUR="$ITEMS_HOUR<item>$COUNT</item>"
	COUNT=`expr $COUNT + 1`
done
COUNT=0; ITEMS_MINUTE="$ITEMS_MINUTE<item>любая</item>"
while [ $COUNT -lt 60 ]; do
	if [ $COUNT -lt 10 ]; then COUNT=0"$COUNT"; fi #always 02 digits
	export ITEMS_MINUTE="$ITEMS_MINUTE<item>$COUNT</item>"
	COUNT=`expr $COUNT + 1`
done
COUNT=1; ITEMS_DAY="$ITEMS_DAY<item>любое</item>"
while [ $COUNT -lt 32 ]; do
	export ITEMS_DAY="$ITEMS_DAY<item>$COUNT.</item>"
	COUNT=`expr $COUNT + 1`
done
export ITEMS_WEEKDAY="$ITEMS_WEEKDAY
<item>любой</item>
<item>Понедельник</item>
<item>Вторник</item>
<item>Среда</item>
<item>Четверг</item>
<item>Пятница</item>
<item>Суббота</item>
<item>Воскресенье</item>"
export ITEMS_MONTH="$ITEMS_MONTH
<item>любой</item>
<item>Январь</item>
<item>Февраль</item>
<item>Март</item>
<item>Апрель</item>
<item>Май</item>
<item>Июнь</item>
<item>Июль</item>
<item>Август</item>
<item>Сентябрь</item>
<item>Октябрь</item>
<item>Ноябрь</item>
<item>Декабрь</item>"
#---


. $PROGPATH/func_cron_to_human
export pschedule="
<window title=\"Pschedule $VERSION - планировщик заданий\">
<vbox>
 <hbox>
  <tree rules_hint=\"true\" exported_column=\"2\">
   <width>500</width><height>200</height>
   <label>\"Задание|Когда выполняется|Команда выполнения\"</label>
   <variable>TREE</variable>
   <input>cat /tmp/pschedule-treeitems</input>
   <action signal=\"button-release-event\">enable:BUTTON_DEL</action>
   <action signal=\"button-release-event\">enable:BUTTON_EDIT</action>
   <action signal=\"button-release-event\">enable:BUTTON_RUN</action>
  </tree>
  <vbox>
   <button>
    <variable>BUTTON_ADD</variable>
    <label>Добавить задание</label>
    <input file stock=\"gtk-add\"></input>
    <action>. $PROGPATH/func_new</action>
    <action>. $PROGPATH/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <button>
    <variable>BUTTON_EDIT</variable>
    <label>\"      Редактировать\"</label>
    <input file stock=\"gtk-edit\"></input>
    <visible>disabled</visible>
    <action>. $PROGPATH/func_new -edit</action>
    <action>. $PROGPATH/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <button>
    <variable>BUTTON_DEL</variable>
    <label>Удалить</label>
    <input file stock=\"gtk-delete\"></input>
    <visible>disabled</visible>
    <action>. $PROGPATH/func_delete</action>
    <action>. $PROGPATH/func_cron_to_human</action>
    <action>refresh:TREE</action>
   </button>
   <button>
    <variable>BUTTON_RUN</variable>
    <label>\"     Запустить\"</label>
    <input file stock=\"gtk-execute\"></input>
    <visible>disabled</visible>
    <action>. $PROGPATH/func_run</action>
   </button>
   <text><label>\"\"</label></text>
   <text><label>\"\"</label></text>
   <button><input file stock=\"gtk-quit\"></input><label>\"     Выход\"</label><action type=\"exit\">EXIT</action></button>
  </vbox>
 </hbox>
</vbox>
</window>"
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog3 --program=pschedule --center); do
	eval $STATEMENTS
done
IFS=$I
case $EXIT in
	*)	rm /tmp/pschedule*
		exit;;
esac
