3 |
# mkdist -- a script to create software distributions |
# mkdist -- a script to create software distributions |
4 |
# |
# |
5 |
# This script is part of OSN Commons. Copyright (C) 2024 OSN, Inc. |
# This script is part of OSN Commons. Copyright (C) 2024 OSN, Inc. |
6 |
# |
# |
7 |
# OSN Commons is free software: you can redistribute it and/or modify it |
# OSN Commons is free software: you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation, either version 3 of the License, or |
# the Free Software Foundation, either version 3 of the License, or |
10 |
# (at your option) any later version. |
# (at your option) any later version. |
11 |
# |
# |
12 |
# OSN Commons is distributed in the hope that it will be useful, |
# OSN Commons is distributed in the hope that it will be useful, |
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28 |
version="1.0.0" |
version="1.0.0" |
29 |
|
|
30 |
check_requirements() { |
check_requirements() { |
31 |
if [ -z "$(command -v tar)" ]; then |
if [ -z "$(command -v tar)" ]; then |
32 |
echo "$me: GNU tar is required to use this script" >&2 |
echo "$me: GNU tar is required to use this script" >&2 |
33 |
exit 1 |
exit 1 |
34 |
fi |
fi |
35 |
|
|
36 |
if [ -z "$(command -v jq)" ]; then |
if [ -z "$(command -v jq)" ]; then |
37 |
echo "$me: jq is required to use this script" >&2 |
echo "$me: jq is required to use this script" >&2 |
38 |
exit 1 |
exit 1 |
39 |
fi |
fi |
40 |
|
|
41 |
if [ -z "$(command -v rsync)" ]; then |
if [ -z "$(command -v rsync)" ]; then |
42 |
echo "$me: rsync is required to use this script" >&2 |
echo "$me: rsync is required to use this script" >&2 |
43 |
exit 1 |
exit 1 |
44 |
fi |
fi |
45 |
|
|
46 |
if [ -z "$(command -v sed)" ]; then |
if [ -z "$(command -v sed)" ]; then |
47 |
echo "$me: sed is required to use this script" >&2 |
echo "$me: sed is required to use this script" >&2 |
48 |
exit 1 |
exit 1 |
49 |
fi |
fi |
98 |
} |
} |
99 |
|
|
100 |
stat_directory() { |
stat_directory() { |
101 |
local result; |
local result |
102 |
result=$(stat "$1" 2>&1) |
result=$(stat "$1" 2>&1) |
103 |
|
|
104 |
if [ $? -ne 0 ]; then |
if [ $? -ne 0 ]; then |
207 |
fi |
fi |
208 |
|
|
209 |
case $1 in |
case $1 in |
210 |
-h|--help) |
-h | --help) |
211 |
show_help |
show_help |
212 |
exit 0 |
exit 0 |
213 |
;; |
;; |
214 |
-v|--version) |
-v | --version) |
215 |
show_version |
show_version |
216 |
exit 0 |
exit 0 |
217 |
;; |
;; |
218 |
-f|--formats) |
-f | --formats) |
219 |
check_optarg $@ |
check_optarg $@ |
220 |
shift |
shift |
221 |
formats=$(echo $1 | tr ',' ' ') |
formats=$(echo $1 | tr ',' ' ') |
222 |
;; |
;; |
223 |
-o|--output-path) |
-o | --output-path) |
224 |
check_optarg $@ |
check_optarg $@ |
225 |
shift |
shift |
226 |
output_path=$1 |
output_path=$1 |
227 |
;; |
;; |
228 |
-n|--output-basename) |
-n | --output-basename) |
229 |
check_optarg $@ |
check_optarg $@ |
230 |
shift |
shift |
231 |
output_basename="$1" |
output_basename="$1" |
232 |
;; |
;; |
233 |
-e|--exclude) |
-e | --exclude) |
234 |
check_optarg $@ |
check_optarg $@ |
235 |
shift |
shift |
236 |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
237 |
excludes="$excludes --exclude \"$escaped\"" |
excludes="$excludes --exclude \"$escaped\"" |
238 |
;; |
;; |
239 |
--exclude-from) |
--exclude-from) |
240 |
check_optarg $@ |
check_optarg $@ |
241 |
shift |
shift |
242 |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
escaped=$(echo "$1" | sed 's/"/\\"/g') |
243 |
excludes="$excludes --exclude-from \"$escaped\"" |
excludes="$excludes --exclude-from \"$escaped\"" |
244 |
;; |
;; |
245 |
-t|--archive-version) |
-t | --archive-version) |
246 |
check_optarg $@ |
check_optarg $@ |
247 |
shift |
shift |
248 |
archive_version="$1" |
archive_version="$1" |
249 |
;; |
;; |
250 |
-V|--verbose) |
-V | --verbose) |
251 |
verbose=1 |
verbose=1 |
252 |
;; |
;; |
253 |
--) |
--) |
254 |
posarg_start=1 |
posarg_start=1 |
255 |
shift |
shift |
256 |
break |
break |
257 |
;; |
;; |
258 |
-*) |
-*) |
259 |
echo "$me: invalid option '$1'" >&2 |
echo "$me: invalid option '$1'" >&2 |
260 |
|
echo "Try '$me --help' for more information." >&2 |
261 |
|
exit 1 |
262 |
|
;; |
263 |
|
*) |
264 |
|
if [ ! -z "$target" ]; then |
265 |
|
echo "$me: too many arguments provided" >&2 |
266 |
echo "Try '$me --help' for more information." >&2 |
echo "Try '$me --help' for more information." >&2 |
267 |
exit 1 |
exit 1 |
268 |
;; |
fi |
|
*) |
|
|
if [ ! -z "$target" ]; then |
|
|
echo "$me: too many arguments provided" >&2 |
|
|
echo "Try '$me --help' for more information." >&2 |
|
|
exit 1 |
|
|
fi |
|
269 |
|
|
270 |
target="$1" |
target="$1" |
271 |
;; |
;; |
272 |
esac |
esac |
273 |
shift |
shift |
274 |
done |
done |
321 |
for format in "$formats"; do |
for format in "$formats"; do |
322 |
archive_name="${output_basename}-${archive_version}.$format" |
archive_name="${output_basename}-${archive_version}.$format" |
323 |
|
|
324 |
if echo "$format" | grep -o "tar" > /dev/null 2>&1; then |
if echo "$format" | grep -o "tar" >/dev/null 2>&1; then |
325 |
echo "$archive_version" > $rsync_tmpdir/.tarball-version |
echo "$archive_version" >$rsync_tmpdir/.tarball-version |
326 |
fi |
fi |
327 |
|
|
328 |
if [ "$format" = "zip" ]; then |
if [ "$format" = "zip" ]; then |
329 |
echo "$archive_version" > $rsync_tmpdir/.zipball-version |
echo "$archive_version" >$rsync_tmpdir/.zipball-version |
330 |
fi |
fi |
331 |
|
|
332 |
case "$format" in |
case "$format" in |
333 |
tar.gz|tar.bz2|tar.xz) |
tar.gz | tar.bz2 | tar.xz) |
334 |
flag="" |
flag="" |
335 |
|
|
336 |
case "$format" in |
case "$format" in |
337 |
tar.gz) |
tar.gz) |
338 |
flag="z" |
flag="z" |
|
;; |
|
|
tar.bz2) |
|
|
flag="j" |
|
|
;; |
|
|
tar.xz) |
|
|
flag="J" |
|
|
;; |
|
|
esac |
|
|
|
|
|
tar_cmd="tar -c${flag}f "$output_path/$archive_name" -C "$tmpdir" ." |
|
|
|
|
|
if [ $verbose -eq 1 ]; then |
|
|
echo "$me: executing: $tar_cmd" |
|
|
fi |
|
|
|
|
|
eval $tar_cmd |
|
339 |
;; |
;; |
340 |
zip) |
tar.bz2) |
341 |
zip_cmd="zip -r "$output_path/$archive_name" $tmpdir" |
flag="j" |
|
|
|
|
if [ $verbose -eq 1 ]; then |
|
|
echo "$me: executing: $zip_cmd" |
|
|
fi |
|
|
|
|
|
eval $zip_cmd |
|
342 |
;; |
;; |
343 |
|
tar.xz) |
344 |
*) |
flag="J" |
|
echo "$me: unsupported format '$format'" >&2 |
|
|
exit 1 |
|
345 |
;; |
;; |
346 |
|
esac |
347 |
|
|
348 |
|
tar_cmd="tar -c${flag}f "$output_path/$archive_name" -C "$tmpdir" ." |
349 |
|
|
350 |
|
if [ $verbose -eq 1 ]; then |
351 |
|
echo "$me: executing: $tar_cmd" |
352 |
|
fi |
353 |
|
|
354 |
|
eval $tar_cmd |
355 |
|
;; |
356 |
|
zip) |
357 |
|
zip_cmd="zip -r "$output_path/$archive_name" $tmpdir" |
358 |
|
|
359 |
|
if [ $verbose -eq 1 ]; then |
360 |
|
echo "$me: executing: $zip_cmd" |
361 |
|
fi |
362 |
|
|
363 |
|
eval $zip_cmd |
364 |
|
;; |
365 |
|
|
366 |
|
*) |
367 |
|
echo "$me: unsupported format '$format'" >&2 |
368 |
|
exit 1 |
369 |
|
;; |
370 |
esac |
esac |
371 |
|
|
372 |
rm -f $rsync_tmpdir/.tarball-version |
rm -f $rsync_tmpdir/.tarball-version |
375 |
|
|
376 |
rm_flags=-rf |
rm_flags=-rf |
377 |
|
|
378 |
if [ $verbose -eq 1 ]; then |
if [ $verbose -eq 1 ]; then |
379 |
rm_flags="${rm_flags}v" |
rm_flags="${rm_flags}v" |
380 |
fi |
fi |
381 |
|
|
|
rm $rm_flags $rsync_tmpdir |
|
382 |
|
rm $rm_flags $rsync_tmpdir |