-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.sh
204 lines (181 loc) · 5.89 KB
/
plugin.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
#!/bin/bash
show_help() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -a, --author The author of the plugin."
echo " -c, --config Specify config file location. (default: ./plugin.cfg)"
echo " -i, --input The directory of the plugin's source code. (default: ./src/)"
echo " -m, --md5 The MD5 hash of the plugin *.txz file. (required but automatically provided)"
echo " -n, --name The name of the plugin. (required)"
echo " -r, --repo The name of the git repo for the plugin."
echo " -u, --url The URL of the *.plg file. (required for automatic checking and updating of the plugin)"
echo " -v, --version The version of the plugin. If not specified it will be generated in the standard Limetech format (e.g 2023.07.21a)"
echo
echo " -l, --min The minimun version of UnRAID allowed for the plugin. Typically used to prevent compatibility issues."
echo " -x, --max The maximum version of UnRAID allowed for the plugin. Typically used to prevent compatibility issues."
echo " -h, --help Display this help message."
exit 0
}
#####################package_plugin#####################
# #
package_plugin() {
dest="../tmp/usr/local/emhttp/plugins/${name}"
mkdir -p "$dest"
echo "Copying files to temporary folder to archive..."
rsync -av --exclude=".*" --exclude='plugin.sh' --exclude='sh/' ./ "$dest"
pushd ../tmp
#tar -cJf ../"${name}".txz --owner=0 --group=0 usr/*
makepkg ../${repo}/"${name}".txz <<< n
popd
rm -dr ../tmp
MD5=$(md5sum "${name}.txz" | awk '{print $1}')
echo "Package hash: $MD5"
}
# #
########################################
#####################create_entity#####################
# #
create_entity() {
keys=()
while IFS='=' read -r line; do
if [[ $line == *"="* ]]; then
keys+=("${line%%=*}")
fi
done < "$config"
keys+=("version")
keys+=("MD5")
max=$(( $(printf "%s\n" "${keys[@]}" | awk '{ print length }' | sort -nr | head -1) + 3 ))
for key in "${keys[@]}"; do
case "$key" in
"version")
value="$version"
;;
"MD5")
value="$MD5"
;;
*)
value="${!key}"
;;
esac
new_key=$(printf "%-${max}s" "$key")
PLUGIN+="<!ENTITY ${new_key}\"$value\">"$'\n'
done
PLUGIN+="]>"$'\n'
}
# #
########################################
#####################getver#####################
# #
getver(){
# get current date and previous version
version=$(date +"%Y.%m.%d")
datepattern='ENTITY version\s+"([^"]+)"'
echo "Current date: ${version}"
url=$(echo "$pluginURL" | sed 's/&/\$/g; s/;//g')
eval "url=\"$url\""
echo "Downloading previous version: ${url}"
wget $url
previousVersion=$(grep -oP '<!ENTITY version\s*"\K[^"]*' $OUTPUT_FILE)
echo "Previous version: ${previousVersion}"
# determine new version
if [[ $version == $previousVersion ]]; then
version+="a"
echo "New version: ${version}"
fi
if [[ $version == ${previousVersion%?} && "$previousVersion" =~ [[:alpha:]]$ ]]; then
extracted_letter=${previousVersion: -1}
echo "Previous sub-version: ${extracted_letter}"
ascii_code=$(printf "%d" "'$extracted_letter")
next_ascii_code=$((ascii_code + 1))
next_letter=$(printf \\$(printf '%03o' "$next_ascii_code"))
version+="$next_letter"
fi
echo "TAG=$version" >> $GITHUB_ENV
}
# #
########################################
########################################################################################################################################################################
# Check if makepkg is installed
if ! command -v makepkg >/dev/null 2>&1; then
echo "makepkg is not installed"
exit 1
fi
# Load the config file
config="./plugin.cfg"
if [[ -f "$config" ]]; then
echo "Loading settings from config file: $config"
. $config
else
echo "Config file not found: $config"
exit 1
fi
OUTPUT_FILE="${name}.plg"
#####################
package_plugin
getver
########################################
PLUGIN="<?xml version='1.0' standalone='yes'?>"$'\n'
PLUGIN+=""$'\n'
PLUGIN+="<!DOCTYPE PLUGIN ["$'\n'
create_entity
sed_string+='/'
for key in "${keys[@]}"; do
sed_string+="&${key};|"
done
sed_string="${sed_string%?}/!s/&/&/g"
PLUGIN+="<PLUGIN"
for key in "${keys[@]}"; do
PLUGIN+=" ${key}=\"&${key};\""
done
PLUGIN+=">"$'\n'$'\n'
PLUGIN+="<CHANGES>"$'\n'
if [[ -e "CHANGELOG.md" ]]; then
PLUGIN+=$(<CHANGELOG.md)$'\n'
fi
PLUGIN+="</CHANGES>"$'\n'$'\n'
#####################################
if [[ -e "./.plugin/01-files.txt" ]]; then
PLUGIN+="<!-- SOURCE FILES -->
$(<./.plugin/01-files.txt)"$'\n'$'\n'
fi
if [[ -e "./.plugin/02-pre-install.sh" ]]; then
PLUGIN+="<!-- PRE-INSTALL SCRIPT -->
<FILE Run=\"/bin/bash\" Method=\"install\">
<INLINE>
$(sed -E "$sed_string" ./.plugin/02-pre-install.sh)
</INLINE>
</FILE>"$'\n'$'\n'
fi
PLUGIN+="<!-- SOURCE PACKAGE -->
<FILE Name=\"&source;.txz\" Run=\"upgradepkg --install-new --reinstall\">
<URL>&packageURL;</URL>
<MD5>&MD5;</MD5>
</FILE>"$'\n'$'\n'
if [[ -e "./.plugin/install.sh" ]]; then
PLUGIN+="<!-- INSTALL SCRIPT -->
<FILE Run=\"/bin/bash\" Method=\"install\">
<INLINE>
$(sed -E "$sed_string" ./.plugin/install.sh)
</INLINE>
</FILE>"$'\n'$'\n'
fi
if [[ -e "./.plugin/03-post-install.sh" ]]; then
PLUGIN+="<!-- POST-INSTALL SCRIPT -->
<FILE Run=\"/bin/bash\" Method=\"install\">
<INLINE>
$(sed -E "$sed_string" ./.plugin/03-post-install.sh)
</INLINE>
</FILE>"$'\n'$'\n'
fi
if [[ -e "./.plugin/04-remove.sh" ]]; then
PLUGIN+="<!-- REMOVE SCRIPT -->
<FILE Run=\"/bin/bash\" Method=\"remove\">
<INLINE>
$(<./.plugin/04-remove.sh)
</INLINE>
</FILE>"$'\n'$'\n'
fi
PLUGIN+="</PLUGIN>"
###############################################
echo "${PLUGIN}" > "${OUTPUT_FILE}"