#!/bin/sh
#
# Copyright (c) 2005-2007 The ABINIT Group (Yann Pouillon)
# All rights reserved.
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

#
# IMPORTANT NOTE
#
# For maintainer use only !
#
# PLEASE DO NOT EDIT THIS FILE, AS IT COULD CAUSE A SERIOUS LOSS OF DATA.
# YOU HAVE BEEN WARNED !
#

# Check that we are in the right directory
if test ! -s "./configure.ac" -o ! -s "src/main/abinit.F90"; then
	echo "clean-source-tree: Cowardly refusing to remove something from here!"
	exit 1
fi

start_time=`date '+%s'`

cat <<EOF
Source-tree clean-up report
===========================

Vacuum clean
------------

BEGIN
EOF

# Remove temporary directories
echo -n " * Removing temporary directories..."
chmod -R u+w .
rm -rf tmp*
find tests -depth -name ',,*' -exec rm -rf {} \;
echo "done."

# Remove script-generated files
echo -n " * Removing script-generated files..."
rm -f config/m4/do-not-edit-*.m4
rm -f \
 lib/etsf_io/*.F90 lib/etsf_io/abinit.src lib/etsf_io/abinit.amf \
 src/13iovars/is_input_variable.F90 \
 src/13ionetcdf/netcdf_*_dtset.F90
rm -f doc/config/build-examples/*.ac
find . -name Makefile.am -exec rm {} \;
rm -f .abilint abilint.out
echo "done."

# Remove autotools files
echo -n " * Removing autotools files..."
rm -f core config.log config.status stamp-h1 config.h config.h.in*
rm -rf aclocal.m4 autom4te.cache configure confstat*
(cd config/gnu && rm -f config.guess config.sub install-sh missing)
echo "done."

# Remove Makefiles and machine-generated files
echo -n " * Removing files produced by the configure script..."
find . -name Makefile -exec rm {} \;
find . -name Makefile.in -exec rm {} \;
rm -f config.mk src/defs/defs_info.F90 tests/tests.env
rm -f util/build/wrap-mpif90 util/build/wrap-buggy-f90 util/wrappers/abinit.py
rm -f src/defs/interfaces_*.F90
echo "done."

# Remove object files, libraries and programs
echo -n " * Removing object files, librairies and programs..."
find lib -name '*.a' -o -name '*.o' -exec rm -f {} \;
find src -name '*.a' -o -name '*.o' -exec rm -f {} \;
find src -name '*.mod' -exec rm {} \;
rm -f bin/*
(cd src/main \
 && rm -f abinip abinitgw aim anaddb conducti cut3d lwf macroave mrgddb mrggkk optic)
(cd lib/netcdf && make -i -f netcdf.mk clean > /dev/null 2>&1)
(cd lib/etsf_xc && make -i -f etsf_xc.mk clean > /dev/null 2>&1)
(cd lib/xmlf90 && make -i -f xmlf90.mk clean > /dev/null 2>&1)
echo "done."

# Remove obsolete files
echo -n " * Removing obsolete files..."
rm -f \
 config/m4/detect-libs.m4 \
 config/m4/env.m4 \
 config/m4/extlibs.m4 \
 config/m4/optflags_dirs.m4 \
 src/03ionetcdf/*_ncdf.F90 \
 src/03ionetcdf/.arch-ids/def_dtset_ncdf.F90.id \
 src/03ionetcdf/.arch-ids/get_dims_dtset_ncdf.F90.id \
 src/03ionetcdf/.arch-ids/get_dtset_ncdf.F90.id \
 src/03ionetcdf/.arch-ids/put_dtset_ncdf.F90.id \
 util/build/.arch-ids/wrap-buggy-f90.id
echo "done."

# Remove GNU Arch garbage
echo -n " * Removing GNU Arch garbage..."
find . -depth -name ',,*' -a ! -path '*{arch}*' -exec rm -rf {} \;
find . -name '*.orig' -exec rm {} \;
find . -name '*.rej' -exec rm {} \;
echo "done."

end_time=`date '+%s'`

cat <<EOF
END

Source-tree audit
-----------------

BEGIN
EOF

echo -n " * Linting the source tree..."
lint=`baz lint 2>&1`
if test "${lint}" = ""; then
 echo "everything OK."
else
 echo "not OK."
 echo "   => You should have a look at this."
fi

cat <<EOF
END

-- 
Time elapsed : `awk "END{print ${end_time}-${start_time}}" < /dev/null` s

EOF
