#!/bin/bash
# H0 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# H0 X
# H0 X   libAtoms+QUIP: atomistic simulation library
# H0 X
# H0 X   Portions of this code were written by
# H0 X     Albert Bartok-Partay, Silvia Cereda, Gabor Csanyi, James Kermode,
# H0 X     Ivan Solt, Wojciech Szlachta, Csilla Varnai, Steven Winfield.
# H0 X
# H0 X   Copyright 2006-2010.
# H0 X
# H0 X   These portions of the source code are released under the GNU General
# H0 X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html
# H0 X
# H0 X   If you would like to license the source code under different terms,
# H0 X   please contact Gabor Csanyi, gabor@csanyi.net
# H0 X
# H0 X   Portions of this code were written by Noam Bernstein as part of
# H0 X   his employment for the U.S. Government, and are not subject
# H0 X   to copyright in the USA.
# H0 X
# H0 X
# H0 X   When using this software, please cite the following reference:
# H0 X
# H0 X   http://www.libatoms.org
# H0 X
# H0 X  Additional contributions by
# H0 X    Alessio Comisso, Chiara Gattinoni, and Gianpietro Moras
# H0 X
# H0 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

usage="reuse saved copies of module files created by a source file if they are identical to new module file
environment: OD_SKIP = number of bytes to skip in module file header
Usage: $0 [ -lc | -uc ] [ -suffix suffix ] -- filename [ filename2 ... ]" 1>&2

mydir=`dirname $0`

if [[ $# -lt 1 ]]; then
  echo "$usage" 1>&2
  exit 1
fi

args=""
while [[ $# -gt 0 && $1 != "--" ]]; do
  args="$args $1"
  shift
done

# echo "args $args" 1>&2

if [[ $1 != "--" ]]; then
  echo "$usage" 1>&2
  exit 1
fi
shift

for file in $*; do
  modules=`egrep -i '^[ 	]*module[ 	]+[^ 	]+[ 	]*$!?' $file | awk '{print $2}'`
  for mod in `echo $modules | $mydir/module_name $args`; do
    if [ -f $mod.save ]; then
      cp $mod $mod.txt
      cp $mod.save $mod.save.txt
      if [[ ! -z $OD_SKIP ]] ; then
	 od -t x -j $OD_SKIP < $mod > $mod.txt
	 od -t x -j $OD_SKIP < $mod.save > $mod.save.txt
      fi
      if [[ ! -z $RE_SKIP ]] ; then
	 egrep -v "$RE_SKIP" < $mod > $mod.txt
	 egrep -v "$RE_SKIP" < $mod.save > $mod.save.txt
      fi
      diff -q $mod.txt $mod.save.txt > /dev/null
      out=$?
      if [[ $out == 0 ]]; then
	mv $mod.save $mod
      else
	rm -f $mod.save
      fi
      # rm -f $mod.txt $mod.save.txt
    fi
  done
done
