bunteshaus.de/add_archive.sh
2022-06-26 19:44:15 +02:00

93 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# define folder for content
PATH="content/posts"
# define an error function
err() {
echo "${1}"
zenity --error --no-wrap --text "${1}"
exit 1
}
#define a warn function
warn() {
echo "${1}"
zenity --warning --no-wrap --text "${1}"
}
#check if unzip installed
if ! command -v unzip &> /dev/null
then
err "'unzip' could not be found"
fi
# choose files to process
IFS=$'\n' files=($(zenity --file-selection --multiple --separator=$'\n' --title="Choose files"))
# check and extract files to destination path
for file in "${files[@]}"; do
# sanitize filename
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
folder="${PATH}/${fname}"
# check if file is compatible by checking if 'index.de.md' available
if ! unzip -l "${file}"|grep -q "index.de.md"; then
err "Maybe wrong archive? This is not compatible."
fi
#check if folder '${PATH}' available
if ! [ -d "${PATH}" ]; then
err "Folder 'content' not found."
fi
# create folder
mkdir -p "${folder}"
# deflate zip file
unzip -o -d "${folder}" "${file}"
# add folder for next commit
git add "${folder}" && \
git commit -m "${fname}" # commit that folder
done
# make a couple of lints for the archives
output_temp=""
for file in "${files[@]}"; do
banner="false"
# sanitize filename
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
folder="${PATH}/${fname}"
# get a list of files in folder
for f in $(find "${folder}" -type f); do
# check if a file called 'banner*' exists
if echo ${i}|grep -q "banner"; then
banner="true"
fi
done
# compile missing or worng things
if [ ${banner} == "false" ]; then
output_temp="${output_temp}banner missing\n "
fi
done
# print missing or wrongg things
warn "${output_temp}"
# completed.. and tell it!
zenity --info --text "Done"