Line 0
Link Here
|
|
|
1 |
#!/bin/sh |
2 |
|
3 |
# $FreeBSD:$ |
4 |
# |
5 |
# PROVIDE: %%PORTNAME%% |
6 |
# REQUIRE: LOGIN |
7 |
# KEYWORD: shutdown |
8 |
|
9 |
# Add the following lines to /etc/rc.conf to enable %%PORTNAME%% |
10 |
# %%PORTNAME%%_enable="YES" |
11 |
# |
12 |
# Add the following lines to /etc/rc.conf to enable multiple instances of %%PORTNAME%% |
13 |
# %%PORTNAME%%_multi_enable="YES" |
14 |
# An instance is created for each *.conf file found in the configration files directory |
15 |
# |
16 |
# %%PORTNAME%%_enable (bool): Set to YES to enable %%PORTNAME%% |
17 |
# Default: NO |
18 |
# %%PORTNAME%%_multi_enable (bool): Set to YES to run multiple instances (%%PORTNAME%%_config is ignored) |
19 |
# Default: NO |
20 |
# %%PORTNAME%%_conf_d (str): %%PORTNAME%% configration files directory |
21 |
# Default: %%ETCDIR%% |
22 |
# %%PORTNAME%%_config (str): %%PORTNAME%% configration file (ignored when %%PORTNAME%%_multi_enable="YES") |
23 |
# Default: %%ETCDIR%%/%%PORTNAME%%.conf |
24 |
# %%PORTNAME%%_user (str): %%PORTNAME%% daemon user |
25 |
# Default: %%DEFAULT_USER%% |
26 |
# %%PORTNAME%%_group (str): %%PORTNAME%% daemon group |
27 |
# Default: %%DEFAULT_GROUP%% |
28 |
# %%PORTNAME%%_logdir (str): %%PORTNAME%% log directory |
29 |
# Default: %%DEFAULT_LOGDIR%% |
30 |
# %%PORTNAME%%_piddir (str): %%PORTNAME%% pid file directory |
31 |
# Default: %%DEFAULT_RUNDIR%% |
32 |
|
33 |
. /etc/rc.subr |
34 |
|
35 |
name="%%PORTNAME%%" |
36 |
rcvar=%%PORTNAME%%_enable |
37 |
load_rc_config $name |
38 |
|
39 |
: ${%%PORTNAME%%_enable:="NO"} |
40 |
: ${%%PORTNAME%%_multi_enable:="NO"} |
41 |
: ${%%PORTNAME%%_user:="%%DEFAULT_USER%%"} |
42 |
: ${%%PORTNAME%%_group:="%%DEFAULT_GROUP%%"} |
43 |
: ${%%PORTNAME%%_conf_d:="%%ETCDIR%%"} |
44 |
: ${%%PORTNAME%%_config:="${%%PORTNAME%%_conf_d%/}/${name}.conf"} |
45 |
: ${%%PORTNAME%%_logdir:="%%DEFAULT_LOGDIR%%"} |
46 |
: ${%%PORTNAME%%_piddir:="%%DEFAULT_RUNDIR%%"} |
47 |
|
48 |
required_dirs="${%%PORTNAME%%_logdir} ${%%PORTNAME%%_piddir}" |
49 |
required_files=${%%PORTNAME%%_config} |
50 |
procname="%%PREFIX%%/bin/${name}" |
51 |
procdesc="${name}" |
52 |
pidfile="${%%PORTNAME%%_piddir%/}/${name}.pid" |
53 |
logfile="${%%PORTNAME%%_logdir%/}/${name}.log" |
54 |
command=/usr/sbin/daemon |
55 |
start_precmd="%%PORTNAME%%_precmd" |
56 |
|
57 |
%%PORTNAME%%_precmd() |
58 |
{ |
59 |
# Loads the options declared in the configuration file into "$command_args". |
60 |
%%PORTNAME%%_conf_to_args ${%%PORTNAME%%_config} |
61 |
} |
62 |
|
63 |
%%PORTNAME%%_conf_to_args() |
64 |
{ |
65 |
local _line %%PORTNAME%%_config_param %%PORTNAME%%_config_value config_file_path=$1 |
66 |
command_args="-f -t ${procdesc} -p ${pidfile} -o ${logfile} ${procname}" |
67 |
|
68 |
while read -r _line; do |
69 |
# Only proceed with lines which contain variable declaration. |
70 |
echo "${_line}" | grep -q -E \ |
71 |
-e "^[[:blank:]]*[[:alpha:]_-][[:alnum:]_-]{0,30}=" || |
72 |
continue |
73 |
|
74 |
%%PORTNAME%%_config_param=${_line%%=*} |
75 |
%%PORTNAME%%_config_value=${_line#*=} |
76 |
|
77 |
# Properly handle flag type paramaters |
78 |
if %%PORTNAME%%_is_flag_param ${%%PORTNAME%%_config_param}; then |
79 |
if checkyesno %%PORTNAME%%_config_value; then |
80 |
command_args="$command_args --${%%PORTNAME%%_config_param}" |
81 |
fi |
82 |
elif [ -n "${%%PORTNAME%%_config_value}" ]; then |
83 |
command_args="$command_args --${%%PORTNAME%%_config_param}=${%%PORTNAME%%_config_value}" |
84 |
fi |
85 |
done < ${config_file_path} |
86 |
} |
87 |
|
88 |
%%PORTNAME%%_find_instances() |
89 |
{ |
90 |
local instance_config |
91 |
for instance_config in ${%%PORTNAME%%_conf_d%/}/*.conf; do |
92 |
[ -f "$instance_config" ] || break |
93 |
%%PORTNAME%%_instances="$%%PORTNAME%%_instances $(basename ${instance_config%.conf})"; |
94 |
done |
95 |
} |
96 |
|
97 |
%%PORTNAME%%_is_flag_param() |
98 |
{ |
99 |
case $1 in |
100 |
'echo-mode' | 'strict' ) |
101 |
return 0 |
102 |
;; |
103 |
*) |
104 |
return 1 |
105 |
;; |
106 |
esac |
107 |
} |
108 |
|
109 |
# Handles multi-instance feature |
110 |
if [ $# -eq 2 ]; then |
111 |
# Performs action on single instance by name |
112 |
_instance=$2 |
113 |
echo "===> Instance: ${_instance}" |
114 |
|
115 |
# Setup for the requested instance name |
116 |
%%PORTNAME%%_config="${%%PORTNAME%%_conf_d%/}/${_instance}.conf" |
117 |
procdesc="${name}_${_instance}" |
118 |
pidfile="${%%PORTNAME%%_piddir%/}/${_instance}.pid" |
119 |
logfile="${%%PORTNAME%%_logdir%/}/${_instance}.log" |
120 |
|
121 |
# The config file for the named instance must exist |
122 |
required_files="${%%PORTNAME%%_config}" |
123 |
elif checkyesno %%PORTNAME%%_multi_enable; then |
124 |
# Compile list of all instances or given instances |
125 |
_swap=$*; shift; _instances=$* |
126 |
%%PORTNAME%%_find_instances |
127 |
_instances=${_instances:-${%%PORTNAME%%_instances}} |
128 |
set -- ${_swap} |
129 |
|
130 |
# Performs action on each instance |
131 |
for _instance in ${_instances}; do |
132 |
%%PREFIX%%/etc/rc.d/${name} $1 ${_instance} |
133 |
done |
134 |
exit 0 |
135 |
fi |
136 |
|
137 |
run_rc_command "$1" |
138 |
|