bunteshaus.de/add_archive.linux.sh

93 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2022-06-26 15:04:39 +02:00
#!/bin/bash
2022-06-26 19:42:17 +02:00
# define folder for content
2022-07-12 17:03:50 +02:00
CONTENT_PATH="content/posts"
2022-06-26 19:42:17 +02:00
# define an error function
2022-06-26 15:04:39 +02:00
err() {
echo "${1}"
zenity --error --no-wrap --text "${1}"
2022-06-26 19:14:13 +02:00
exit 1
2022-06-26 15:04:39 +02:00
}
2022-06-26 19:42:17 +02:00
#define a warn function
2022-06-26 15:04:39 +02:00
warn() {
echo "${1}"
zenity --warning --no-wrap --text "${1}"
}
2022-06-26 19:42:17 +02:00
#check if unzip installed
2022-06-26 15:04:39 +02:00
if ! command -v unzip &> /dev/null
then
err "'unzip' could not be found"
fi
2022-06-26 19:42:17 +02:00
# choose files to process
IFS=$'\n' files=($(zenity --file-selection --multiple --separator=$'\n' --title="Choose files"))
2022-06-26 15:04:39 +02:00
2022-06-26 19:42:17 +02:00
# check and extract files to destination path
2022-06-26 18:53:44 +02:00
for file in "${files[@]}"; do
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# sanitize filename
2022-06-26 19:23:41 +02:00
s="$(basename ${file%.*})" # receive input in first argument
s="${s//[^[:alnum:]]/-}" # replace all non-alnum characters to -
s="${s//+(-)/-}" # convert multiple - to single -
s="${s/#-}" # remove - from start
s="${s/%-}" # remove - from end
fname="${s,,}" # convert to lowercase
2022-07-12 17:03:50 +02:00
folder="${CONTENT_PATH}/${fname}"
2022-06-26 19:42:17 +02:00
# check if file is compatible by checking if 'index.de.md' available
2022-06-26 18:53:44 +02:00
if ! unzip -l "${file}"|grep -q "index.de.md"; then
2022-06-26 19:42:17 +02:00
err "Maybe wrong archive? This is not compatible."
2022-06-26 15:04:39 +02:00
fi
2022-06-26 19:42:17 +02:00
#check if folder '${PATH}' available
2022-07-12 17:03:50 +02:00
if ! [ -d "${CONTENT_PATH}" ]; then
2022-06-26 19:42:17 +02:00
err "Folder 'content' not found."
2022-06-26 15:04:39 +02:00
fi
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# create folder
2022-06-26 18:53:44 +02:00
mkdir -p "${folder}"
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# deflate zip file
2022-06-26 19:20:24 +02:00
unzip -o -d "${folder}" "${file}"
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# add folder for next commit
2022-06-26 19:44:15 +02:00
git add "${folder}" && \
git commit -m "${fname}" # commit that folder
2022-06-26 15:04:39 +02:00
done
2022-06-26 19:42:17 +02:00
# make a couple of lints for the archives
2022-06-26 15:04:39 +02:00
output_temp=""
2022-06-26 18:53:44 +02:00
for file in "${files[@]}"; do
banner="false"
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# sanitize filename
2022-06-26 19:23:41 +02:00
s="$(basename ${file%.*})" # receive input in first argument
s="${s//[^[:alnum:]]/-}" # replace all non-alnum characters to -
s="${s//+(-)/-}" # convert multiple - to single -
s="${s/#-}" # remove - from start
s="${s/%-}" # remove - from end
fname="${s,,}" # convert to lowercase
2022-07-12 17:03:50 +02:00
folder="${CONTENT_PATH}/${fname}"
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# get a list of files in folder
2022-06-26 18:53:44 +02:00
for f in $(find "${folder}" -type f); do
2022-06-26 19:42:17 +02:00
# check if a file called 'banner*' exists
2022-06-26 18:53:44 +02:00
if echo ${i}|grep -q "banner"; then
banner="true"
fi
done
2022-06-26 19:44:15 +02:00
2022-06-26 19:42:17 +02:00
# compile missing or worng things
2022-06-26 18:53:44 +02:00
if [ ${banner} == "false" ]; then
2022-06-26 15:04:39 +02:00
output_temp="${output_temp}banner missing\n "
fi
done
2022-06-26 19:42:17 +02:00
# print missing or wrongg things
2022-06-26 15:04:39 +02:00
warn "${output_temp}"
2022-06-26 19:42:17 +02:00
# completed.. and tell it!
2022-06-26 19:14:13 +02:00
zenity --info --text "Done"