Commit 8f1f8d65 authored by Dimitri Enns's avatar Dimitri Enns
Browse files

Merge branch 'develop' into 'main'

Merge it

See merge request !2
parents f4f77d1f f2e8036d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
# OfficeTime
Just a pretty trivial script to help you track whether you are one phone duty, in home office, in the office or just doing private things on your work computer / laptop.  

### CronJob
Setup this cronjob to remind you one everyday at 10:30. In case things are changing more often in your shift, just let the Cron run more often.  
Please make sure you adjust the paths to your terminal application and the path the script.
```bash
30 10 * * * env DISPLAY=:0.0 /usr/bin/terminator --title="OfficeTime" -e "/bin/bash ~/officetime.sh"
```
### Reports
The script just takes your input and writes it into a .csv file so that you have some sort of a journal / report at the end of the year.
See the `~/.work_journal/` Directory for the files.
+137 −0
Original line number Diff line number Diff line
#! /bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
APP="OfficeTime"
JOURNALDIR="$HOME/.work_journal"
HO_FILE="${JOURNALDIR}/home_office_days.csv"
OF_FILE="${JOURNALDIR}/office_days.csv"
PD_FILE="${JOURNALDIR}/phone_duty_days.csv"
SUM_FILE="${JOURNALDIR}/summary.csv"
LOCKFILE="${JOURNALDIR}/.lock.file"
T_HEADER="Datum,Uhrzeit,Arbeitsmodell"
DATE=$(date +"%d.%m.%Y")
TIME=$(date +"%H:%M")

function echox() {
	echo "${APP} - $(date) - ${1}"
}

if [[ ! $(which which) ]]; then
	echox "Which is missing. Exiting here!"
	exit 1;
fi

if [[ ! $(which whiptail) ]]; then
	echox "whiptail is missing. Exiting here!"
	exit 1;
fi


function lockfile() {
	PID="$$"
	if [[ ! -f ${LOCKFILE} ]]; then
		touch ${LOCKFILE}
		echo "${PID}" > ${LOCKFILE}
	else
		LKFILEPID="$(cat ${LOCKFILE})"
		if [[ -f ${LOCKFILE} && ! -d "/proc/${LKFILEPID}"  ]]; then
			rm -f ${LOCKFILE}
			/bin/bash $(basename $0) && exit 0
		elif [[ ${LKFILEPID} != ${PID} && -d "/proc/${LKFILEPID}" ]]; then
			echox "A process is already running. PID: ${LKFILEPID}"
			exit 0
		fi
	fi
	if [[ $1 == "clear" ]]; then
		if [[ -f ${LOCKFILE} ]]; then
			rm -f ${LOCKFILE}
		fi
	fi
}


function init()	{
	if [[ ! -d ${JOURNALDIR} ]]; then
		mkdir ${JOURNALDIR}
		sleep 0.2
		if [[ ! -d ${JOURNALDIR} ]]; then
			echox "Failed creating directory: ${JOURNALDIR}. Exiting here!"
			exit 1
		fi
	fi
	if [[ ! -f ${SUM_FILE} ]]; then
		touch ${SUM_FILE}
		if [[ ! -f ${SUM_FILE} ]]; then
			echox "Failed creating: ${SUM_FILE}. Exiting here!"
			exit 1
		fi
		echo "${T_HEADER}" > ${SUM_FILE}
	fi
	if [[ $1 == "file" ]]; then
		TYPE=$2
		if [[ ! -f ${TYPE} ]]; then
			touch ${TYPE}
			if [[ ! -f ${TYPE} ]]; then
				echox "Failed creating: ${TYPE}. Exiting here!"
				exit 1
			fi
			echo "${T_HEADER}" > ${TYPE}
		fi
	fi
}

init
lockfile

function write () {
	declare -A T_ARR
	T_ARR[HO]="HomeOffice"
	T_ARR[OF]="Office"
	T_ARR[PD]="Bereitschaft"
	T_ARR[PV]="Privat"

	init file ${2}
	if [[ -f ${2} && -f ${SUM_FILE} ]]; then
		echo "${DATE},${TIME},${T_ARR[$1]}" >> ${2}
		echo "${DATE},${TIME},${T_ARR[$1]}" >> ${SUM_FILE}
	else
		echox "Cant write to ${2}. File does not exist!"
	fi
	lockfile clear
}

CHOICES=$(whiptail --menu "Wähle dein Arbeitsmodell!" 18 100 10 \
  "1" "HomeOffice" \
  "2" "Büro" \
  "3" "Bereitschaft" \
  "4" "Privat" 3>&1 1>&2 2>&3)


if [ -z "$CHOICES" ]; then
	lockfile clear
  exit 0
else
  for CHOICE in $CHOICES; do
    case "$CHOICE" in
    "1")
			FILE=${HO_FILE}
      write HO ${FILE}
      ;;
    "2")
			FILE=${OF_FILE}
			write OF ${FILE}
      ;;
    "3")
      FILE=${PD_FILE}
			write PD ${FILE}
      ;;
    "4")
			echox "Alles klar !"
			lockfile clear
			exit 0
      ;;
    *)
      echo "$CHOICE Kann hier nicht verwendet werden!" >&2
      exit 1
      ;;
    esac
  done
fi