#!/bin/bash

# splitpdf
# This is a BASH script.
# Should work with Linux or Mac OSX.
# FREE SOFTWARE, WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK.
# Copyright 2018 Robert Allgeyer.
# This file may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3c
# of this license or [at your option] any later version.
# The latest version of this license is in
#    http://www.latex-project.org/lppl.txt
# and version 1.3c or later is part of all distributions of LaTeX
# version 2005/12/01 or later.
# This file is distributed with the "novel" LuaLaTeX document class.
# https://ctan.org/pkg/novel  [get the one zip archive]
# But you do not need a TeX installation to use this script.

THISVER="1.4"
VERMSG="splitpdf version $THISVER."
USAGEMSG="Usage: ./splitpdf filename.pdf"
HELPMSG="Help:  ./splitpdf -h"

# Provides version, using -v or --v or equivalent:
if [ "$1" == "" ] || [ "$1" == "-v" ] || [ "$1" == "--v" ] || [ "$1" == "-version" ] || [ "$1" == "--version" ] || [ "$1" == "-V" ] || [ "$1" == "--V" ]
then
  echo "$VERMSG"
  echo 
  exit 0
fi

# Provides help, using -h or --h or equivalent:
if [ "$1" == "-h" ] || [ "$1" == "--h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]
then
  echo 
  echo "$VERMSG"
  echo "WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK."
  echo "Splits a multi-page pdf into one or more individual pages."
  echo 
  echo "$USAGEMSG"
  echo "  The extension may be pdf or PDF."
  echo "  Input file must be located in the input folder."
  echo 
  echo "The script first locates Ghostscript, then counts the pdf pages."
  echo "First page is 1, no matter how they are numbered within the pdf."
  echo 
  echo "You will be asked for the first and last page numbers to split."
  echo "  The split includes the given numbers."
  echo "  If the numbers are identical, then only that one page is split."
  echo "Split pages appear in the input folder."
  echo 
  echo "See novel-scripts-README.html for more information."
  echo 
  exit 0
fi

# Welcome message:
echo "Splits a multi-page pdf into one or more individual pages."
echo "WITHOUT WARRANTY EXPRESS OR IMPLIED. USE AT OWN RISK."

if [ ! -d "temp" ]; then mkdir temp; fi
if [ ! -d "output" ]; then mkdir output; fi

# Check input filename:
if [ "$1" == "" ]
then
  echo 
  echo "You did not provide an input filename."
  echo "$USAGEMSG"
  echo "$HELPMSG"
  echo 
  exit 1
else
  if [ -f "input/$1" ]
  then
    FN="$1"
  else
    echo 
    echo "Could not find $1 in input folder."
    echo "$USAGEMSG"
    echo "$HELPMSG"
    echo 
    exit 1
  fi
fi


# Check input filename extension:
CN=$(basename "input/$FN")
CE="${CN##*.}"
CN="${CN%.*}"
if [ "$CE" == "pdf" ] || [ "$CE" == "PDF" ]
then
  :
else
  echo "ERROR."
  echo "Input file does not have allowable file extension pdf or PDF."
  echo "$HELPMSG"
  echo 
  exit 1
fi


# Locate Ghostscript:
printf "Checking for Ghostscript... "
gs -v 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]
then
  echo "ERROR."
  echo 
  echo "This script requires Ghostscript, but did not find it."
  echo "See novel-scripts-README.html for more information."
  echo 
  exit 1
else
  echo "OK."
fi
cd input
gs -q -dNODISPLAY -c "($FN) (r) file runpdfbegin pdfpagecount = quit" >../temp/temp-identify.txt
PDFCOUNT=$(cat ../temp/temp-identify.txt)
cd ../
if [ -f "temp/temp-identify.txt" ]; then rm temp/temp-identify.txt; fi

#let PDFCOUNT++

echo 

if [ $PDFCOUNT -gt 1 ]
then
  echo "This pdf has $PDFCOUNT pages. The first page is always 1."
  read -p "First page to split, answer 1 to $PDFCOUNT : " BEGPAGE
  if [ $BEGPAGE -lt 1 ]
  then
    echo "Cannot be less than page 1. Start over."
    echo 
    exit 1
  fi
  if [ $BEGPAGE -gt $PDFCOUNT ]
  then
    echo "Cannot be greater than page $PDFCOUNT. Start over."
    echo 
    exit 1
  fi

  read -p "Last page to split, answer $BEGPAGE to $PDFCOUNT : " ENDPAGE

  if [ $ENDPAGE -lt $BEGPAGE ]
  then
    echo "Cannot be less than page $BEGPAGE. Start over."
    echo 
    exit 1
  fi
  if [ $ENDPAGE -gt $PDFCOUNT ]
  then
    echo "Cannot be greater than page $PDFCOUNT. Start over."
    echo 
    exit 1
  fi

  let SUMPAGE=BEGPAGE-1
  MYPLUS="plus"

  cd input
  gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=$BEGPAGE -dLastPage=$ENDPAGE -sOutputFile=temp-split-range.pdf $FN 1>/dev/null
  gs -sDEVICE=pdfwrite -dSAFER -o $CN-$SUMPAGE$MYPLUS%d.pdf temp-split-range.pdf 1>/dev/null

  if [ -f "temp-split-range.pdf" ]; then rm temp-split-range.pdf; fi
  cd ../
  echo 
  echo "Done! The split pages are in the input folder."
  echo 
else
  echo "That pdf file only has one page. No need to split it. No output generated."
  echo 
fi

exit 0
# end of file

