#!/bin/bash
# Apply CSS style to 'infodoc.html'
#  a) verify existance of 'idpp', 'infodoc.html', 'infodoc-styles.css'
#  b) copy 'infodoc.html' to infodoc_css.html
#  c) invoke 'idpp' Infodoc Post Processor with the specified
#     processing options.
#     - source document: infodoc.html
#     - backup document: infodoc_css.html~
#     - styled document: infodoc_css.html
#  d) exit
# 
# Default processing is to apply the input response file, 'apply_response.txt'.
# If command-line argument(s) are specified, perform alternate processing.
# See below for available options.
# 
# Author: Mahlon R. Smith - The Software Samurai
# Date  : 02-Aug-2025
success=1

if [ -e ./idpp/idpp ]; then

   if [ -e ./infodoc.html ]; then

      if [ -e ./infodoc-styles.css ]; then

         if [ -e apply_response.txt ]; then
            cp --preserve=all infodoc.html infodoc_css.html
            clear
            # No argument: read the response file
            if [ -z $1 ]; then
               ./idpp/idpp -iV --response=apply_response.txt infodoc_css.html
            # 'a' = Auto: full auto processing
            elif [ $1 = 'a' ]; then
               ./idpp/idpp -V infodoc_css.html    # Full automatic mode
            # 'i' = Interactive processing
            elif [ $1 = 'i' ]; then
               ./idpp/idpp -i infodoc_css.html     # Interactive mode
            # 'b' = Automatic processing with Book
            elif [ $1 = 'b' ]; then
               ./idpp/idpp -V --book infodoc_css.html
            # 't' = Automatic processing with modified Table-Of-Contents
            elif [ $1 = 'c' ]; then
               ./idpp/idpp -cV infodoc_css.html
            # 'r' = Stepped scan with optional delay interval
            elif [ $1 = 's' ]; then
               ./idpp/idpp -iV --step=30 --response=apply_response.txt infodoc_css.html
            # '-' = Assume either "--version" or "--help"
            elif [[ "$1" =~ "-" ]]; then
               {
               echo "applycss to infodoc_css.html"
               echo "============================================================"
               echo "no args: process using response file: 'apply_response.txt'"
               echo "'a'    : process in full automatic mode"
               echo "'i'    : process in interactive mode"
               echo "'b'    : process in interactive mode using '--book'"
               echo "'c'    : process in interactive mode with TOC mod"
               echo "'s'    : process using response file with stepped scan/delay"
               echo
               success=0
               }
            fi
            if [ $success = 1 ]; then
            {
               echo '******************************'
               echo '** infodoc_css.html created **'
               echo '******************************'
            }
            fi
         else
            echo 'apply_response.txt not found'
         fi
      else
         echo 'infodoc-styles.css not found'
      fi
   else
      echo 'infodoc.html not found'
   fi
else
   echo 'idpp not found'
fi