#!/bin/bash

AVERAGEHOURRATE=630

cd ~
mkdir -p .switchwork
cd .switchwork
DATE=`date +%Y.%m.%d`

mkdir -p $DATE

TIME=`date +%T`
STIME=`date +%s`


ARG1=$1


if [ "$ARG1" == ""  ];then
	ARG1=STOP
fi

if [ "$ARG1" == "-r"  ];then #round
	SWROUND=$2
	#SWROUND=15 #default is 15min

	export SWROUND
	shift 2
	ARG1=$1
fi


if [ "$ARG1" == "-m"  ];then #MOVE
	
	read ddate ttime sstart act rest <<!!!!!!
`tail -n 1 switchwork.log`
!!!!!!

	ifs=$IFS
	IFS=":"

	read hh mm ss <<!!!!!!
$2
!!!!!!
	IFS=$ifs


	seconds=`expr $hh "*" 3600 "+" $mm "*" 60 "+" $ss`
	
	echo $seconds
	
	echo " "$STIME >> switchwork.log

	echo $DATE $TIME 0000000000 $4 $seconds >> switchwork.log

	echo $DATE $TIME $seconds $3 0000000000 >> switchwork.log
	
	echo -n $DATE $TIME $STIME $act >> switchwork.log

	exit
	
fi



if [ "$ARG1" == "-get"  ];then # GET
	while read a b c d e
	do
		if [ "$a" == "Still" ] && [ "$b" == "running:" ];then
			a=$c
			b=$d
		fi

		if [ "$a" == "$2" ];then
			echo $b
		fi 
	done<<!!!!!!
`sw -c $DATE | grep -v "^Total Work " | grep -v "^STOP " | grep -v "^================"`
!!!!!!

	exit
fi

if [ "$ARG1" == "-set"  ];then # SET

	ifs=$IFS
	IFS=":"
	read hh mm ss <<!!!!!!
$2
!!!!!!
	IFS=$ifs
	secondsToSet=`expr $hh "*" 3600 "+" $mm "*" 60 "+" $ss`

	ifs=$IFS
	IFS=":"
	read hh mm ss <<!!!!!!
`sw -get "$3"`
!!!!!!
	IFS=$ifs
	secondsActual=`expr $hh "*" 3600 "+" $mm "*" 60 "+" $ss`
	
	#echo "$secondsActual" "-" "$secondsToSet"
	

	seconds=`expr "$secondsToSet" "-" "$secondsActual" `

	#echo $seconds


		s=`expr "$seconds" "%" "60"`
		m=`expr "$seconds" "/" "60"`
		h=`expr "$m" "/" "60"`
		m=`expr "$m" "%" "60"`
		echo sw -m $h:$m:$s STOP $3
		sw -m $h:$m:$s STOP $3

	exit

fi











if [ "$ARG1" == "-d"  ];then #DUMP
	echo -n > dump.csv
	while read ddate 
	do
		echo ,, >> dump.csv
		echo $ddate,, | sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\3\.\2\.\1/g' >> dump.csv
		while read a b c d e
		do
			if [ "$a" == "Still" ] && [ "$b" == "running:" ];then
				a="Still running $c"
				b=$d
			fi
			echo ,$a,$b >> dump.csv
		done<<!!!!!!
`sw -c $ddate | grep -v "^Total Work " | grep -v "^STOP " | grep -v "^================"`
!!!!!!
	done<<!!!!!!
`ls -r| grep -v log | grep -v csv`
!!!!!!

	exit

fi


if [ "$ARG1" == "-c" ];then # CHECK
	if [ "$2" != "" ];then
		if [ "$2" != "-" ];then
			DATE=$2
		fi
	fi
	dontDisplay=$4
	if [ "$dontDisplay" = "" ];then
		dontDisplay=UplneStupidniRetezecCoNikdoNikdyNepouzijeTEDAUZNAvzdyCkY
	fi
	sum=0
	bigSum=0
	echo
	echo $DATE:
		
	while read d h start act stop 
	do

		if [ "$oldact" != "$act" ];then
			
			if [ "$sum" != "0" ];then
				
				if [ "$SWROUND" != "" ];then
					sum=$(echo "scale=0;(($sum+$SWROUND*60-60)/($SWROUND*60))*($SWROUND*60)" | bc)
				fi
			
				s=`expr "$sum" "%" "60"`
				m=`expr "$sum" "/" "60"`
				h=`expr "$m" "/" "60"`
				m=`expr "$m" "%" "60"`
				echo $oldact $h:$m:$s
			fi
			if [ "$oldact" != "STOP" ];then
				bigSum=`expr "$bigSum" "+" "$sum"`
			fi;	
			sum=0
			oldact=$act
			
		fi
		if [ "$stop" == "" ];then
			echo -n "Still running: "
			stop=$STIME
		fi
		sum=`expr "$sum" "+" "$stop" "-" "$start"`
	done<<!!!!!!
`cat switchwork.log | grep "^$DATE" | grep "$3" | grep -v "$dontDisplay" | sort -t" " -k 4`
!!!!!!
	
	if [ "$SWROUND" != "" ];then
		sum=$(echo "scale=0;(($sum+$SWROUND*60-60)/($SWROUND*60))*($SWROUND*60)" | bc)
	fi
	s=`expr "$sum" "%" "60"`
	m=`expr "$sum" "/" "60"`
	h=`expr "$m" "/" "60"`
	m=`expr "$m" "%" "60"`
	
	echo $oldact $h:$m:$s
	if [ "$oldact" != "STOP" ];then
		bigSum=`expr "$bigSum" "+" "$sum"`
	fi;	

	czk=`expr "$AVERAGEHOURRATE" "*" "$bigSum" "/" "36" `
	
	czk=`expr "$czk" "/" "100"`","`expr "$czk" "%" "100"`
	

	s=`expr "$bigSum" "%" "60"`
	m=`expr "$bigSum" "/" "60"`
	h=`expr "$m" "/" "60"`
	m=`expr "$m" "%" "60"`
	echo =================================
	echo Total Work $h:$m:$s "(~ $czk CZK)"
	
	exit
fi




if [ "$ARG1" != "-s"  ];then #START

echo " "$STIME >> switchwork.log

echo -n $DATE $TIME $STIME $ARG1""$2""$3""$4""$5""$6""$7""$8""$9 >> switchwork.log

cd $DATE

echo $DATE $TIME $STIME $ARG1""$2""$3""$4""$5""$6""$7""$8""$9 >> $ARG1""$2""$3""$4""$5""$6""$7""$8""$9

else #START

cd $DATE
bash

fi



