#!/bin/sh /etc/rc.common
#
# Copyright (C) 2021-2022  sirpdboy  <herboy2008@gmail.com> https://github.com/sirpdboy/luci-app-lucky

# This file is part of lucky .
#
# This is free software, licensed under the Apache License, Version 2.0 .


USE_PROCD=1
START=99
STOP=15

CONF="lucky"
PROG=/usr/bin/lucky
DEFAULT_DIR='/etc/config/lucky.daji/'
SET_TZ=""

get_tz()
{
	[ -e "/etc/localtime" ] && return
	for tzfile in /etc/TZ /var/etc/TZ
	do
		[ -e "$tzfile" ] || continue
		tz="$(cat $tzfile 2>/dev/null)"
	done
	[ -z "$tz" ] && return
	SET_TZ=$tz
}

get_config() {
  config_get_bool enabled $1 enabled 1
  config_get_bool logger $1 logger 1
  config_get port $1 port 16601
  config_get SafeURL $1 safe 
  config_get configdir $1 configdir $DEFAULT_DIR
}

start_service() {
	config_load "$CONF"
	config_foreach get_config "$CONF"
	[ "x$enabled" = "x1" ] || return 1
	[ ! -d $configdir ] && mkdir -p $configdir 2>/dev/null
	[ $port != 16601 ] && $(which lucky) -setconf -key AdminWebListenPort -value $port -cd $configdir
	[ $SafeURL ] && $(which lucky) -setconf -key SafeURL -value $SafeURL -cd $configdir
	procd_open_instance
	get_tz
	[[ -z "$SET_TZ" ]] || procd_set_param env TZ="$SET_TZ"
	procd_set_param command $PROG -cd "$configdir"
	[ "x$logger" == x1 ] && procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance

}


service_triggers() {
	procd_add_reload_trigger lucky
}
