#!/bin/sh
#
# Copyright (C) 2005-2009 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

# Import functions
. ./config/scripts/functions.sh

# Go!
start_time=`date '+%s'`

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

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

BEGIN
EOF

# Prepare the source tree
chmod -R u+w .

# Remove temporary directories
echon " * Removing temporary directories..."
rm -rf tmp*
find tests -depth -name ',,*' -exec rm -rf {} \;
echo "done."

# Remove script-generated files
echon " * Removing script outputs..."
rm -f config/local/*
rm -f config/m4/do-not-edit-*.m4
rm -f doc/config/build-examples/*.ac
find . -name Makefile.am -exec rm {} \;
rm -f .abilint abilint.out
find src -name 'abinit.dep' -exec rm {} \;
find src -name 'abinit.dir' -exec rm {} \;
echo "done."

if test "${1}" != "--keep-source"; then
 echon " * Removing script-generated source files..."
 find src -name 'interfaces_*.F90' -exec rm {} \;
 rm -f src/13iovars/is_input_variable.F90
 echo "done."
fi

# Remove autotools files
echon " * Removing autotools files..."
find src -depth -name '.deps' -exec rm -rf {} \;
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 depcomp install-sh ltmain.sh missing)
echo "done."

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

# Remove object files, libraries and programs
echon " * Removing object files, libraries and programs..."
find prereqs -name '*.a' -o -name '*.o' -exec rm {} \;
find plugins -name '*.a' -o -name '*.o' -exec rm {} \;
find src -name '*.a' -o -name '*.o' -exec rm {} \;
find prereqs -name '*.mod' -exec rm {} \;
find plugins -name '*.mod' -exec rm {} \;
find src -name '*.mod' -exec rm {} \;
rm -f bin/*
(cd src/main \
 && rm -f abinip aim anaddb conducti cut3d lwf macroave mrgddb mrggkk optic)
(cd plugins/netcdf && make -i -f netcdf.mk clean > /dev/null 2>&1)
(cd plugins/etsf_xc && make -i -f etsf_xc.mk clean > /dev/null 2>&1)
(cd plugins/fox && make -i -f fox.mk clean > /dev/null 2>&1)
(cd doc/build && make -i -f build-docs.mk clean > /dev/null 2>&1)
(cd doc/users && make -i -f users-docs.mk clean > /dev/null 2>&1)
(cd doc/versioning && make -i -f versioning-docs.mk clean > /dev/null 2>&1)
(cd doc/theory && make -i -f theory-docs.mk clean > /dev/null 2>&1)
echo "done."

# Remove unwanted tarballs
echon " * Removing unwanted tarballs..."
rm -f \
 plugins/bigdft/bigdft-1.0.tar.gz \
 plugins/etsf_io/etsf_io-1.0.1.tar.gz \
 plugins/etsf_xc/etsf_xc-0.9.tar.gz \
 plugins/fox/FoX-4.0.1.tar.gz \
 plugins/light/light-abinit-5.6.tar.gz \
 plugins/netcdf/netcdf-3.6.2.tar.gz \
 plugins/xmlf90/xmlf90-1.2g.tar.gz
echo "done."

cat <<EOF
END

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

BEGIN
EOF

echon " * Checking the source tree..."
lint=`bzr status 2>&1 | grep '^unknown:$'`
if test "${lint}" = ""; then
 echo "everything OK."
else
 echo "not OK."
 echo "   -> Some files are marked as \"unknown\" by bzr."
fi

end_time=`date '+%s'`

cat <<EOF
END

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

EOF
