#!/bin/sh
#
# Copyright (C) 2005-2010 ABINIT Group (Yann Pouillon)
#
# 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/98_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 and files
echon " * Removing temporary directories and files..."
rm -f src/abirules.log
rm -rf tmp*
find . -depth -name 'tmp-*' -exec rm -rf {} \;
find src -name '*.abiauty' -exec rm -f {} \;
find src -name '*.abirules' -exec rm -f {} \;
find src -name '*.old' -exec rm -f {} \;
find src -name '*.~[0-9]~' -exec rm -f {} \;
echo "done."

# Remove script-generated files
echon " * Removing script outputs..."
rm -f config/local/*
rm -f config/m4/auto-*.m4
rm -f config/m4/do-not-edit-*.m4
rm -f doc/config/build-examples/*.ac
rm -f doc/config/build-examples/uncertified/*.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/57_iovars/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.dump config.mk config.optim config.pc config.py config.sh
rm -f src/01manage_mpi/defs_info.F90
rm -f tests/tests.env tests/tests-install.env
rm -f util/build/wrap-mpif90 util/build/wrap-buggy-f90 util/wrappers/abinit.py
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/98_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/libxc && make -i -f libxc.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 \
  infos_html*.tar.gz \
  robodoc-html*.tar.gz \
  plugins/bigdft/bigdft-1.0.tar.gz \
  plugins/etsf_io/etsf_io-1.0.1.tar.gz \
  plugins/libxc/libxc-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
echo "done."

# Remove obsolete files
#echon " * Removing obsolete files..."
#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
