initial version
This commit is contained in:
commit
bf27e7a47f
47
mige.sh
Executable file
47
mige.sh
Executable file
@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR_CONTENT=${DIR_CONTENT:-in}
|
||||
DIR_DIST=${DIR_DIST:-out}
|
||||
TEMPLATE_HEADER=${TEMPLATE_HEADER:-templates/header.html}
|
||||
TEMPLATE_FOOTER=${TEMPLATE_FOOTER:-templates/footer.html}
|
||||
SOURCEDIR=$( dirname -- $BASH_SOURCE )
|
||||
|
||||
check_files () {
|
||||
if [ ! -f "$SOURCEDIR/$TEMPLATE_HEADER" ]; then echo "Header template '$TEMPLATE_HEADER' doesn't exist."; exit 2; fi
|
||||
if [ ! -f "$SOURCEDIR/$TEMPLATE_FOOTER" ]; then echo "Header template '$TEMPLATE_FOOTER' doesn't exist."; exit 2; fi
|
||||
if [ ! -d "$SOURCEDIR/$DIR_CONTENT" ]; then echo "Content directory '$DIR_CONTENT' doesn't exist. Create it and add some content!"; exit 4; fi
|
||||
if [ ! -d "$SOURCEDIR/$DIR_DIST" ]; then mkdir "$SOURCEDIR/$DIR_DIST"; echo "Directory '$DIR_DIST' created."; fi
|
||||
}
|
||||
|
||||
process_file () {
|
||||
local timep=$(date +%s%N)
|
||||
|
||||
mkdir -p $( dirname "$1" | sed "s/$SOURCEDIR\/$DIR_CONTENT/$SOURCEDIR\/$DIR_DIST/" )
|
||||
local file_output=$( echo "$1" | sed "s/$SOURCEDIR\/$DIR_CONTENT/$SOURCEDIR\/$DIR_DIST/" )
|
||||
cat "$SOURCEDIR/$TEMPLATE_HEADER" "$1" "$SOURCEDIR/$TEMPLATE_FOOTER" > $file_output
|
||||
while read -r variable; do
|
||||
if [[ $variable =~ ^([0-9]+)=(.+)$ ]]; then
|
||||
local variable_name=${BASH_REMATCH[1]}
|
||||
local variable_value=${BASH_REMATCH[2]}
|
||||
sed -i "s/\$$variable_name/$variable_value/g;/$variable/d" $file_output
|
||||
else
|
||||
echo -e "\e[38;5;208mWarning: could not match '$variable' as a variable assignment.\e[0m"
|
||||
fi
|
||||
|
||||
done < <(grep -P '^\d+=.+$' $1)
|
||||
|
||||
timep=$((($(date +%s%N) - $timep)/1000000))
|
||||
echo -e "\e[32mFile $1 processed to $file_output ($timep ms).\e[0m"
|
||||
}
|
||||
|
||||
main () {
|
||||
while IFS= read -d '' file_content; do
|
||||
process_file "${file_content}" </dev/null
|
||||
done < <(find "$SOURCEDIR/$DIR_CONTENT" -type f -print0)
|
||||
}
|
||||
|
||||
check_files
|
||||
|
||||
echo -e "\e[1m=== STARTING ===\e[0m"
|
||||
main
|
||||
echo -e "\e[1m=== FINISHED ===\e[0m"
|
||||
Loading…
x
Reference in New Issue
Block a user