#!/bin/bash
set -e

# Define paths
CURDIR=`pwd`
SRC=$CURDIR/allPapers-preHeader
DES=$CURDIR/proceedings-withHeader
TEM=`mktemp -d -t conference-XXXXXX`
TEM2=$CURDIR/headerFooterGeneration


# Delete and recreate destination directory
rm -rf $DES
mkdir $DES

# Get list of all papers
papLis=`cd $SRC && find . -name '*.pdf' -print`

# Variables for page counter.
nPageSta=0
nPageEnd=0
nAdd=0

for pa in $papLis; do
    echo "**************************************"
    echo "*** Converting $pa"
    echo "**************************************"
    rm -rf $TEM
    mkdir -p $TEM

    cp $SRC/$pa $TEM/$pa

    #############################
    # Get page numbers using PDFTK
    #############################
    let nPageSta=nPageEnd+1
    cd $TEM
    echo "--- Dumping data"
    pdftk $pa dump_data output data.txt
    grep "NumberOfPages:" data.txt > data2.txt
    sed -e 's/NumberOfPages://g' data2.txt > data3.txt
    iPag=`cat data3.txt`
    # iPag is the number of pages of the current PDF document
    nPageEnd=$((nPageSta + iPag -1))
    echo "    Page numbers: $nPageSta - $nPageEnd  ($iPag pages)"
    cd $CURDIR

    #############################
    # Make latex document
    #############################
    mkdir -p $TEM2
    cd $TEM2
    nAdd=$((nPageSta - 1))
    # Add your LaTeX code here...
    echo "\documentclass[10pt, a4paper]{article}" > pageHeaderFooter.tex
    echo "\begin{document}" >> pageHeaderFooter.tex
    # Tell  LaTeX to create page numbers
    echo "\addtocounter{page}{${nAdd}}" >> pageHeaderFooter.tex

    COUNTER=0
    while [  $COUNTER -lt $iPag ]; do
	echo "~ \clearpage" >> pageHeaderFooter.tex
	let COUNTER=COUNTER+1
    done
    echo "\end{document}" >> pageHeaderFooter.tex

    echo "    Starting pdfLaTeX to generate PDF file with page numbers. In case of errors, enter x RETURN to quit."
    # Start pdflatex and write output to file out.txt instead of console...
    pdflatex pageHeaderFooter.tex > out.txt
    rm out.txt
    # Use the line below for debugging instead...
    #pdflatex pageHeaderFooter.tex

    #############################
    # Merge documents
    #############################
    cd $TEM
    cp $TEM2/pageHeaderFooter.pdf .
    echo "--- Bursting paper"
    pdftk $pa burst output paper_%03d.pdf
    echo "--- Bursting header"
    pdftk pageHeaderFooter.pdf burst output header_%03d.pdf

    # Assemble pdf
    COUNTER=1
    echo "--- Adding background"
    while [  $COUNTER -le $iPag ]; do
	printf -v COUNTER_PADDED "%03d" $COUNTER
	pdftk paper_${COUNTER_PADDED}.pdf background header_${COUNTER_PADDED}.pdf output out_${COUNTER_PADDED}.pdf
	let COUNTER=COUNTER+1
    done
    cp out_001.pdf $pa
    COUNTER=2
    echo "--- Merging paper"
    while [  $COUNTER -le $iPag ]; do
	printf -v COUNTER_PADDED "%03d" $COUNTER
	pdftk $pa out_${COUNTER_PADDED}.pdf cat output temp.pdf
	mv temp.pdf $pa
	let COUNTER=COUNTER+1
    done
    cp $pa $DES
    cd $CURDIR
done

rm -f $DES/data.txt $DES/data2.txt $DES/data3.txt
rm -fr $TEM2

#############################
# Merge all papers
#############################
cd $DES
pdftk $papLis cat output proceedings.pdf
