#!/bin/bash


# make sure latexindent.pl is installed and in path

CMD=latexindent.pl
if [[ ! "$(which $CMD 2>/dev/null)" ]] ; then
    echo I cannot find latexindent.pl, aborting
    exit
fi

INFILE="$1" 
echo INDENTING $INFILE

THISDIR=$(dirname "$0")
LFCW_SETTINGS="$THISDIR"/lfindentsettings.yaml
INDENTOPTS="--overwrite --local="$LFCW_SETTINGS" --silent"

# ACTUAL WORK!
$CMD $INDENTOPTS $INFILE 
rm indent.log

# If only tex files this good
#BAKFILE=$(basename $INFILE .tex).bak
# If we also do .inc files, have to do this:
BAKFILE=$(echo $INFILE | sed -e s/\.tex// -e s/\.inc//).bak

# discard back up file if no change
if  [[ -z "$(diff $INFILE $BAKFILE)" ]] ; then
    echo $INFILE and $BAKFILE are the same, removing .bak file
    rm $BAKFILE
fi
