#!/bin/bash
#set -x

#Variables
TEMPDIR=$(mktemp -d /tmp/PDFdiff_XXXX)
METHOD="html"
OUTPUT="1"
echo "---"
echo "$TEMPDIR"
echo "---"


function usage(){
# Purpose: Shows the usage 
# Input:   nothing
# Output:  prints the usage string

echo "----------------------------------------------------------------------------------------"
echo " "
echo "pdfdiff look for differences between two PDFs."    #This function need no arguments.
echo " "
echo "pdfdiff -option  PDF_FILE_1 PDF_FILE_2"
echo " option = --html|-H, --text|-T "
echo " "
echo " --html = Convert the PDF into HTML and look for differences between two HTML-Documents."
echo " --txt = Convert the PDF into text and look for differences between two txt-Documents."
echo " "
echo "----------------------------------------------------------------------------------------"
exit 0
}


function f_pdforhtml(){
# Purpose: Decides the kind of converting (pdf or html)
# Input:   
#	$1: Includes the information about the Method
#	$2: Is the path of a PDF file
#	$3: Is a different path of a PDF file
#	$4: Is the path of the temp directory
#	$5: Is the Information which kind of output is needed
#	$6: The output will be saved in this directory.
# Output:
#	$1: The file
#	$2: The second file
#	$3: Contains the informations about the method
#	$4: Contains the temp directory
#	$5: Contains the Information about the output 
#	$6: Contains the path for the output




#This function received the following variables: METHOD, PDF1, PDF2, TEMPDIR, OUTPUT, OUTPUTPATH.
local METHOD=$1 PDF1=$2 PDF2=$3 TEMPDIR=$4 OUTPUT=$5 OUTPUTPATH=$6

if [[ "text" = $METHOD ]]; then   #Wenn METHOD = text; dann werden die PDFs konvertiert in text.
     
      f_pdftotext "$PDF1" "$PDF2" "$METHOD" "$TEMPDIR" "$OUTPUT" "$OUTPUTPATH"
      #The function send the following variables to f_pdftotext: PDF1&2, METHOD, TEMPDIR, OUTPUT, OUTPUTPATH.     

elif [[ "html" = $METHOD ]]; then   #Wenn METHOD = html; dann werden die PDFs konvertiert in HTML.
      
      f_pdftohtml "$PDF1" "$PDF2" "$METHOD" "$TEMPDIR" "$OUTPUT" "$OUTPUTPATH"
      #The function send the following variables to f_pdftotext: PDF1&2, METHOD, TEMPDIR, OUTPUT, OUTPUTPATH.
fi

if [ $? = 2 ]; then
     echo "The operation was not successful" 
     exit 6
fi
}



function f_pdftotext(){
#Purpose: Converting the PDFs into txt.
#Input:
#	$1: Includes the method options
#	$2: Stores the Path of the first file
#	$3: Stores the Path of the second file
#	$4: Contains the temp directory
#	$5: Includes the output options
#	$6: Stores the path for the output
#Output:
#       $1: The converted file
#       $2: The converted second file
#       $3: Includes the method options
#       $4: Contains the temp directory
#       $5: Includes hte output options



#This fucntion received the following variables from f_pdforhtml.
local PDF1=$1 PDF2=$2 METHOD=$3 TEMPDIR=$4 OUTPUT=$5    

	local PDF_FILENAME1=$(basename $PDF1)

	   pdftotext -layout  $PDF1  $TEMPDIR/${PDF_FILENAME1%.*}.txt
           #Convert the PDF into txt

        local PDF_FILENAME2=$(basename $PDF2)
   
           pdftotext -layout  $PDF2  $TEMPDIR/${PDF_FILENAME2%.*}.txt
           #Convert the PDF into txt

   f_diff "$PDF1" "$PDF2" "$METHOD" "$TEMPDIR" "$OUTPUT"
   #Send the following variables to f_diff: PDF1&2 METHOD TEMPDIR OUTPUT
}


