#!/bin/bash

[[ "$TRACE" ]] && set -x
set -eo pipefail

drills_flag_path="/var/dirty-orange/drills"

function write_log()
{
    log_file="/var/log/dirty-orange/drills.log"
    ts=$(date '+%Y-%m-%d %H:%M:%S')
    echo "$ts $@" >> "$log_file"
}

function drills()
{
    action=$1

    if [[ $action == "status" ]]; then
        if [[ -f $drills_flag_path ]]; then
            echo "drills mode"
        else
            echo "silent mode"
        fi
        exit 0
    fi
    if [[ $action == "on" ]]; then
        msg="drills turned on"
        touch $drills_flag_path
    fi
    if [[ $action == "off" ]]; then
        msg="drills turned off"
        rm -f $drills_flag_path
    fi
    echo $msg
    write_log $msg
}

function usage()
{
    echo 'drills on|off|status'
}

case "$1" in
    -h | --help)
        usage
        ;;
    on | off | status)
        drills $@
        ;;
    *)
        usage
        exit 1
        ;;
esac
