forked from zippyy/GL.iNET-Toggle-Switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
143 lines (125 loc) · 3.63 KB
/
install.sh
File metadata and controls
143 lines (125 loc) · 3.63 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
set -eu
SCRIPT_NAME="wds-router-toggle"
BASE_DIR="/root/wds-router-toggle"
SWITCH_SCRIPT="/etc/gl-switch.d/${SCRIPT_NAME}.sh"
SWITCH_CONFIG="/etc/config/switch-button"
SYSUPGRADE_CONF="/etc/sysupgrade.conf"
ROUTER_PROFILE="${BASE_DIR}/router.tar.gz"
WDS_PROFILE="${BASE_DIR}/wds.tar.gz"
need_root() {
[ "$(id -u)" -eq 0 ] || { echo "Run this installer as root." >&2; exit 1; }
}
say() {
printf '%s\n' "$*"
}
ask() {
prompt="$1"
default="${2:-}"
if [ -n "$default" ]; then
printf "%s [%s]: " "$prompt" "$default" >&2
else
printf "%s: " "$prompt" >&2
fi
IFS= read -r answer || true
if [ -z "$answer" ]; then
answer="$default"
fi
printf '%s' "$answer"
}
confirm() {
prompt="$1"
default="${2:-y}"
answer="$(ask "$prompt (y/n)" "$default")"
case "$(printf '%s' "$answer" | tr '[:upper:]' '[:lower:]')" in
y|yes) return 0 ;;
*) return 1 ;;
esac
}
save_profile() {
name="$1"
target="$BASE_DIR/$name.tar.gz"
say ""
say "Saving current router configuration as '$name'..."
mkdir -p "$BASE_DIR"
umask 077
sysupgrade -b "$target"
say "Saved $target"
}
append_unique_line() {
file="$1"
line="$2"
grep -Fqx "$line" "$file" 2>/dev/null || printf '%s\n' "$line" >> "$file"
}
write_switch_script() {
mkdir -p /etc/gl-switch.d
cat > "$SWITCH_SCRIPT" <<'EOF'
#!/bin/sh
[ "$1" = "on" ] && f=/root/wds-router-toggle/wds.tar.gz || f=/root/wds-router-toggle/router.tar.gz; [ -f "$f" ] || { logger -t wds-router-toggle "missing profile $f"; exit 1; }; logger -t wds-router-toggle "restoring $f"; sysupgrade -r "$f"; reboot
EOF
chmod 755 "$SWITCH_SCRIPT"
}
write_switch_config() {
cat > "$SWITCH_CONFIG" <<'EOF'
config main
option func 'wds-router-toggle'
EOF
}
main() {
need_root
say "GL.iNet WDS / Router Toggle installer"
say "Switch off => Router profile"
say "Switch on => WDS profile"
write_switch_script
write_switch_config
mkdir -p "$BASE_DIR"
if confirm "Keep the switch files across firmware upgrades" "y"; then
append_unique_line "$SYSUPGRADE_CONF" "$SWITCH_SCRIPT"
append_unique_line "$SYSUPGRADE_CONF" "$SWITCH_CONFIG"
append_unique_line "$SYSUPGRADE_CONF" "$BASE_DIR/"
fi
say ""
say "Choose what to save:"
say "1. Save current config as Router profile"
say "2. Save current config as WDS profile"
say "3. Guided setup for both profiles"
say "4. Install switch only"
choice="$(ask "Selection" "3")"
case "$choice" in
1)
save_profile router
;;
2)
save_profile wds
;;
3)
say ""
say "Step 1: set the router exactly how you want normal Router mode."
confirm "Press y when the current config is ready to save as Router" "y" || exit 1
save_profile router
say ""
say "Step 2: change the router in the web UI so WDS is fully working."
confirm "Press y when the current config is ready to save as WDS" "y" || exit 1
save_profile wds
;;
4)
;;
*)
say "Invalid selection."
exit 1
;;
esac
say ""
say "Installed:"
say " $SWITCH_SCRIPT"
say " $SWITCH_CONFIG"
say ""
say "Profiles:"
[ -f "$ROUTER_PROFILE" ] && say " Router: $ROUTER_PROFILE" || say " Router: not saved yet"
[ -f "$WDS_PROFILE" ] && say " WDS: $WDS_PROFILE" || say " WDS: not saved yet"
say ""
say "Manual test:"
say " $SWITCH_SCRIPT off"
say " $SWITCH_SCRIPT on"
}
main "$@"