#!/bin/bash

config=/etc/rtc_plan.conf
DMI=$(which dmidecode)
DISABLE_RTC=$(which disablertc)

crontabConf=/etc/crontab
if [ ! -e $crontabConf ]; then
    touch $crontabConf
fi
chmod 0644 $crontabConf

getboard() {
    if [ ! -z "$DMI" ]; then
        $DMI | grep "XunDun" >/dev/null
        [ $? -eq 0 ] && return 0
    fi
    return 1
}

close_power_on() {
    local DATE_F=$(date -d"-10 year ago" +"%F")
    getboard
    if [ $? -eq 0 ]; then
        xunduntools WAKE "${DATE_F} 24:60:60"
    else
        [ ! -z "$DISABLE_RTC" ] && $DISABLE_RTC
        rtcwake -m disable
    fi
}

close_power_off() {
    sed -i '/poweroff/d' $crontabConf
    systemctl restart cron >/dev/null &
}

setWakeUpTime() {
    local Time=$1
    getboard
    if [ $? -eq 0 ]; then
        xunduntools WAKE "$Time"
    else
        [ ! -z "$DISABLE_RTC" ] && $DISABLE_RTC 1
        hwclock -w # write system time to rtc
        rtcwake -m no --local --date="$Time"
    fi
}

time_list_sort() {
    local time_list=($1)
    local len=${#time_list[@]}
    for ((i = 0; i < $len; i++)); do
        for ((j = i + 1; j < $len; j++)); do
            if [[ "${time_list[i]}" > "${time_list[j]}" ]]; then
                local temp=${time_list[i]}
                time_list[i]=${time_list[j]}
                time_list[j]=$temp
            fi
        done
    done
    echo ${time_list[@]}
}

parse_json_list() {
    local start_list=""
    local stop_list=""
    local list_keys=$(jq -r '.list|to_entries|map(.key)|.[]' $config)
    for item_key in $list_keys; do
        local item_action=$(jq -r ".list.${item_key}.action" $config)
        local item_timelist=$(jq -r ".list.${item_key}.timelist|.[]" $config)
        if [ "$item_action" = "start" ]; then
            start_list="$start_list $item_timelist"
        else
            stop_list="$stop_list $item_timelist"
        fi
    done
    if [ ! -z "$start_list" ]; then
        start_list=$(time_list_sort "$start_list")
        power_on_alarm "$start_list"
    else
        close_power_on
    fi
    if [ ! -z "$stop_list" ]; then
        power_off_alarm "$stop_list"
    else
        close_power_off
    fi
}

power_on_alarm() {
    local start_list=($1)
    local current_week=$(date +"%w")
    local current_time=$(date +"%w%H%M")
    local alarm_time=""
    for item_time in ${start_list[@]}; do
        [[ "$current_time" > "$item_time" ]] && continue
        [ "$current_time" = "$item_time" ] && continue
        alarm_time=$item_time
        break
    done
    [ -z "$alarm_time" ] && alarm_time=${start_list[0]}
    local week=${alarm_time:0:1}
    local hour=${alarm_time:1:2}
    local minute=${alarm_time:3}
    let days_ago=current_week-week
    [ $days_ago -gt 0 ] && let days_ago=days_ago-7
    local alarm_point="$(date -d "$days_ago days ago" +"%F") ${hour}:${minute}:00"
    setWakeUpTime "$alarm_point"
}

power_off_alarm() {
    sed -i '/poweroff/d' $crontabConf
    local stop_list=($1)
    for item_time in ${stop_list[@]}; do
        local week=${item_time:0:1}
        local hour=${item_time:1:2}
        local minute=${item_time:3}
	echo "$minute $hour * * $week $(whoami) /sbin/poweroff" >> $crontabConf
    done
    systemctl restart cron.service > /dev/null &
}

if [ ! -e $config ]; then
    close_power_on
    close_power_off
    exit 0
fi

enable=$(jq -r .enable $config)
if [ "$enable" != "1" ]; then
    close_power_on
    close_power_off
    exit 0
else
    parse_json_list
fi

exit 0
