#!/bin/bash # Time sheet creator # Stores clock in and outs to timesheet named for the ending date of the ts usage() { cat < "${TIMESHEET_DIR}/${TIMESHEET}" } getTimesheet() { # get Timesheet to use if [[ -z "$TIMESHEET" ]] ; then # no timesheet, create one createTimesheet else # timesheet found, check we are still in period period_end=$(date -d "$(echo "$TIMESHEET" | awk -F . '{print $1}') + 1 days" +%s) if [[ $(date +%s) -ge $period_end ]] ; then # past timesheet, moving to archive and creating new mv "${TIMESHEET_DIR}/${TIMESHEET}" "$ARCHIVE_DIR" createTimesheet fi fi } clockIn() { # clock in by creating env file curdate=$(date +%s) env_details=$(printf 'DATE=%s\nTIME_IN=%s\n' "$curdate" "$curdate") if [[ ! -f "$CURRENT_SESSION" ]] ; then # creating session echo "$env_details" > "$CURRENT_SESSION" else echo "Already Clocked In!" >&2 exit 1 fi } clockOut() { # clock out if [[ -f "$CURRENT_SESSION" ]] ; then # session exists, get info source "$CURRENT_SESSION" # printing to timesheet TIME_OUT=$(date +%s) TOTAL_TIME=$(($TIME_OUT - $TIME_IN + 3600 * 5)) echo "$TOTAL_TIME" # printing to timesheet printf '%s, %s, %s, %s\n' "$(date -d @$DATE "+%a %b-%d")" "$(date -d @$TIME_IN +%I:%M%P)" "$(date -d @$TIME_OUT +%I:%M%P)" "$(date -d @$TOTAL_TIME +%H:%M)" >> ${TIMESHEET_DIR}/${TIMESHEET} # cleaning up session rm ${CURRENT_SESSION} else # no session echo "Not clocked in!" >&2 exit 1 fi } visualize() { # visualize the current timesheet column -s "," -t < ${TIMESHEET_DIR}/${TIMESHEET} } totalHours() { # tally up hours for the current timesheet while read -r entry; do date=$(echo "$entry" | awk -F , '{print $1}' | tr -d ",\n") time=$(echo "$entry" | awk -F , '{print $4}' | tr -d " ,\n") if [[ "$date" != "Date" ]] ; then printf 'On %s worked %s\n' "$date" "$time" hours=$(($hours + 10#$(echo "$time" | awk -F : '{print $1}'))) mins=$(($mins + 10#$(echo "$time" | awk -F : '{print $2}'))) fi done < ${TIMESHEET_DIR}/${TIMESHEET} hours=$(($hours + $mins/60)) # overflow mins=$(($mins%60)) printf 'Worked %d hours, %d minutes\n' "$hours" "$mins" } # get the timesheet getTimesheet # parse args while [[ $# -gt 0 ]] ; do case $1 in -h | --help) usage exit 0 ;; -i | --in) clockIn ;; -o | --out) clockOut ;; -v | --visualize) visualize ;; -t | --total) totalHours ;; *) echo "Error: Unrecognized" >&2 usage exit 1 ;; esac shift done