#!/bin/bash
# make-sources-list.sh
# GPLv3+
# This will generate a list of files to be included as source code.
# Then it will generate LaTeX includes.
# XXX Great cruft
# Redirect fix XXX
# No spaces in filenames, or this may go awry.
# Perhaps add QR codes like:
CODEDIR="/home/`id -u -n`/FORKSAND/IT//forksand-it-manual/source/resources"
cd "$CODEDIR" || exit
TEXOUT="/home/`id -u -n`/FORKSAND/IT/forksand-it-manual/source/Source-gen.tex"
# Build a grep exclude command that has file extensions to not include.
EXCLUDE="\./\.git \.csv$ \.eps \.git$ \.gz$ \.jpg$ \.ods$ \.ods\#$ \.png$ \.pdf$ \.swp$ \.tmp$ \.xml$ \.aux$ \.fuse_hidden*$  \.glo$  \.gls$  \.idx$  \.ilg$  \.ind$  \.lof$  \.log$  \.lol$  _minted-*$  \.old$  \.out$  \.swp$  \.toc$  \.zip$"
EXCLUDEGREP="grep -v -i "
for i in $EXCLUDE 
  do EXCLUDEGREP="$EXCLUDEGREP -e $i"
done
SRCFILES=`find . -type f | sort -V | $EXCLUDEGREP`
rm -f $TEXOUT $SHAOUT SHA256SUM
touch $TEXOUT $SHAOUT SHA256SUM
# Make checksums
# LaTeX files to be rendered
for i in $SRCFILES
do
  #NOUNDER=`echo $i | sed -e 's/_/\\\_/g'`
  NOUNDER=`echo $i | sed -e 's/_/\\\_/g' | cut -f 2- -d "/"`
  echo "\section{\texttt{$NOUNDER}}"
#  echo "\texttt{$NOUNDER} \\\ "
  echo "\texttt{SHA256: `sha256sum $i | cut -f 1 -d " "`}"
  sha256sum $i >> SHA256SUM
MINTEDPATH=`echo $i | cut -f 2- -d "/"`
MINTEDPATH="resources/$MINTEDPATH"
  echo "\inputminted{sh}{$MINTEDPATH}"
done >> $TEXOUT

sort -V SHA256SUM > SHA256SUM.tmp
echo "There are `cat SHA256SUM | sort -u | wc -l` source code files included." > STATS
echo "There are `cat SHA256SUM.tmp | cut -f 1 -d " " | sort -u | wc -l` unique files." >> STATS
rm SHA256SUM.tmp

# Files not rendered
#echo "\section{Not Rendered}" >> $TEXOUT
#echo "The files listed in this section were not rendered." >> $TEXOUT
#echo "Upstream {\LaTeX} files:" >> $TEXOUT
