#!/bin/bash

function 01_header_latex (){ tex_file="$1" file_ext_main="$2" title="$3"

cat«EOF > “$tex_file” ## Print the tex file header \batchmode \documentclass[titlepage,twoside]{article}

%\usepackage{showframe} %\usepackage[inner=2cm,outer=4cm]{geometry} %\usepackage[]{geometry} \usepackage[inner=2.5cm,outer=2.5cm,bottom=2.5cm]{geometry}

\usepackage{listings} \usepackage[usenames,dvipsnames]{color} %% Allow color names \lstdefinestyle{customasm}{ belowcaptionskip=1\baselineskip, xleftmargin=\parindent, language=$file_ext_main, %% Change this to whatever you write in breaklines=true, %% Wrap long lines basicstyle=\footnotesize\ttfamily, commentstyle=\itshape\color{Gray}, stringstyle=\color{Black}, keywordstyle=\bfseries\color{OliveGreen}, identifierstyle=\color{blue}, %xleftmargin=-8em, }

\usepackage[colorlinks=true,linkcolor=blue]{hyperref}

\begin{document}

% \title{$title} \title{\MakeUppercase{$title}} \author{Reginaldo Pereira Junior} \maketitle

\pagenumbering{roman} \tableofcontents

\newpage \setcounter{page}{1} \pagenumbering{arabic}

EOF

}

function cat_code_latex (){ code="$1"

cat « EOF » “$tex_file”

\newpage \section{$code} \lstinputlisting[style=customasm]{$code} EOF

}

function 03_make_pdf(){ tex_file="$1"

#nome do pdf final
make_pdf="$2" 

echo "\end{document}" >> $tex_file

printf "\nCriando o pdf $pdf_file\n"

pdflatex $tex_file -output-directory . 
pdflatex $tex_file -output-directory . 

}

function 02_src_to_latex (){ tex_file="$1" filesarray="${@:2}"

echo "Creating tex file..."

for i in $filesarray
do
	echo "Adicionando o arquivo: $i"
	cat_code_latex "$i"
done

}

function help_user(){ printf “\n\nUso: ${0#*/} <file_type_LaTex_Representation>” printf " "<Title_PDF>" <main.file> <second.file> <N.file | *.file >\nExecutado: $@ \n "

}

if [[ $1 == ’’ ]]; then

help_user
exit 1

else help_user printf “\n\n\nExecutando…\n\n\n” pdf_file="$2".pdf tmp_file="mktemp" file_type_tex="$1"

01_header_latex "$tmp_file" "$file_type_tex" "$2"

#02_src_to_latex "$tmp_file" "${@:3}" 

files="$3"
for i in ${@:3} ; do 
	[ "$i" != "$3" ] && files="$files $i" 
done

02_src_to_latex "$tmp_file" "$files" 

03_make_pdf "$tmp_file" "$pdf_file"

mv -v tmp.pdf "$pdf_file" 2>/dev/null
rm tmp.{aux,log,out,toc,pdf}  &>/dev/null
# rm $tmp_file &>/dev/null

fi