#!/bin/bash

SYSTEMD_SERVICE=sidfm-vm-agent.service
SYSTEMD_TIMER=sidfm-vm-agent.timer

TIMER_FILE=/etc/systemd/system/sidfm-vm-agent.timer
CRON_FILE=/etc/cron.d/sidfm-vm-agent

if [ -f $TIMER_FILE ];then
    method=systemd
elif [ -f $CRON_FILE ];then
    method=crond
else
    method=none
    echo "$0: Unknow error, system file broken."
    exit 3
fi

function _usage {
    if [ -z "$1" ];then
        echo "usage: $0 <COMMAND>"
        echo "	enable		Enable agent timer or cron."
        echo "	disable		Disable agent timer or cron."
        echo "	runon <time>	Change agent execution time to <time>. (e.g. 0:00)"
        exit 1
    fi
}

function enable_timer {
    systemctl enable $SYSTEMD_SERVICE || true
    systemctl start $SYSTEMD_TIMER || true
    systemctl enable $SYSTEMD_TIMER || true
}

function disable_timer {
    systemctl stop $SYSTEMD_TIMER || true
    systemctl disable $SYSTEMD_TIMER || true
    systemctl disable $SYSTEMD_SERVICE || true
}

function modify_timer {
    min=$1
    hour=$2
    sed -i "s/OnCalendar=\*-\*-\* .*/OnCalendar=*-*-* ${hour}:${min}:00/g" $TIMER_FILE
    systemctl daemon-reload || true
}

function enable_cron {
    if grep -q ^# $CRON_FILE;then
        sed -i 's/^#//g' $CRON_FILE
    fi
}

function disable_cron {
    if grep -q ^[0-9] $CRON_FILE;then
        sed -i 's/^/#/g' $CRON_FILE
    fi
}

function modify_cron {
    min=$1
    hour=$2
    sed -i "s/[0-9]* [0-9]* \* \* \*/${min} ${hour} * * */g" $CRON_FILE
}


cmd=$1
case $cmd in
    enable)
        if [ "$method" == "systemd" ];then
            echo "Enable: sidfm-vm-agent.timer systemd timer."
            enable_timer
        else
            echo "Enable: sidfm-vm-agent cron."
            enable_cron
        fi
    ;;
    disable)
        if [ "$method" == "systemd" ];then
            echo "Disable: sidfm-vm-agent.timer systemd timer."
            disable_timer
        else
            echo "Disable: sidfm-vm-agent cron."
            disable_cron
        fi
    ;;
    runon)
        runon=$2
        if [ -n "$runon" ];then
            hour=$(echo $runon|awk -F: '{print $1}')
            min=$(echo $runon|awk -F: '{print $2}')

            expr "$min" + 1 >/dev/null 2>&1
            if [ $? -gt 1 ];then
                echo "$0: Invalid time format."
                exit 2
            fi
            expr "$hour" + 1 >/dev/null 2>&1
            if [ $? -gt 1 ];then
                echo "$0: Invalid time format."
                exit 2
            fi
            if (( 10#$min < 0 || 10#$min > 59 ));then
                echo "$0: Invalid time format in minute. 00-59"
                exit 2
            fi
            if (( 10#$hour < 0 || 10#$hour > 23 ));then
                echo "$0: Invalid time format in hour. 00-23"
                exit 2
            fi

            if [ "$method" == "systemd" ];then
                echo "Set time: $runon in sidfm-vm-agent.timer systemd timer."
                modify_timer $min $hour
            else
                echo "Set time: $runon in sidfm-vm-agent cron."
                modify_cron $min $hour
            fi
        else
            _usage
        fi
    ;;
    *)
        _usage
    ;;
esac

exit 0
