|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#
|
|
|
|
# pdfattach --- embed specified file(s) in a specified PDF file
|
|
|
|
# Requires pdfLaTeX and attachfile.sty package to run
|
|
|
|
# Returns 0 on success or >0 on error
|
|
|
|
#
|
|
|
|
# Written by Tigran Aivazian <tigran@bibles.org.uk>
|
|
|
|
#
|
|
|
|
|
|
|
|
progname=$(basename $0)
|
|
|
|
|
|
|
|
function usage()
|
|
|
|
{
|
|
|
|
echo "Usage: $progname -o file.pdf file1.djvu [file2.mp3] ..."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! getopts ":o:" opt); then
|
|
|
|
echo "$progname: Missing options." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! type pdflatex > /dev/null 2>&1 ; then
|
|
|
|
echo "$progname: pdfLaTeX program is required." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! kpsewhich attachfile.sty > /dev/null 2>&1 ; then
|
|
|
|
echo "$progname: attachfile.sty package is required." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
declare outfile=""
|
|
|
|
declare -a infiles=()
|
|
|
|
declare -i infcount=0 outfcount=0
|
|
|
|
|
|
|
|
while getopts ":o:" opt; do
|
|
|
|
case $opt in
|
|
|
|
o)
|
|
|
|
outfile=$(readlink -f "$OPTARG")
|
|
|
|
((outfcount++))
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "$progname: Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "$progname: Option -$OPTARG requires an argument." >&2
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
|
|
numargs=$#
|
|
|
|
for ((i=1 ; i <= $numargs ; i++))
|
|
|
|
do
|
|
|
|
fullname=$(readlink -f "$1")
|
|
|
|
if [ ! -f "$fullname" ] ; then
|
|
|
|
echo "$progname: file \"$fullname\" does not exist" >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
infiles[$infcount]="$fullname"
|
|
|
|
((infcount++))
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
if ((infcount == 0)) ; then
|
|
|
|
echo "$progname: No input file(s) specified." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ((outfcount != 1)) ; then
|
|
|
|
echo "$progname: One (and only one) output file must be specified." >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
workdir=$(mktemp --tmpdir -d pdfattach.XXXXXX)
|
|
|
|
cd $workdir
|
|
|
|
> tmp.tex
|
|
|
|
echo -E "\documentclass{book}" >> tmp.tex
|
|
|
|
echo -E "\usepackage{attachfile}" >> tmp.tex
|
|
|
|
echo -E "\begin{document}" >> tmp.tex
|
|
|
|
echo -E "\pagestyle{empty}" >> tmp.tex
|
|
|
|
for ((i = 0 ; i < ${#infiles[*]} ; i++));
|
|
|
|
do
|
|
|
|
echo "\attachfile{${infiles[$i]}}" >> tmp.tex
|
|
|
|
done
|
|
|
|
echo -E "\end{document}" >> tmp.tex
|
|
|
|
pdflatex -halt-on-error tmp.tex > /dev/null && mv tmp.pdf "$outfile"
|
|
|
|
cd - > /dev/null
|
|
|
|
rm -rf $workdir
|