#!/bin/bash

fc=${1}
cc=${2}
ld=${3}
ff=${4}
cf=${5}
arch=${6}

date=`date`
dir=`/bin/pwd`

hostname=`uname -n`
errcode="$?"
if [ ${errcode} != "0" ] ; then
hostname="unknown";
fi

#mods=`module list 2>&1 | grep -v Loaded | tr -d '\n'`

cat << END_CAT > MG_ENV.F
MODULE MG_ENV_MOD
! Do not edit this file, it is automatically generated by the
! script capture_env.

   CONTAINS

      SUBROUTINE ENV(OUTPUT_LOC)

      IMPLICIT NONE
      INTEGER, INTENT(IN) :: OUTPUT_LOC

      WRITE(OUTPUT_LOC,*) 'code: miniGhost'
      WRITE(OUTPUT_LOC,*) 'version:1.0.0'
      WRITE(OUTPUT_LOC,*) 'compiled_on: ${date}'
      WRITE(OUTPUT_LOC,*) 'directory: ${dir}'
      WRITE(OUTPUT_LOC,*) 'FORTRAN_compiler: ${fc}'
      WRITE(OUTPUT_LOC,*) 'C_compiler: ${cc}'
      WRITE(OUTPUT_LOC,*) 'linker: ${ld}'
      WRITE(OUTPUT_LOC,*) 'FORTRAN_flags: ${ff}'
      WRITE(OUTPUT_LOC,*) 'C_flags: ${cf}'
      WRITE(OUTPUT_LOC,*) 'arch: ${arch}'
      WRITE(OUTPUT_LOC,*) 'hostname: ${hostname}'
END_CAT
for i in `module list 2>&1 | grep -v Loaded | tr -d ' '`
do
mod=`echo $i | tr ')' ':' | sed -e 's/:/: /'`
cat << END_CAT1 >> MG_ENV.F
      WRITE(OUTPUT_LOC,*) 'module_list${mod}'
END_CAT1
done
cat << END_CAT2 >> MG_ENV.F

      END SUBROUTINE ENV

END MODULE MG_ENV_MOD
END_CAT2
