#!/usr/bin/env bash

#------------------------------------------------------
display_help()
{
    echo "Used to transform tex file into a PDF"
    echo ""
    echo "-f: Path to latex file"
    echo "-i: Path to directory with images, default \"images\""
    echo "-o: Path to output file, default, same as input but with pdf extension"
    echo "-c: Path to tex file that has to be inserted, e.g. with definitions, optional. Can be space separated and doublequoted set of paths"
}
#------------------------------------------------------
get_opts()
{
    if [[ $# -eq 0 ]]; then
        display_help
        exit 1
    fi  

    while getopts :hf:f:i:o:c: option; do 
        case "${option}" in
            f)  FPATH=${OPTARG};;
            i)  IPATH=${OPTARG};;
            o)  OPATH=${OPTARG};;
            c)  CPATH=${OPTARG};;
            h)  display_help
                exit 0
                ;;  
           \?)  echo "Invalid option: -${OPTARG}"
                display_help
                exit 1
                ;;  
            :)  echo "$0: Arguments needed"
                display_help
                exit 1
                ;;  
        esac
    done

    if [[ ! -z $CPATH ]];then
	IFS=' ' read -ra CPATH <<< "$CPATH"
    fi
}
#------------------------------------------------------
check_inputs()
{
    if [[ ! -f $FPATH ]];then
	echo "$FPATH not found"
	exit 1
    fi

    if [[ -z $OPATH ]];then
	OPATH=$(echo $FPATH | sed 's|\.tex|\.pdf|g')
    else
	ODIR=$(dirname $OPATH)
	mkdir -p $ODIR
    fi
}
#------------------------------------------------------
create_tex()
{
    TPATH=input.tex

    echo "\documentclass{article}" > $TPATH

    echo "\usepackage{tikz}"       >> $TPATH
    echo "\usepackage{xcolor}"       >> $TPATH
    echo "\usepackage{graphicx}"       >> $TPATH
    echo "\usepackage{subcaption}"     >> $TPATH
    echo "\usepackage{float}" >> $TPATH
    echo "\usepackage{amssymb}" >> $TPATH
    echo "\usepackage{bm}" >> $TPATH
    echo "\usepackage{epigraph}" >> $TPATH
    echo "\usepackage{ifthen}" >> $TPATH
    echo "\usepackage{caption}" >> $TPATH
    echo "\usepackage{subcaption}" >> $TPATH
    echo "\usepackage{listings}" >> $TPATH
    echo "\usepackage{amsmath}" >> $TPATH
    echo "\usepackage{fvextra}" >> $TPATH
    echo "\usepackage{textpos}" >> $TPATH
    echo "\usepackage{booktabs}" >> $TPATH
    echo "\usepackage{ifthen}" >> $TPATH
    echo "\usepackage{rotating}" >> $TPATH
    echo "\usepackage{courier}" >> $TPATH
    echo "\usepackage{pgffor}" >> $TPATH
    echo "\usepackage[section]{placeins}" >> $TPATH
    echo "\usepackage{longtable}" >> $TPATH
    echo "\usepackage{multirow}" >> $TPATH
    echo "\usepackage{hyperref}" >> $TPATH

    if [[ ! -z $IPATH ]];then
	echo "\graphicspath{ {$IPATH} }" >> $TPATH
    fi


    if [[ ! -z $CPATH ]];then
	for COMPATH in ${CPATH[@]};do
	    echo "\input{$COMPATH}" >> $TPATH
	done
    fi

    echo "\begin{document}" >> $TPATH

    cat $FPATH >> $TPATH

    echo "\end{document}" >> $TPATH
}
#------------------------------------------------------
make_links()
{
    for OBJ in `find $BASEDIR -maxdepth 1`;do
	NAME=$(basename $OBJ)
	ln -s $OBJ $BUILDDIR/$NAME
    done
}
#------------------------------------------------------
build()
{
    BASEDIR=$PWD
    BUILDDIR=$BASEDIR/.cache/tex_to_pdf

    mkdir -p $BUILDDIR 
    cd $BUILDDIR 
    rm -rf *

    make_links
    create_tex

    pdflatex $TPATH

    if [[ $? -ne 0 ]];then
	return
    fi

    pdflatex $TPATH

    ODIR=$(dirname  $OPATH)

    ls .
    echo "cp $PWD/input.pdf $ODIR"
    cp $PWD/input.pdf $ODIR
}
#------------------------------------------------------
get_opts "$@"
check_inputs
build

