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

set -e

# Init
my_name="convert-source-tree"

# Check if we are in the right directory
if test ! -s "makearch" -o ! -s "Src_main/abinit.F90"; then
	echo "${my_name}: Cowardly refusing to touch a non-ABINIT 4 tree."
	exit 1
fi

start_time=`date '+%s'`

cat <<EOF
Conversion report
=================

Base
----

BEGIN
EOF

echo -n " * Preparing source tree..."
mkdir -p lib src tests
rm -rf Lib_netcdf Lib_nanoquantaxc Lib_XMLf90
rm -f Src_3ionetcdf/*_dtset_ncdf.F90
rm -rf Arch_hooks
rm -rf Machine_dept_files
rm -rf Makefile descrip.mms make.bat makearch makedefns makefile_macros makemake
rm -rf makemake.ROBODOC_OLD makencdf robodoc.rc tmp_makefile ,version
echo "done."

# Lib_*
echo -n " * Converting external libraries..."
for f in Lib_light/*.f; do mv "${f}" `echo "${f}" | sed -e 's/f$/F90/'`; done
for d_old in Lib_*; do
 d_new=`echo "${d_old}" | sed -e 's,Lib_,lib/,'`
 mv "${d_old}" "${d_new}"
done
mv lib/fftnew src/lib01fftnew
echo "done."

# Src_*
echo -n " * Refining core source structure..."
for d_old in Src_*; do
 d_new=`echo "${d_old}" | sed -e 's,Src_,src/0,'`
 mv "${d_old}" "${d_new}"
done
mv src/0defs src/defs
mv src/0main src/main
echo "done."

echo -n " * Updating levels..."
mv src/02nonlocal src/03nonlocal
mv src/02bader src/04bader
mv src/03gw src/05gw
mv src/09drive src/11drive
echo "done."

echo -n " * Upgrading source files..."
for f in src/*/*.F90; do
 awk -f `dirname $0`/include-config-h.awk "${f}" | \
  sed -f `dirname $0`/upgrade-source.sed > "${f}.tmp-convert"
 mv "${f}.tmp-convert" "${f}"
done
echo "done."

# Test_*
cat <<EOF

Test suite
----------

BEGIN
EOF

mv Tutorial Test_tutorial
for d_old in Test_*; do
 d_new=`echo "${d_old}" | sed -e 's,Test_,tests/,'`
 mv "${d_old}" "${d_new}"
 mkdir -p "${d_new}/Input"
 mv "${d_new}/RunTests.cnf" "${d_new}/tests.cnf" || /bin/true
 mv "${d_new}"/*.files "${d_new}/Input" || /bin/true
 mv "${d_new}"/*.in "${d_new}/Input" || mv "${d_new}"/input[A-Z] "${d_new}/Input"
 mv "${d_new}"/*.gz "${d_new}/Input" || /bin/true
 chmod 644 "${d_new}"/Input/* "${d_new}"/Refs/* || /bin/true
done

find Psps_for_tests -type f -exec chmod 644 {} \;
mv Psps_for_tests tests
mv tests/in tests/built-in
mv tests/tutorial/trf2_6.in_freq tests/tutorial/Input
mv tests/v1/t97.exit tests/v1/Input
mv tests/v3/*.xml tests/v3/Input
mv tests/v4/t51.in_* tests/v4/Input

mkdir -p tests/xmlf90/Input
mv tests/built-in/Input/test6.in tests/xmlf90/Input

rm RunTests.pl
rm -f tests/built-in/Input/*.files tests/built-in/*.pl
rm -f tests/cpu/getrep.pl tests/cpu/files
rm -f tests/paral/Run*
rm -f tests/physics/Solid.pl
rm -f tests/tutorial/gz2.py tests/tutorial/dim.m tests/tutorial/*.out
rm -rf tests/tutorial/Tutorial-NLO

for f in tests/*/tests.cnf tests/*/Refs/*.out; do
 sed -e 's,ABINIT/Infos/copyright,abinit/COPYING,' \
     -e 's,ABINIT/Infos/contributors,abinit/doc/developers/contributors,' \
     -e 's,ABINIT/Infos/acknowledgments,abinit/doc/users/acknowledgments,' \
     -e 's,ABINIT/Infos/new_user_guide,abinit/doc/users/new_user_guide,' \
     -e 's,ABINIT/Infos/rules_coding,abinit/doc/developers/rules_coding,' \
     -e 's,Infos/var,doc/input_variables/var,' \
     "${f}" > "${f}.tmp"
 mv "${f}.tmp" "${f}"
done

cat <<EOF
END

Utilities
---------

BEGIN
EOF

# Utilities
echo -n " * Converting utilities..."
mkdir -p util/old util/subs
mv Utilities/* util/old
rm -rf Utilities
mv mkPrl_in util
mv scripts/netcdf util/subs
rm -rf scripts
echo "done."

cat <<EOF
END

Documentation
-------------

BEGIN
EOF

# Infos
echo -n " * Converting Infos..."
mv Infos doc
cd doc
rm -f TODO
mkdir -p developers gallery help_make known_problems \
 maintainers manpages users
mv Features features
mv Tutorial tutorial
mv ../tests/tutorial/lesson_visual tutorial
mv Images/* tutorial
rm -rf Images
mv Installation_notes install_notes
mv Macroave_Docs macroave
mv Manual users/manual
mv Miscellaneous misc
mv Notes_for_coding/* developers
rm -rf Notes_for_coding
mv Presentation presentation
mv Psp_infos psp_infos
mv Release_notes release_notes
mv Strain_nonlocal developers/strain_nonlocal
mv Theory theory
mv abinis_help.html acknowledgments.html aim_help.html aimhelp.ps \
 aimhelp.tex anaddb_help.html band2eps_help bibliography.html \
 conducti_manual.tex cut3d_help cut3d_help.html elphon_manual.ps \
 gwmanual.txt mrgddb_help.html netiquette.html new_user_guide.html \
 newsp_help optic_help.html paral_use piezoelectric.txt \
 positronmanual.txt problem_report respfn_help.html respfn_help.keep \
 tuning FAQ.html users
mv context contributors ddbs.upgrade dirs_and_files formabinit.css \
 format_KSS other_codes planning developers
mv known_problems.4.6.x known_problems
mv check_list maintainers
mv make_dev_help make_help make_targz_help help_make
rm makefile_macros_help
mv spacegrouphelpfile.html spgrcopyright.html spgrdefinition.html \
 spgrdescription.html spgrhead.html users
mkdir input_variables
mv varbas.html vardev.html varfil.html vargeo.html vargs.html vargw.html varint.html \
 varpar.html varpaw.html varrf.html varrlx.html keyhr.html input_variables
cd ..
echo "done."

echo -n " * Converting miscellaneous documents..."
mv changes.* ChangeLog
echo "done."

cat <<EOF
END

 ******************************************************************************
 ***** Your tree is now roughly compatible with the version 5 of ABINIT.  *****
 ***** Use at your own risks.                                             *****
 *****                                                                    *****
 ***** Have fun !                                                         *****
 ******************************************************************************

 Please DO CHECK these files against a recent ABINIT source tree:

    * src/9cut3d/wffile.F90
    * src/1managempi/herald.F90
    * src/defs/object_list
    * src/main/*.F90 (requires manual editing of the diff)
    * tests/*/tests.cnf

 This is very important, in order not to reintroduce old regressions.

                             --------------------

 Things that have been lost in the conversion:

    * changes in the old makemake, which should go to config/specs/binaries.cf
    * changes to test scripts
    * maybe something else, who knows ?

EOF
