#!/bin/sh
# License: http://creativecommons.org/publicdomain/zero/1.0/
# (CC0 or Public Domain).  To the extent possible under law, the author,
# Jim Avera (email jim.avera at gmail) has waived all copyright and
# related or neighboring rights to this document.
# Attribution is requested but not required.

# Inspired by https://unix.stackexchange.com/a/20057/559541
# $Id: cvt_png_pdf,v 1.5 2025/04/28 19:19:42 jima Exp jima $

set -u -e

ppi=300
pgwid_inches=8.5
pght_inches=11
border_inches=0.5
outfile=""
quiet=
extra_copts=""

print_help() {
echo "
Convert .png image to single-page PDF file.

USAGE
  $(basename $0) [OPTIONS] input.png

OPTIONS (defaults shown in parenthesis)

  -o outputfile (input filename with suffix changed to .pdf)
  -ppi pixels_per_inch ($ppi)
  -w   page_width_in_inches ($pgwid_inches)
  -h   page_height_in_inches ($pght_inches)
  -b   border_in_inches ($border_inches)
  --   end of options (needed if input filename starts with '-')
"
}

[ $# -gt 0 ] || set -- --help
while [ $# -gt 0 ] ; do
  case "$1" in
    --help) print_help; exit 0 ;;
    -b) border_inches="$2"; shift; shift ;;
    -c) extra_copts="${extra_copts}$2 "; shift; shift ;;
    -h) pght_inches="$2"; shift; shift ;;
    -o)   outfile="$2"; shift; shift ;;
    -ppi|--ppi) ppi="$2"; shift; shift ;;
    -q) quiet=yes; shift ;;
    -w) pgwid_inches="$2"; shift; shift ;;
    -*) echo "Unknown option '$1'" >&2; exit 1 ;;
    --) shift ; break ;;
    *) break ;;
  esac
done
if [ $# -eq 0 ] ; then echo "Missing input .png arg" >&2; exit 2; fi
infile="$1"; shift
if [ $# -ne 0 ] ; then echo "Extraneous argument(s)" >&2; exit 2; fi

case "$outfile" in
  "") outfile=`expr "$infile" : '\(.*\)\.[^.]*'`.pdf ;;
  *.pdf) ;;
  *) echo "Output file name must be something.pdf" >&2; exit 3 ;;
esac

border_pixels=$(perl -wE "print $ppi * $border_inches")
pgwid_pixels=$(perl -wE "print $ppi * $pgwid_inches")
pght_pixels=$(perl -wE "print $ppi * $pght_inches")

imgwid_pixels=$(perl -wE "print $pgwid_pixels - (2 * $border_pixels)")
imght_pixels=$(perl -wE "print $pght_pixels - (2 * $border_pixels)")

if [ -z "$quiet" ] ; then
  echo "# infile  = $infile"
  echo "# outfile = $outfile"
  echo "# ppi     = $ppi"
  echo "# pgwid   = $pgwid_inches in. = $pgwid_pixels pix"
  echo "# pght    = $pght_inches in. = $pght_pixels pix"
  echo "# border  = $border_inches in. = $border_pixels pix"
  echo "# imgwid  = $imgwid_pixels pix"
  echo "# imght   = $imght_pixels pix"
  echo ""
  echo "(set -x;"
fi

echo " convert '$infile' \\"

echo "   -compress jpeg -quality 92 \\"
echo "   -density ${ppi}x${ppi} -units PixelsPerInch \\"
echo "   -resize ${imgwid_pixels}x${imght_pixels} \\"

# I don't understand how to use -extent to center the image
#   see https://github.com/ImageMagick/ImageMagick/discussions/8042
#echo "   -extent ${pgwid_pixels}x${pght_pixels} -gravity center -background purple -compose copy \\"
#echo "   -repage ${pgwid_pixels}x${pght_pixels} \\"

# So instead, doing this.  The result will be at the bottom of the page (but with borders)
echo "   -border ${border_pixels} ${extra_copts}\\" 
echo "   '$outfile'"

if [ -z "$quiet" ] ; then
  echo ")"
fi
echo "# (pipe this to /bin/sh to execute)"