#This is the function converts the PDFs into HTML.
function f_pdftohtml(){

#Purpose: Converting the PDFs into HTML.
#Input:
#       $1: Includes the method options
#       $2: Stores the Path of the first file
#       $3: Stores the Path of the second file
#       $4: Contains the temp directory
#       $5: Includes the output options
#       $6: Stores the path for the output
#Output:
#       $1: The converted file
#       $2: The converted second file
#       $3: Includes the method options
#       $4: Contains the temp directory
#       $5: Includes hte output options


	#This function received the following variables from f_pdforhtml
        local  PDF1=$1 PDF2=$2 METHOD=$3  TEMPDIR=$4 OUTPUT=$5 
 
        local PDF_FILENAME=$(basename $1)
        
        echo "{$1} {$PDF_FILENAME}"
        
        # -----------
        #mkdir -v $TEMPDIR/${PDF_FILENAME%.html}
        pdftohtml -s "$1"  "$TEMPDIR/${PDF_FILENAME}"
               
        local PDF_FILENAME2=$(basename $2)
        
        echo "{$2} {$PDF_FILENAME2}"

        # -----------
        #mkdir -v $TEMPDIR/${PDF_FILENAME2%.html}
        pdftohtml -s "$2"  "$TEMPDIR/${PDF_FILENAME2}"

        f_diff "$TEMPDIR/${PDF_FILENAME}-html.html" "$TEMPDIR/${PDF_FILENAME2}-html.html" "$OUTPUT" 
	#f_pdftohtml send this variables to f_diff
 
}


function f_diff(){

#Purpose: Make the diff between the two converted pdfs..
#Input:
#       $1: Stores the Path of a temp directory with the converted pdf file
#       $2: Stores the Path of a temp directory with the converted pdf file
#       $3: Saves the output informations
#Output: Show the result on the screen

#f_diff reveived the following variables from f_pdftotext or f_pdftohtml
local FILE1=$1 FILE2=$2 OUTPUT=$3 
if [[ $OUTPUT = "" ]]; then

   echo "The Output will be saved in the file $OUTPUT"
 
   diff -u "${FILE1}" "${FILE2}"

 else   

   diff -u "${FILE1}" "${FILE2}" > $OUTPUT
             
   
 fi
}



#Initializing of the options for the getopts switch case
if ! options=$(getopt -o hHTo: -l help,html,text,output: -- "$@")
 then
    exit 1
fi


eval set -- "$options"
while [ $# -gt 0  ]
 do                #getopts switch case to choose an option       
  case "$1" in                                                    
    -h|--help) usage;;                                            #-h||--help open the help page.
    -H|--html) METHOD="html" ;;                                   #-H||--html choose the html converting process.
    -T|--text) METHOD="text" ;;                                   #-T||--text choose the txt converting process.
    -o|--output) OUTPUT="$2"; shift;;                             #-o||--output write the results in a file.
    (--) shift; break;;                                           
    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;; 
    (*)  break;;
  esac
 shift
done

echo "Danach $@, $#"
echo "METHOD=$METHOD, OUTPUT=$OUTPUT"


#-----
OLD_PDF="$1"
NEW_PDF="$2"
OUTPUTPATH="$3"
#This are variables for the primary process
#-----


# This block controll the paths. It's not possible to use pdfdiff with only one pdf. You need to PDFs.
if [ $# -ne 2 ]; then
  echo "pdfdiff need two .pdf files"; exit 1;
        fi

if [ $# = 2 ]; then
  echo "---"
  echo "pdfdiff checked your files for differences"
  echo "---"       
fi



f_pdforhtml "$METHOD" "$OLD_PDF" "$NEW_PDF" "$TEMPDIR" "$OUTPUT" "$OUTPUTPATH"
#This is the activation of the f_pdforhtml function to start the primary process



rm -rf $TEMPDIR
#This delete the temporaly directory

exit 0


