-
Notifications
You must be signed in to change notification settings - Fork 1
/
library.sh
executable file
·209 lines (185 loc) · 5.29 KB
/
library.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# NextCloudPi function library
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# More at ownyourbits.com
#
# Initializes $INSTALLATION_CODE
function config()
{
local INSTALL_SCRIPT="$1"
local BACKTITLE="NextCloudPi installer configuration"
type dialog &>/dev/null || { echo "please, install dialog for interactive configuration"; return 1; }
test -f "$INSTALL_SCRIPT" || { echo "file $INSTALL_SCRIPT not found"; return 1; }
local VARS=( $( grep "^[[:alpha:]]\+_=" "$INSTALL_SCRIPT" | sed 's|_=.*$||' ) )
local VALS=( $( grep "^[[:alpha:]]\+_=" "$INSTALL_SCRIPT" | sed 's|^.*_=||' ) )
[[ "$NO_CONFIG" == "1" ]] || test ${#VARS[@]} -eq 0 && { INSTALLATION_CODE="$( cat "$INSTALL_SCRIPT" )"; return; }
for i in $( seq 1 1 ${#VARS[@]} ); do
local PARAM+="${VARS[$((i-1))]} $i 1 ${VALS[$((i-1))]} $i 15 60 120 "
done
local DIALOG_OK=0
local DIALOG_CANCEL=1
local DIALOG_ERROR=254
local DIALOG_ESC=255
local RET=0
while test $RET != 1 && test $RET != 250; do
local value
value="$( dialog --ok-label "Start" \
--no-lines --backtitle "$BACKTITLE" \
--form "Enter configuration for $( basename "$INSTALL_SCRIPT" .sh )" \
20 70 0 $PARAM \
3>&1 1>&2 2>&3 )"
RET=$?
case $RET in
$DIALOG_CANCEL)
return 1
;;
$DIALOG_OK)
local RET_VALS=()
while read l; do RET_VALS+=("$l"); done < <( echo -e "$value" )
for i in $( seq 0 1 $(( ${#RET_VALS[@]} - 1 )) ); do
# check for invalid characters
grep -q "[&[:space:]]" <<< "${RET_VALS[$i]}" && { echo "Invalid characters in field ${VARS[$i]}"; return 1; }
local SEDRULE+="s|^${VARS[$i]}_=.*|${VARS[$i]}_=${RET_VALS[$i]}|;"
done
break
;;
$DIALOG_ERROR)
echo "ERROR!$value"
return 1
;;
$DIALOG_ESC)
echo "ESC pressed."
return 1
;;
*)
echo "Return code was $RET"
return 1
;;
esac
done
INSTALLATION_CODE="$( sed "$SEDRULE" "$INSTALL_SCRIPT" )"
}
function install_script()
{
(
local SCRIPT=$1
source ./"$SCRIPT"
echo -e "Installing $( basename "$SCRIPT" .sh )"
set +x
install
)
}
function activate_script()
{
local SCRIPT=$1
echo -e "Activating $( basename "$SCRIPT" .sh )"
launch_script "$SCRIPT"
}
function is_active_script()
{
(
local SCRIPT=$1
unset is_active
source "$SCRIPT"
[[ $( type -t is_active ) == function ]] && {
is_active
return $?
}
grep -q "^ACTIVE_=yes" "$SCRIPT" && return 0
)
}
function run_and_log()
{
local SCRIPT=$1
touch /var/log/ncp.log
chmod 640 /var/log/ncp.log
chown root:www-data /var/log/ncp.log
echo -e "[ $( basename "$SCRIPT" .sh ) ]" >> /var/log/ncp.log
configure 2>&1 | tee -a /var/log/ncp.log
local RET="${PIPESTATUS[0]}"
echo "" >> /var/log/ncp.log
return "$RET"
}
function launch_script()
{
(
local SCRIPT=$1
source ./"$SCRIPT"
set +x
run_and_log "$SCRIPT"
)
}
# show an info box for a script if the INFO variable is set in the script
function info_script()
{
(
local SCRIPT=$1
cd /usr/local/etc/ncp-config.d/ || return 1
unset show_info INFO INFOTITLE
source ./"$SCRIPT"
local INFOTITLE="${INFOTITLE:-Info}"
[[ "$INFO" == "" ]] && return 0
whiptail --yesno --backtitle "NextCloudPi configuration" --title "$INFOTITLE" "$INFO" 20 90
)
}
function configure_script()
{
(
local SCRIPT=$1
cd /usr/local/etc/ncp-config.d/ || return 1
config "$SCRIPT" || return 1 # writes "$INSTALLATION_CODE"
echo -e "$INSTALLATION_CODE" > "$SCRIPT" # save configuration
source ./"$SCRIPT" # load configuration
printf '\033[2J' && tput cup 0 0 # clear screen, don't clear scroll, cursor on top
echo -e "Launching $( basename "$SCRIPT" .sh )"
set +x
run_and_log "$SCRIPT"
return 0
)
}
function cleanup_script()
{
(
local SCRIPT=$1
cd /usr/local/etc/ncp-config.d/ || return 1
unset cleanup
source ./"$SCRIPT"
if [[ $( type -t cleanup ) == function ]]; then
cleanup
return $?
fi
return 0
)
}
function persistent_cfg()
{
local SRC="$1"
local DST="${2:-/data/etc/$( basename "$SRC" )}"
test -e /changelog.md && return # trick to disable in dev docker
mkdir -p "$( dirname "$DST" )"
test -e "$DST" || {
echo "Making $SRC persistent ..."
mv "$SRC" "$DST"
}
rm -rf "$SRC"
ln -s "$DST" "$SRC"
}
# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA