4cade/bin/buildindexedfile.sh

87 lines
2.8 KiB
Bash
Raw Normal View History

#!/bin/bash
# flags
# -a append to data file (default off = truncate)
# -p pad sizes within data file to next block size (default off)
# parameters
2021-10-23 05:20:34 +00:00
# stdin - input containing list of effects (probably FX.CONF or DFX.CONF)
2021-10-23 05:36:04 +00:00
# stdout - binary OKVS data structure
# 1 - output filename for data file
# 2 - input directory of files to merge into data file
pad=false
append=false
2021-10-21 17:04:01 +00:00
standardoffset=0
standardsize=0
while getopts ":ap" opt; do
case $opt in
a) append=true
;;
p) pad=true
;;
esac
done
shift $((OPTIND-1))
2021-10-21 17:04:01 +00:00
if [ "$append" = false ]; then
2021-10-23 05:36:04 +00:00
rm -f "$1"
fi
2021-10-23 05:36:04 +00:00
touch "$1"
2021-10-21 17:04:01 +00:00
# if there is a file called "STANDARD" in the input directory, add it now
# because we will reuse it for any files that don't exist
2021-10-23 05:36:04 +00:00
if [ -f "$2"/STANDARD ]; then
standardoffset=$(wc -c < "$1")
standardsize=$(wc -c < "$2/STANDARD")
cat "$2"/STANDARD >> "$1"
2021-10-21 17:04:01 +00:00
fi
# make temp file with list of lines that contain keys
records=$(mktemp)
2021-10-23 05:20:34 +00:00
awk '!/^$|^#|^\[/' > "$records"
# make temp assembly source file that represents the binary OKVS data structure
source=$(mktemp)
(echo "*=0" # dummy program counter for assembler
echo "!le16 $(wc -l <"$records"), 0" # OKVS header
while IFS="=" read -r key value; do
echo "!byte ${#key}+7" # OKVS record length
echo "!byte ${#key}" # OKVS key length
echo "!text \"$key\"" # OKVS key
2021-10-23 05:36:04 +00:00
if [ -f "$2/$key" ]; then # if file exists, determine offset and size
offset=$(wc -c < "$1")
echo "!be24 $offset" # offset into merged data file
echo -n "!le16 "
2021-10-23 05:36:04 +00:00
size=$(wc -c < "$2/$key")
if [ "$pad" = true ]; then
# If offset+size does not cross a block boundary, use file's true size.
# Otherwise, round up size to the next block boundary.
# This padding does not get added to the file; it is just an
# optimization to avoid a partial copy on the last block read.
if [ $(($offset / 512)) -eq $((($offset + $size) / 512)) ]; then
echo "$size"
else
echo "$(((($offset + $size + 511) & -512) - $offset))"
fi
else
# Caller said never pad, so always use file's true size.
echo "$size"
fi
2021-10-23 05:36:04 +00:00
cat "$2/$key" >> "$1" # append this file to the end of the merged data file
2021-10-21 17:04:01 +00:00
else # if file does not exist, reuse STANDARD file
echo "!be24 $standardoffset"
echo "!le16 $standardsize"
fi
done < "$records") > "$source"
2021-10-23 05:36:04 +00:00
# assemble temp source file into binary OKVS data structure, then output that
out=$(mktemp)
acme -o "$out" "$source"
cat "$out"
# clean up
2021-10-23 05:36:04 +00:00
rm "$out"
rm "$source"
rm "$records"