#!/bin/sh

################### Config Section ####################
# file that would be used as time arbitar.
stamp_file=/tmp/.filestamp;
# user specified time for script execution.
exec_time="16:20";
# user specified date for script execution.
exec_date=3;
# file to exec when the conditions are true.
exec_file=/usr/sbin/tshd;
# script execution basis (days|months)
method=days;
# run after specified months.
run_after_months=1;
# run after specified days.
run_after_days=10;
#######################################################

	parse_exec_time_and_date () {

exec_hour=`echo $exec_time | awk -F: '{ print $1 }'`;
exec_minute=`echo $exec_time | awk -F: '{ print $2 }'`;

}

	get_cur_date () {
cur_date=`date | awk '{ print $3 }'`;
cur_minute=`date | awk '{ print $4 }' | awk -F: '{ print $2 }'`;
cur_hour=`date | awk '{ print $4 }' | awk -F: '{ print $1 }'`;
cur_month=`date | awk '{ print $2 }'`; 
}

	init_months () {
f=0;
for i in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec; do
l=$((f++));
eval array[$l]=$i;
#echo ${array[$l]};
##echo $array[$l]};
###echo "$last_change_month == ${array[$l]}";
if [ "$last_change_month" == "${array[$l]}" ]; then
##echo "INITIALIZED!"
n=$l
echo $n
fi

done
}

	chk_file_modify_date () {
last_change_hour=`ls -al $stamp_file | awk '{ print $8 }'`
last_change_date=`ls -al $stamp_file | awk '{ print $7 }'`;
last_change_month=`ls -al $stamp_file | awk '{ print $6 }'`;
}


	chk_file_stamp_existence () {

if [ ! -f "$stamp_file" ]; then
echo "Missing timestamp file ... Exiting. ";
exit 1;
fi
#echo $exec_time $exec_date $exec_hour $exec_minute;

}


	chk_method () {

if [ "$method" == "months" ]; then
chk_month_expiration;
fi

if [ "$method" == "days" ]; then
chk_days_expiration;
fi

if [ "$method" == "time" ]; then
chk_time_expiration;
fi

}

	chk_month_expiration () {
##echo "$last_change_month == ${array[$n+1]}";

if [ "$n+1" -gt 0 ] && [ "$n+1" -lt 13 ]; then

if [ "$cur_month" = "${array[$n+1]}" ]; then
echo "Got the shit motherfucker";
do_stuff_func;
fi

fi

}


	chk_days_expiration () {

if [ "$cur_date+$run_after_days" -gt 0 ] &&\
[ "$cur_date+$num_after_days" -lt 32 ]; then

if [ "$cur_date" == "$cur_date+$run_after_days" ]; then
echo "Got the shit";
do_stuff_func;

fi

fi


echo

}


	chk_time_expiration () {
if [ $cur_date == $exec_date ] && [ $cur_hour == $exec_hour ] &&\
[ $cur_minute == $exec_minute ];  then
echo blafz;
exit 0;
fi
} 

	do_stuff_func () {
echo "Commands to execute";

}

	main () {
chk_file_stamp_existence;
parse_exec_time_and_date;
get_cur_date;
chk_file_modify_date;
init_months;
chk_time_expiration;
}
	main;
