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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#!/bin/bash
# mkhint - Manage hint files for slackrepo scripts
#
# Usage:
# ./mkhint --version VERSION --hintfile FILE Update existing hint file
# ./mkhint --version VERSION --new FILE Create new hint file
# ./mkhint --new FILE Create new hint file (no version)
# ./mkhint --list List hint files
# ./mkhint --clean Remove .bak files from HINT_DIR
# ./mkhint --delete FILE Delete a hint file (and .bak if present)
# ./mkhint --help Show this help
set -e
# Default configuration
REPO_DIR="/var/lib/sbopkg/SBo-danix/"
HINT_DIR="/etc/slackrepo/SBo-danix/hintfiles/"
TMP_DIR="/tmp/mkhint"
# create the temp dir if not existing
if [[ ! -d $TMP_DIR ]]; then
mkdir $TMP_DIR
fi
# Variables
VERSION=""
HINT_FILE=""
NEW_HINT_FILE=""
DELETE_HINT_FILES=()
COMMAND=""
# Show help message
show_help() {
cat <<EOF
mkhint - Manage hint files for slackrepo scripts
Usage:
./mkhint --version VERSION --hintfile FILE Update existing hint file
./mkhint --version VERSION --new FILE Create new hint file
./mkhint --new FILE Create new hint file (no version)
./mkhint --list List hint files
./mkhint --clean Remove .bak files from HINT_DIR
./mkhint --help Show this help
Options:
--version, -v VERSION New version string (required for --hintfile)
--hintfile, -f FILE Path to existing hint file (required with --version)
--new, -n FILE Create new hint file (required with --version or standalone)
--list, -l List all hint files in the default directory
--clean, -c Remove all .bak files from HINT_DIR
--delete, -d FILE Delete a hint file (and .bak if present)
--help, -h Show this help message
Hint files are stored in: $HINT_DIR
Temporary files are stored in: $TMP_DIR
Variables order in hint files:
VERSION, ARCH, DOWNLOAD, MD5SUM, DOWNLOAD_x86_64, MD5SUM_x86_64
Exit codes:
0 - Success
1 - Invalid arguments or missing required options
2 - File not found
3 - File already exists
4 - wget not available
EOF
}
# List hint files
list_hint_files() {
if [[ ! -d "$HINT_DIR" ]]; then
echo "Error: Hint directory does not exist: $HINT_DIR" >&2
exit 2
fi
echo "Hint files in: $HINT_DIR"
echo "======================================================="
printf "%-40s %10s %10s %-20s %s\n" "File" "HintVer" "SBOVer" "Category" "Created"
echo "-------------------------------------------------------"
local count=0
for file in "$HINT_DIR"/*.hint; do
if [[ -f "$file" ]]; then
local VER=$(grep "^VERSION" "$file" |cut -d '"' -f2)
local name=$(basename "$file")
local pkg="${name%.hint}"
local info_file
info_file=$(find "$REPO_DIR" -mindepth 2 -name "${pkg}.info" 2>/dev/null | head -1)
local SBO_VER=""
local category=""
if [[ -f "$info_file" ]]; then
SBO_VER=$(grep "^VERSION" "$info_file" | cut -d '"' -f2)
category=$(basename "$(dirname "$(dirname "$info_file")")")
fi
local date=$(stat -c "%y" "$file" | cut -d'.' -f1)
printf "%-40s %10s %10s %-20s %s\n" "$name" "$VER" "$SBO_VER" "$category" "$date"
count=$((count + 1))
fi
done
if [[ $count -eq 0 ]]; then
echo " (no hint files found)"
fi
echo "======================================================="
echo "Total: $count file(s)"
}
# Validate wget availability
check_wget() {
if ! command -v wget &> /dev/null; then
echo "Error: wget is not installed. Please install wget first." >&2
exit 4
fi
}
# download files
download_file() {
local url="$1"
local dlfile="${TMP_DIR}/download"
if [[ -f $dlfile ]]; then
rm $dlfile
fi
# Download the file
if [[ ! -z $1 ]]; then
wget -O "$dlfile" "$url" || return 1
fi
# calculate md5
local md5=$(md5sum "$dlfile" | awk '{print $1}')
rm $dlfile
echo $md5
}
# Create new hint file
create_new_hint_file() {
cd $HINT_DIR
local file="$1"
local normalized_file="${file}"
if [[ "$file" != *.hint ]]; then
normalized_file="${file}.hint"
fi
# search repository for .info file
info=$(find $REPO_DIR -mindepth 2 -name ${normalized_file%.hint}.info)
# Check if file exists
if [[ ! -f "$normalized_file" ]]; then
# the hint file we want to create doesn't exists, so we can check
# the sbo repository for a .info file and use that as hint
if [[ -n $info ]]; then
cp $info $normalized_file
# remove unwanted lines from hint file
sed -i -e "/^PRGNAM=/d" \
-e "/^HOMEPAGE=/d" \
-e "/^MAINTAINER=/d" \
-e "/^EMAIL=/d" \
-e "s/^REQUIRES=/#REQUIRES=/" \
"${normalized_file}"
if grep -q '^ARCH=' $normalized_file; then
sed -i 's/^ARCH=.*/ARCH="x86_64"/' $normalized_file
else
echo 'ARCH="x86_64"' >> $normalized_file
fi
echo "generated $normalized_file from $(basename $info)."
echo "Check variables before using."
fi
else
echo "Hint file exists: $normalized_file" >&2
mv "$normalized_file" "${normalized_file}.bak"
echo "Backed up to: ${normalized_file}.bak" >&2
# Create new hint file with empty variables
cat > "$normalized_file" <<EOF
VERSION=""
ARCH="x86_64"
DOWNLOAD=""
MD5SUM=""
DOWNLOAD_x86_64=""
MD5SUM_x86_64=""
EOF
echo "Created new hint file [EMPTY]: $normalized_file"
fi
}
# Update existing hint file
update_hint_file() {
cd "$HINT_DIR"
local file="$1"
local new_version="$2"
local old_version=""
local normalized_file="${file}"
if [[ "$file" != *.hint ]]; then
normalized_file="${file}.hint"
fi
# Check if file exists
if [[ ! -f "$normalized_file" ]]; then
echo "Error: Hint file does not exist: $normalized_file" >&2
exit 2
fi
# Force backup as precaution before modifying
echo "Hint file exists: $normalized_file" >&2
cp "$normalized_file" "${normalized_file}.bak"
echo "Backed up to: ${normalized_file}.bak" >&2
# Extract current version from hint file
old_version=$(grep '^VERSION=' "$normalized_file" | sed 's/VERSION="//;s/"$//')
# Use sed for global replacement of OLD_VERSION in all variables
sed -i "s/${old_version}/${new_version}/g" "$normalized_file"
download=$(grep '^DOWNLOAD=' "$normalized_file" | sed 's/DOWNLOAD="//;s/"$//')
if [[ -n "$download" ]]; then
if [[ $download != "UNSUPPORTED" && $download != "UNTESTED" ]]; then
# we skip for unsupported or untested arch
sum=$(download_file ${download})
sed -i "/^MD5SUM=/s/MD5SUM=\"[^\"]*\"/MD5SUM=\"$sum\"/" "$normalized_file"
fi
fi
# we should repeat the check for x86_64 DOWNLOAD
download_x64=$(grep '^DOWNLOAD_x86_64=' "$normalized_file" | sed 's/DOWNLOAD_x86_64="//;s/"$//')
if [[ -n "$download_x64" ]]; then
if [[ $download_x64 != "UNSUPPORTED" && $download_x64 != "UNTESTED" ]]; then
# we skip for unsupported or untested arch
sum64=$(download_file ${download_x64})
sed -i "/^MD5SUM_x86_64=/s/MD5SUM_x86_64=\"[^\"]*\"/MD5SUM_x86_64=\"$sum64\"/" "$normalized_file"
fi
fi
hf=$(cat $normalized_file)
echo "Updated hint file: $normalized_file"
echo "=========================================="
echo -n "$hf"
echo; echo "=========================================="
}
# Delete a hint file (and .bak if present)
delete_hint_file() {
local file="$1"
local normalized_file="${file}"
if [[ "$file" != *.hint ]]; then
normalized_file="${file}.hint"
fi
local full_path="${HINT_DIR}/${normalized_file}"
if [[ ! -f "$full_path" ]]; then
echo "Error: Hint file does not exist: $full_path" >&2
exit 2
fi
rm "$full_path"
echo "Deleted: $full_path"
local bak_path="${full_path}.bak"
if [[ -f "$bak_path" ]]; then
rm "$bak_path"
echo "Deleted: $bak_path"
fi
}
# Clean .bak files from HINT_DIR
clean_bak_files() {
if [[ ! -d "$HINT_DIR" ]]; then
echo "Error: Hint directory does not exist: $HINT_DIR" >&2
exit 2
fi
local count=0
for bakfile in "$HINT_DIR"/*.bak; do
if [[ -f "$bakfile" ]]; then
rm "$bakfile"
count=$((count + 1))
fi
done
echo "Removed $count .bak file(s) from $HINT_DIR"
}
# Main function
main() {
# Parse command line arguments directly
while [[ $# -gt 0 ]]; do
case "$1" in
--version|-v)
VERSION="$2"
shift 2
;;
--hintfile|-f)
HINT_FILE="$2"
shift 2
;;
--new|-n)
NEW_HINT_FILE="$2"
shift 2
;;
--list|-l)
COMMAND="list"
shift
;;
--clean|-c)
COMMAND="clean"
shift
;;
--delete|-d)
shift
while [[ $# -gt 0 && "$1" != -* ]]; do
DELETE_HINT_FILES+=("$1")
shift
done
;;
--help|-h)
COMMAND="help"
shift
;;
*)
echo "Unknown option: $1" >&2
show_help
exit 1
;;
esac
done
if [[ -z "$COMMAND" ]]; then
# Default to update hint file if VERSION and HINT_FILE are provided
if [[ -n "$VERSION" && -n "$HINT_FILE" ]]; then
COMMAND="update"
elif [[ -n "$NEW_HINT_FILE" ]]; then
COMMAND="new"
elif [[ ${#DELETE_HINT_FILES[@]} -gt 0 ]]; then
COMMAND="delete"
fi
fi
case "$COMMAND" in
help)
show_help
;;
list)
list_hint_files
;;
clean)
clean_bak_files
;;
update)
check_wget
update_hint_file "$HINT_FILE" "$VERSION"
;;
new)
create_new_hint_file "$NEW_HINT_FILE"
;;
delete)
for f in "${DELETE_HINT_FILES[@]}"; do
delete_hint_file "$f"
done
;;
*)
echo "Error: Unknown command: $COMMAND" >&2
show_help
exit 1
;;
esac
}
main "$@"
|