maison-diaporama/bin/create-json
2023-01-07 18:32:57 +01:00

65 lines
1.1 KiB
Bash
Executable file

#!/bin/sh
usage() {
printf "Usage: %s -s SRC -o OUTPUT -b BASE_URL\n" "$0"
}
on_interrupt() {
print "Process aborted!\n"
exit 130
}
main() {
while getopts "hs:b:o:" option; do
case "${option}" in
h) usage; exit 0;;
s) SRC="$OPTARG";;
o) OUTPUT="$OPTARG";;
b) BASE_URL="$OPTARG";;
*) usage; exit 1;;
esac
done
if [ -z "$SRC" ]; then
printf "You must provided the source (directory).\n"
usage
exit 1
fi
if [ -z "$OUTPUT" ]; then
printf "You must provided the output (json file).\n"
usage
exit 1
fi
if [ -z "$BASE_URL" ]; then
printf "You must provided the base url.\n"
usage
exit 1
fi
images=""
for image in "$SRC/"*; do
filename="$(basename "$image")"
date="$(printf "%s" "$filename" | sed 's#^\([[:digit:]]\{4\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)_.*$#\3/\2/\1#')"
images="$(
printf '%s%s{"file":"%s/%s","date":"%s"}' \
"$images" \
"$(test -n "$images" && printf ",")" \
"$BASE_URL" \
"$filename" \
"$date"
)"
done
printf '{"images":[%s]}' "$images" > "$OUTPUT"
exit 0
}
trap on_interrupt INT
main "$@"