#!/bin/bash
#
# Copyright (C) 2001-2021 Quantum ESPRESSO group
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License. See the file `License' in the root directory
# of the present distribution.
#
#
# This script is a simple wrapper calling the autoconf configuration
# script (configure) in install/
# Dependencies may be also directly generated
# 
# Courtesy of A. Ferretti and G. Bussi
# Modified by F. Spiga and P. Giannozzi
#
#================================================================
#
MANUAL=" Usage
   configure [-h, --help] [--save] [<conf_flags>]

 -h, --help           print this manual    
     --save           do not make clean
 <conf_flags>         these flags will be passed to 
                      the autoconf configure

 After configuration, the make.inc file will be created in the
 QE home (current) directory
 
 ---------------------------------------------------------------
 Manual from autoconf configure : 
 ---------------------------------------------------------------
"
#
#================================================================
#

# run from directory where this script is
auxdir=`echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
if [ "$auxdir" != "configure" ] ; then cd $auxdir ; fi

#
# detect the simplest cases 
#
case $1 in 
  ("-h" | "--help" )      echo "$MANUAL"     ; ./install/configure --help ; exit 0 ;;
esac

# SAFEGUARD: if you run configure without clean everything first there 
#            are chances that something goes wrong. Forcing veryclean then.

if [[  ($1 =~ "--save") ]] ; then
  shift;
elif [[ (-e make.inc) && (-e Makefile) ]] ; then
  make -f Makefile veryclean
fi
test -e ./install/make.inc       && rm ./install/make.inc

# compute dependencies (next two lines avoid bogus errors)
touch ./include/qe_cdefs.h
touch ./include/configure.h
./install/makedeps.sh

# run the autoconf configure with the given conf_flags 
./install/configure "$@"

# copy make.inc in the home dir
# and final clean up
#
test -e ./install/make.inc && mv ./install/make.inc .
test -e config.log        && mv config.log    ./install/
test -e config.status     && mv config.status ./install/
test -e configure.msg     && mv configure.msg ./install/

exit 0
