#!/bin/sh /etc/rc.common

START=82

USE_PROCD=1

ETESYNC_INI="/var/etc/etesync-server/etesync-server.ini"


etesync_print_uci_allow_all_ips_of() {
    local ifstat="$(ifstatus "$1")"

    for ip in $(echo "${ifstat}" | jsonfilter -e '@["ipv4-address"].*.address')
    do echo "allowed_host_${ip//[^0-9]/_} = ${ip}"
    done

    for ip in $(echo "${ifstat}" | jsonfilter -e '@["ipv6-address"].*.address')
    do echo "allowed_host_${ip//[^0-9A-Fa-f]/_} = [${ip}]"
    done

    for ip in $(echo "${ifstat}" | \
        jsonfilter -e '@["ipv6-prefix-assignment"].*["local-address"].address')
    do echo "allowed_host_${ip//[^0-9A-Fa-f]/_} = [${ip}]"
    done
}


etesync_validate_global() {
    cd /usr/share/etesync-server/ >/dev/null || return

    uci_load_validate etesync_server django "global" "$1" \
        'secret_file:file:secret.txt' \
        'static_url:string:/etesync/static' \
        'language_code:string:en-us' \
        'time_zone:string:UTC' \
        'debug:bool:false' \
        ;
}


etesync_print_global() {
    printf "\n[global]\n"

    echo "secret_file = ${secret_file}"
    echo "static_root = /www/etesync/static" #sic!
    echo "static_url = ${static_url}"
    echo "language_code = ${language_code}"
    echo "time_zone = ${time_zone}"
    echo "debug = ${debug}"
}


etesync_validate_allowed_hosts() {
    uci_load_validate etesync_server django "allowed_hosts" "$1" \
        'uci_allow_all_ips_of:network' \
        'allowed_host:host' \
        ;
}


etesync_print_allowed_hosts() {
    printf "\n[allowed_hosts]\n"

    local iface
    for iface in ${uci_allow_all_ips_of}
    do etesync_print_uci_allow_all_ips_of "${iface}"
    done

    local host
    for host in ${allowed_host}
    do echo "allowed_host_${host//[^0-9A-Za-z]/_} = ${host}"
    done
}


etesync_validate_database() {
    cd /usr/share/etesync-server/ >/dev/null || return

    uci_load_validate etesync_server django "database" "$1" \
        'engine:hostname:django.db.backends.sqlite3' \
        'name:file:db.sqlite3' \
        ;
}


etesync_print_database() {
    printf "\n[database]\n"

    echo "engine = ${engine}"
    echo "name = ${name}"
}


etesync_init() { # This must print ONLY configuration lines:
    echo "; This file is re-created from /etc/config/etesync_server "
    etesync_validate_global etesync_print_global
    etesync_validate_allowed_hosts etesync_print_allowed_hosts
    etesync_validate_database etesync_print_database
} >"${ETESYNC_INI}"


start_service() {
    mkdir -p /var/etc/etesync-server/
    etesync_init
    logger -p 'daemon.info' -t 'etesync-server_init' 'starting ...'
    ln -sf /etc/uwsgi/vassals/etesync-server.available \
        /var/etc/etesync-server/uwsgi.ini
}


stop_service() {
    rm -f /var/etc/etesync-server/uwsgi.ini "${ETESYNC_INI}"
}


reload_service() {
    etesync_init
    logger -p 'daemon.info' -t 'etesync-server_init' 'reloading ...'
    kill -SIGHUP "$(cat "/var/etc/etesync-server/master.pid")" 2>/dev/null
    #if the server is in on-demand mode, the ini files are reloaded then, too.
}


service_triggers() {
    procd_open_validate
    etesync_validate_global "$@"
    etesync_validate_allowed_hosts "$@"
    etesync_validate_database "$@"
    procd_close_validate

    config_load etesync_server
    config_list_foreach "allowed_hosts" "uci_allow_all_ips_of" \
        procd_add_reload_interface_trigger

    procd_add_reload_trigger etesync_server
}