summaryrefslogtreecommitdiff
path: root/net/pichi/files/pichi.in
blob: 31b4959da63a36ee3c2b9ec560c22fd2f46b30e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh

# PROVIDE: pichi
# REQUIRE: DAEMON
# KEYWORD: shutdown

get_pid()
{
  ps -o "pid=" -p "$(cat ${pid_file} 2>/dev/null)" 2>/dev/null
}

pichi_start()
{
  pid="$(get_pid)"
  if [ -n "${pid}" ]; then
    echo "${name} is running with pid ${pid}"
    exit 1
  fi
  if [ -z "${pichi_port}" ]; then
    echo "pichi_port is not set"
    exit 1
  fi
  "${command}" -u nobody --group daemon -d -g "${pichi_mmdb}" -p "${pichi_port}" -l "${pichi_bind}" --json "${pichi_conf}"
  if ! get_pid >/dev/null 2>&1; then
    echo "Failed to start ${name}"
    exit 1
  fi
}

# Main
. /etc/rc.subr

load_rc_config "${name}"

name="pichi"
rcvar="${name}_enable"
prefix="%%PREFIX%%"
command="${prefix}/bin/${name}"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
restart_cmd="${name}_restart"
status_cmd="${name}_status"
extra_commands="reload"
reload_cmd="${name}_reload"
pid_file="${prefix}/var/run/${name}.pid"

: ${pichi_enable:="NO"}
: ${pichi_bind:="::1"}
: ${pichi_port:="21127"}
: ${pichi_conf:="${prefix}/etc/${name}/pichi.json"}
: ${pichi_mmdb:="${prefix}/etc/${name}/geo.mmdb"}

pichi_stop()
{
  pid=$(get_pid)
  if [ -n "${pid}" ]; then
    kill ${pid}
  fi
}

pichi_restart()
{
  pichi_stop
  pichi_start
}

pichi_reload()
{
  pid=$(get_pid)
  if [ -n "${pid}" ]; then
    kill -HUP ${pid}
  fi
}

pichi_status()
{
  pid=$(get_pid)
  if [ -n "${pid}" ]; then
    echo "${name} is running with PID ${pid}"
  else
    echo "${name} is not running"
  fi
}

run_rc_command "$1"