Sleds/utils/convert_multipage_library_to_fml.sh

93 lines
1.8 KiB
Bash
Raw Normal View History

2025-03-13 21:28:38 +00:00
#!/bin/bash
# Usage: convert_multipage_library_to_fml.sh [--validate] {srcroot} {destroot}
if [ "$1" = "-validate" ]; then
validate=true
shift
fi
SRC=$1
DEST=$2
function usage(){
msg=$1
echo "ERROR: $msg"
echo "Usage: convert_multipage_library_to_fml.sh {srcroot} {destroot}"
exit 1
}
if [ ! -d "${SRC}" ]; then
usage "Source directory is required"
fi
if [ ! -d "${DEST}" ]; then
usage "Destination directory is required"
fi
if [ "${SRC}" = "${DEST}" ]; then
usage "Source and destination may not be the same directory"
fi
echo "=== Processing source multipage documents from: ${SRC} ==="
prevdt=""
cvtargs="-density 300 -alpha off"
find "${SRC}" -type f | while read file; do
doctype=`dirname "$file"`
doctype=`basename "$doctype"`
out="${DEST}/${doctype}"
fn=`basename "$file"`
extn="${fn##*.}"
fn="${fn%.*}"
outfn="${out}/${fn}-%04d.pdf"
if [ "${prevdt}" != "${doctype}" ]; then
echo ">>> $doctype"
fi
prevdt="${doctype}"
echo " SRC $file"
echo " => $outfn"
#echo "fn: ${fn} extn: ${extn}"
ok=`echo "$extn" | awk '/pdf|PDF|tif|tiff|TIF|TIFF/ {print $0; next}'`
if [ "$ok" = "" ]; then
echo "*** ERROR The input file '$file' is not a pdf or TIFF file"
continue
fi
# validate (only)
if [ "$validate" = "true" ]; then
continue
fi
# create the output root
mkdir -p "$out"
# and the FML sub-dirs
mkdir -p "$out/first"
mkdir -p "$out/middle"
mkdir -p "$out/last"
# do the conversion
convert ${cvtargs} "${file}" "${outfn}"
# move pages to fml
first=true
last=""
ls "${out}"/"${fn}"-*.pdf | (while read converted; do
if [ $first = true ]; then
mv "$converted" "$out/first"
first=false
else
mv "$converted" "$out/middle"
base=`basename "$converted"`
last="$out/middle/$base"
fi
done
if [ "$last" != "" ]; then
mv "$last" "$out/last"
fi
)
done
exit 0;