#!/bin/sh -x

dn=$(dirname $0)
if [ "${dn}" = "." ]; then
  echo "Run the build in a subdirectory or in an out-of-source directory"
  exit 1
fi

topdir=$(readlink -f ${dn})
builddir=$(readlink -f .)
papersdir=${builddir}/papers
latex_templates=${topdir}/instructions/LaTeXTemplates.zip

die() {
    echo >&2 "$1"
    exit 1
}

build_from_source() {
    local project project_dir main_tex_file other_files bib_file ok
    project_dir="$1"
    project=$(echo ${project_dir} | tr '/' '_')
    main_tex_file="$2"
    [ -f "${topdir}/contributions/${project_dir}/${main_tex_file}" ] || die "invalid call to build_from_source()"
    shift; shift;
    other_files="$@"

    if [ ! -d ${project_dir} ]; then
	mkdir -p ${project_dir}
	cd ${project_dir}

	cp ${topdir}/contributions/${project_dir}/${main_tex_file} ./${project}.tex \
	    && for f in ${other_files}; do \
	           cp -a ${topdir}/contributions/${project_dir}/${f} .; done \
	    && unzip ${latex_templates} \
	    && pdflatex ${project} \
	    && if grep bibdata ${project}.aux; then
	           unzip -j BibTeX/iopart-num.zip iopart-num/iopart-num.bst
		   bibtex ${project}
		   pdflatex ${project}
	       fi \
	    && pdflatex ${project} \
	    && ln -sf ${builddir}/${project_dir}/${project}.pdf ${papersdir}

	cd -
    fi
}

link_pdf() {
    local project project_dir pdf_file
    project_dir="$1"
    project=$(echo ${project_dir} | tr '/' '_')
    pdf_file="$2"
    [ -f "${topdir}/contributions/${project_dir}/${pdf_file}" ] || die "invalid call to link_pdf()"

    ln -s "${topdir}/contributions/${project_dir}/${pdf_file}" ${papersdir}/${project}.pdf
}

if [ ! -d ${papersdir} ]; then
    mkdir -p ${papersdir}
fi

cd ${builddir}

# prepare cover
#link_pdf cover cover.pdf
#link_pdf experiment experiment.pdf
#link_pdf datacenter datacenter.pdf
#link_pdf research research.pdf
#link_pdf transfer transfer.pdf
#link_pdf additional additional.pdf

build_from_source user-support main.tex *.PNG
build_from_source ams AMS-report-2019.tex  AMS_nuovo.pdf  contributors.pdf  He-MC.pdf  input_output.jpg  production_jobs.jpg
build_from_source alice main.tex *.png 
build_from_source atlas    atlas.tex
build_from_source borexino Borexino_CNAFreport2018.tex
build_from_source cms report-cms-feb-2019.tex tier1-jobs-2018.pdf tier1-readiness-2018.pdf
link_pdf belle Cnaf-2019-5.0.pdf
#build_from_source cosa      cosa.tex  biblio.bib beegfs.PNG
build_from_source cnprov cnprov.tex
build_from_source cta CTA_annualreport_2018_v1.tex *.eps
link_pdf cuore cuore.pdf
build_from_source cupid    main.tex cupid-biblio.bib 
build_from_source dampe    main.tex *.jpg *.png
link_pdf darkside ds-annual-report-2019.pdf
#build_from_source eee eee.tex  EEEarch.eps  EEEmonitor.eps  EEEtracks.png  ELOGquery.png  request.png
#build_from_source exanest   exanest.tex  biblio.bib monitoring.PNG storage.png
#build_from_source fazia fazia.tex
build_from_source fermi fermi.tex
build_from_source gamma gamma.tex
build_from_source icarus report_2018.tex *.png
#build_from_source gerda    gerda.tex *.pdf
#build_from_source glast 	glast.tex 
link_pdf juno juno-annual-report-2019.pdf
build_from_source km3net km3net.tex compmodel.png  threetier.png
build_from_source na62 main.tex 
build_from_source newchim repnewchim18.tex fig1.png
build_from_source lhcb lhcb.tex *.png
build_from_source lhcf  lhcf.tex
build_from_source limadou limadou.tex
#build_from_source lowcostdev lowcostdev.tex *.jpg
#build_from_source lspe lspe.tex biblio.bib lspe_data_path.pdf
build_from_source virgo AdV_computing_CNAF.tex
build_from_source xenon main.tex xenon-computing-model.pdf

build_from_source sc18 SC18.tex *.png

#build_from_source mw-esaco     mw-esaco.tex *.png
#build_from_source mw-kube      mw-kube.tex
#build_from_source mw-cdmi-storm mw-cdmi-storm.tex *.png *.jpeg
#build_from_source mw-software  mw-software.tex
#build_from_source mw-iam       mw-iam.tex

#build_from_source na62	 na62.tex
link_pdf padme 2019_PADMEcontribution.pdf
#build_from_source xenon xenon.tex  xenon-computing-model.pdf
build_from_source sysinfo  sysinfo.tex *.png
#link_pdf virgo VirgoComputing.pdf

#build_from_source tier1 tier1.tex
#build_from_source flood theflood.tex *.png
build_from_source HTC_testbed HTC_testbed_AR2018.tex
build_from_source farming ARFarming2018.tex *.png *.jpg
#build_from_source dynfarm dynfarm.tex 
#build_from_source storage storage.tex *.png Huawei_rack.JPG
#build_from_source seagate seagate.tex biblio.bib  *.png *.jpg
#build_from_source dataclient dataclient.tex
#build_from_source ltpd ltpd.tex *.png
build_from_source net main.tex *.png
#build_from_source ssnn1 ssnn.tex *.jpg
#build_from_source ssnn2 vmware.tex *.JPG *.jpg

#build_from_source infra Chiller.tex  chiller-location.png
build_from_source audit Audit-2018.tex
#build_from_source cloud_cnaf cloud_cnaf.tex *.png
build_from_source dmsq dmsq2018.tex ar2018.bib
#build_from_source st StatMet.tex sm2017.bib
#build_from_source cloud_a cloud_a.tex *.pdf
#build_from_source cloud_b cloud_b.tex *.png *.jpg
#build_from_source cloud_c cloud_c.tex *.png *.pdf
#build_from_source cloud_d cloud_d.tex *.png 

build_from_source pett pett.tex bibliopett.bib
#build_from_source iso iso.tex 27001.png  biblioiso.bib

pdflatex ${topdir}/cnaf-annual-report-2018.tex \
&& pdflatex ${topdir}/cnaf-annual-report-2018.tex 2> /dev/null \
&& pdflatex ${topdir}/cnaf-annual-report-2018.tex 2> /dev/null
