#!/usr/bin/env bash
#
# PDF Fonts Embedder
# Copyright (C) 2009-2026 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no
#

# Bash options:
set -euo pipefail


# ------ BEGIN-OF-USAGE-BLOCK ------
# ###### Usage ##############################################################
usage () {
   local program="$1"
   local exitCode="$2"
   local header=$'\x1b[4m'
   local cmd=$'\x1b[1m'
   local opt=$'\x1b[1m'
   local arg=$'\x1b[4m'
   local syn=$'\x1b[1m'
   local reset=$'\x1b[0m'
   cat >&2 <<EOF
${header}Usage:${reset}
${cmd}${program}${reset} ${arg}input_pdf${reset} ${arg}output_pdf${reset}
	${syn}[${reset}${opt}-O${reset} ${syn}|${reset} ${opt}--optimise${reset} ${syn}|${reset} ${opt}--optimize${reset}${syn}]${reset}
${cmd}${program}${reset}
	${syn}[${reset}${opt}-h${reset} ${syn}|${reset} ${opt}--help${reset}${syn}]${reset}
${cmd}${program}${reset}
	${syn}[${reset}${opt}-v${reset} ${syn}|${reset} ${opt}--version${reset}${syn}]${reset}
EOF
   exit "${exitCode}"
}
# ------ END-OF-USAGE-BLOCK ------


# ###### Version ############################################################
version () {
   echo "System-Info @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
   exit 0
}



# ###### Main program #######################################################

# ====== Handle arguments ===================================================
UNAME="$(uname)"
case "${UNAME}" in
   FreeBSD)
      GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
      ;;
   NetBSD)
      GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
      ;;
   OpenBSD)
      GETOPT="$(command -v gnugetopt || true)"
      ;;
   SunOS)
      GETOPT="$(PATH="/usr/gnu/bin:${PATH}" command -v getopt || true)"
      ;;
   Darwin)
      GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
      ;;
   *)
      GETOPT="$(command -v getopt || true)"
      ;;
esac
if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
   echo >&2 "ERROR: Enhanced/GNU getopt is required!"
   exit 1
fi
options="$(${GETOPT} -o Ohv --long optimise,optimize,help,version -a -- "$@")"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
   usage "$0" 1
fi

OPTIMISE=0
eval set -- "${options}"
while [ $# -gt 0 ] ; do
   case "$1" in
      -O | --optimise | --optimize)
         OPTIMISE=1
         shift
         ;;
      -h | --help)
         usage "$0" 0
         # shift
         ;;
      -v | --version)
         version
         ;;
      --)
         shift
         break
         ;;
  esac
  shift
done
if [ $# -ne 2 ] ; then
   usage "$0" 1
fi
INPUT_PDF="$1"
OUTPUT_PDF="$2"
if [ ! -e "${INPUT_PDF}" ] ; then
   echo >&2 "ERROR: PDF file ${INPUT_PDF} does not exist!"
   exit 1
fi


# ====== Perform conversion =================================================

# ------ Using PDF/X --------------------------------------------------------
if [ ${OPTIMISE} -eq 1 ] ; then
   ( ps2pdf -dPDFSETTINGS=/prepress -dCompatibilityLevel=1.7 "${INPUT_PDF}" "${OUTPUT_PDF}"-tmp.pdf && qpdf --linearize "${OUTPUT_PDF}"-tmp.pdf "${OUTPUT_PDF}" ) || cp "${INPUT_PDF}" "${OUTPUT_PDF}"
   rm -f "${OUTPUT_PDF}"-tmp.pdf
else
    ps2pdf -dPDFSETTINGS=/prepress -dCompatibilityLevel=1.7 "${INPUT_PDF}" "${OUTPUT_PDF}"
fi
