#!/bin/bash

#Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
#
#Licensed under CLOUD LINUX LICENSE AGREEMENT
#http://cloudlinux.com/docs/LICENSE.TXT

curTime=$(date +"%k")

if [[ -z "$1" ]]; then
    echo 'Specify action (reset|restore)'
    exit 1
fi

if [[ ! -d /root/temp_folder ]]; then
    mkdir /root/temp_folder
fi

if [[ "$1" = "reset" ]]; then
    if [[ "$curTime" < 12 ]]; then
        delta=$(expr 12 - $curTime);
        date -s "$delta hours"
        echo "$delta hours ago" > /root/temp_folder/old_time
    elif [[ "$curTime" > 12 ]]; then
        delta=$(expr $curTime - 12);
        date -s "$delta hours ago"
        echo "$delta hours" > /root/temp_folder/old_time
    else
        echo 'skip';
    fi
elif [[ "$1" = "restore" ]]; then
    if [[ -f /root/temp_folder/old_time ]]; then
        date -s "$(cat /root/temp_folder/old_time)";
        rm /root/temp_folder/old_time
    else
        echo 'Not specified "old time"'
    fi
else
    echo 'Specify correct action (reset|restore)'
fi
