forked from jacklul/asuswrt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts-startup.sh
120 lines (95 loc) · 3.34 KB
/
scripts-startup.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# This script starts all other scripts
# Only scripts containing "start" case will be started
#
#shellcheck disable=SC2155
readonly SCRIPT_PATH="$(readlink -f "$0")"
readonly SCRIPT_NAME="$(basename "$SCRIPT_PATH" .sh)"
readonly SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
readonly SCRIPT_CONFIG="$SCRIPT_DIR/$SCRIPT_NAME.conf"
readonly SCRIPT_TAG="$(basename "$SCRIPT_PATH")"
SCRIPTS_DIR="/jffs/scripts"
CHECK_FILE="/tmp/scripts_started"
if [ -f "$SCRIPT_CONFIG" ]; then
#shellcheck disable=SC1090
. "$SCRIPT_CONFIG"
fi
scripts() {
readonly _ACTION="$1"
[ ! -d "$SCRIPTS_DIR" ] && return
for ENTRY in "$SCRIPTS_DIR"/*.sh; do
[ "$(basename "$ENTRY" .sh)" = "$SCRIPT_NAME" ] && continue
! grep -q "\"start\")" "$ENTRY" && continue
if [ -x "$ENTRY" ]; then
ENTRY="$(readlink -f "$ENTRY")"
case "$_ACTION" in
"start")
logger -s -t "$SCRIPT_TAG" "Starting $ENTRY..."
;;
"stop")
logger -s -t "$SCRIPT_TAG" "Stopping $ENTRY..."
;;
esac
/bin/sh "$ENTRY" "$_ACTION"
fi
done
}
case "$1" in
"start")
if [ ! -f "$CHECK_FILE" ]; then
logger -s -t "$SCRIPT_TAG" "Starting custom scripts ($SCRIPTS_DIR)..."
date > $CHECK_FILE
scripts start
fi
;;
"stop")
logger -s -t "$SCRIPT_TAG" "Stopping custom scripts ($SCRIPTS_DIR)..."
rm -f "$CHECK_FILE"
scripts stop
;;
"install")
mkdir -pv "$SCRIPTS_DIR"
if [ -f "/usr/sbin/helper.sh" ]; then # could also be [ "$(uname -o)" = "ASUSWRT-Merlin" ] ?
cat <<EOT
You should not be using this script on Asuswrt-Merlin!
Please start individual scripts from /jffs/scripts/services-start instead!
If you continue an entry to start this script will be added to /jffs/scripts/services-start.
EOT
#shellcheck disable=SC3045,SC2162
read -p "Continue ? [y/N] : " -n1 REPLY
echo
case $REPLY in
[Yy]*)
if [ ! -f /jffs/scripts/services-start ]; then
echo "Creating /jffs/scripts/services-start"
cat <<EOT > /jffs/scripts/services-start
#!/bin/sh
EOT
chmod 0755 /jffs/scripts/services-start
fi
if ! grep -q "$SCRIPT_PATH" /jffs/scripts/services-start; then
echo "Adding script to /jffs/scripts/services-start"
echo "$SCRIPT_PATH start & # jacklul/asuswrt-scripts" >> /jffs/scripts/services-start
else
echo "Script line already exists in /jffs/scripts/services-start"
fi
;;
esac
else
NVRAM_SCRIPT="/bin/sh $SCRIPT_PATH start"
if [ "$(nvram get script_usbmount)" != "$NVRAM_SCRIPT" ]; then
echo "Setting NVRAM variable \"script_usbmount\" to \"$NVRAM_SCRIPT\""
nvram set script_usbmount="$NVRAM_SCRIPT"
nvram commit
else
echo "NVRAM variable \"script_usbmount\" is already set to \"$NVRAM_SCRIPT\""
fi
fi
;;
*)
echo "Usage: $0 start|stop|install"
exit 1
;;
esac