#!/bin/bash
source "./custom/defines.sh"

cat "${TIMEDATED_CONFIG_DIR}/timesyncd.conf" | setman-backup "timedated-ntp" "timesyncd.conf"

timeZoneKV=$(timedatectl show --property=Timezone)
timeZoneVal=${timeZoneKV#*=}

ntpKV=$(timedatectl show --property=NTP)
ntpVal=${ntpKV#*=}
[[ "$ntpVal" == "yes" ]] && ntp=true || ntp=false

json=$(jq -n --arg tz ${timeZoneVal} --argjson en ${ntp} \
    '{datetime: {TimeZone: $tz}, ntp: {Enabled: $en}}')

echo ${json} > "${DYNAMIC_CONFIG_DIR}/settings.json" #this creates config json

cat "${DYNAMIC_CONFIG_DIR}/settings.json" | setman-backup "timedated-ntp" "settings.json" #copy json