#!/bin/sh
#
# title:  dclf2c, DCL for C automatic generator from DCL for Fortran
# author: Takashi Kagimoto (kagimoto@jamstec.go.jp)
# create: 2007/02/16
# modify: 2007/02/26 T.Kagimoto
#         (bug fix at the first line in the main procedure section)
# modify: 2007/02/28 T.Kagimoto
#         (bug fix in the section where prototype declaration file, cdcl.h, is
#          created. An expression, find xxx/yyy/zzz/{abc,def,ghi}/, is an bash
#          extension, and is not acceptable with /bin/sh.)
# modify: 2007/04/04 T.Kagimoto
#         (delete procedures for bitlib.h and bitlib.in and add procedures for
#          config.h.in and src/misc1/clcklib/Makefile)
# modify: 2011/02/17 T.Koshiro
#         (bug fix in the procedure forsrc/env2/pslib/dclpsXXX)
#

#-------------------------------------------------------------------------------
# parameter settings
#-------------------------------------------------------------------------------
AWK=gawk
DCLVER=5.3.4.2
DCLF2CDIR=/home/koshiro/comp/gfd-dennou/dclf2c/dclf2c-5.3.4
F2COPT="-R"

#-------------------------------------------------------------------------------
# load module
#-------------------------------------------------------------------------------
. ${DCLF2CDIR}/lib/mod_chMakefile
. ${DCLF2CDIR}/lib/mod_chMkinclude
. ${DCLF2CDIR}/lib/mod_f2c
. ${DCLF2CDIR}/lib/mod_utils
. ${DCLF2CDIR}/lib/mod_prototype

#-------------------------------------------------------------------------------
# main procedure
#-------------------------------------------------------------------------------
test ! -d dcl-${DCLVER} -a ! -h dcl-${DCLVER} && { message_dump "MAIN" "dcl-${DCLVER} is not found."; }
file_list=`find dcl-${DCLVER}/`

Csrc_list=
for f in ${file_list}
do
    fdest=`echo ${f} | sed s/dcl-${DCLVER}/dcl-${DCLVER}-C/`
    IsFortran=`Inquire_src ${f} "f"`
    IsC=`Inquire_src ${f} "c"`
    IsGFile=`Inquire_src ${f} "g"`
    IsLibsrc=`Inquire_Libsrc ${f}`

    #
    # f: directory
    #
    if [ -d ${f} ]; then
        if [ ! -d ${fdest} ]; then
            echo "Pass 0: mkdir -p ${fdest}"
            mkdir -p ${fdest}
        fi
        continue
    fi
    #
    # f: Fortran source code
    #
    if [ ${IsFortran} -eq 1 ]; then
        #---- skip if ${f} is osgarg.f.dummy or osqarn.f.dummy ----
        test `Inquire_file ${f} "osgarg.f.dummy"` -eq 1 && { continue; }
        test `Inquire_file ${f} "osqarn.f.dummy"` -eq 1 && { continue; }
        #---- create yet another C code if ${f} is msgdmp.f ----
        if [ `Inquire_file ${f} "msgdmp.f"` -eq 1 ]; then
            fdest=`echo ${fdest} | sed "s/\.f/.c/"`
            echo "Pass 1: ${f} -> ${fdest}"
            create_yet_another_msgdmp > ${fdest}
            continue
        fi
        #---- rename if ${f} is osgarg.f.getarg or osqarn.f.iargc ----
        if [ `Inquire_file ${f} "osgarg.f.getarg"` -eq 1 ]; then
            fdest=`echo ${fdest} | sed "s/\.getarg//"`
        fi
        if [ `Inquire_file ${f} "osqarn.f.iargc"` -eq 1 ]; then
            fdest=`echo ${fdest} | sed "s/\.iargc//"`
        fi
        #---- convert Fortran code to C code
        fdest=`echo ${fdest} | sed "s/\.f/.c/"`
        echo "Pass 1: ${f} -> ${fdest}"
        f2c ${F2COPT} < ${f} | change_incl_header > ${fdest}
        continue
    fi
    #
    # f: Fortran source code with the extension of "g" rather than "f"
    #
    if [ ${IsGFile} -eq 1 ]; then
        echo "Pass 2: ${f} -> ${fdest}"
        pre_gfile < ${f} | f2c ${F2COPT} | change_incl_header | post_gfile > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/Makefile.in
    #
    if [ `Inquire_file ${f} dcl-${DCLVER}/Makefile.in` -eq 1 ]; then
        echo "Pass 3: ${f} -> ${fdest}"
        change_topMakefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/Mkinclude.in
    #
    if [ `Inquire_file ${f} dcl-${DCLVER}/Mkinclude.in` -eq 1 ]; then
        echo "Pass 4: ${f} -> ${fdest}"
        change_Mkinclude < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/bin/Makefile
    #
    if [ `Inquire_file ${f} "bin/Makefile"` -eq 1 ]; then
        echo "Pass 5: ${f} -> ${fdest}"
        change_bin_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/lib/Makefile
    #
    if [ `Inquire_file ${f} "/lib/Makefile"` -eq 1 ]; then
        echo "Pass 6: ${f} -> ${fdest}"
        change_lib_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/src/env1/*/Makefile
    #
    if [ `Inquire_file ${f} "src/env1"` -eq 1 -a `basename ${f}` = "Makefile" ]; then
        echo "Pass 7: ${f} -> ${fdest}"
        change_env1_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/src/env2/*/Makefile
    #
    if [ `Inquire_file ${f} "src/env2"` -eq 1 -a `basename ${f}` = "Makefile" ]; then
        echo "Pass 8: ${f} -> ${fdest}"
        change_env2_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: dcl-x.y.z/src/misc1/clcklib/Makefile
    #
    if [ `Inquire_file ${f} "src/misc1/clcklib/Makefile"` -eq 1 -a `basename ${f}` = "Makefile" ]; then
        echo "Pass 9: ${f} -> ${fdest}"
        change_clcklib_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: other Makefile
    #
    if [ `basename ${f}` = "Makefile" ]; then
        echo "Pass 10: ${f} -> ${fdest}"
        change_Makefile < ${f} > ${fdest}
        continue
    fi
    #
    # f: config.h.in
    #
    if [ `Inquire_file ${f} "config.h.in"` -eq 1 ]; then
        echo "Pass 11: ${f} -> ${fdest}"
        modify_config_h_in < ${f} > ${fdest}
        continue
    fi
    #
    # f: C source code
    #
    if [ ${IsC} -eq 1 ]; then
        Csrc_list="${Csrc_list} ${f}"
        continue
    fi
    #
    # f: dcl-x.y.z/src/env2/pslib/dclpsXXX
    #
    if [ `Inquire_file ${f} "src/env2/pslib/dclps"` -eq 1 ]; then
        fdest=`echo ${fdest} | sed s/dclps/cdclps/`
        echo "Pass 12: ${f} -> ${fdest}"
        sed -e "s/dclps/cdclps/" -e "s/\`dclver\`/\`cdclver\`/" < ${f} > ${fdest}
        continue
    fi
    #
    # f: dclcc.org
    #
    if [ `Inquire_file ${f} "dclcc.org"` -eq 1 \
      -o `Inquire_file ${f} "dclconfig.org"` -eq 1 ]; then
        echo "Pass 13: ${f} -> ${fdest}"
        sed -e "s/dclconfig/cdclconfig/g" < ${f} > ${fdest}
        continue
    fi
    #
    # f: dclfrt.org
    #
    if [ `Inquire_file ${f} "dclfrt.org"` -eq 1 ]; then
        echo "dclfrt.org is not copied."
        continue
    fi
    #
    # f: other files
    #
    echo "Pass 14: cp ${f} ${fdest}"
    cp ${f} ${fdest}
done
#
# C code
#
for f in ${Csrc_list}
do
    fdest=`echo ${f} | sed s/dcl-${DCLVER}/dcl-${DCLVER}-C/`

    if [ ! -f ${fdest} ]; then
        echo "Pass 15: cp ${f} ${fdest}"
        cp ${f} ${fdest}
    else
        echo "${f} does not copied."
    fi
done

fdest=dcl-${DCLVER}-C/include/Makefile
echo "Pass 16: creating ${fdest} ...."
create_incl_Makefile > ${fdest}

#
# prototype declaration
#   create dcl-x.y.z-C/include/cdcl.h
#
done_csgi=0
done_isgc=0
cdcl_h="dcl-${DCLVER}-C/include/cdcl.h"
rm -f ${cdcl_h}

echo "${cdcl_h} is created....."

dir_list=`ls -d dcl-${DCLVER}-C/src/[gm]*[12]`
file_list=`Find_MultiDir "${dir_list}"`

for f in ${file_list}
do
    test `Inquire_src ${f} "c"` -eq 0 && { continue; }
    if [ `Inquire_file ${f} "msgdmp.c"` -eq 1 ]; then
        create_prototype_msgdmp >> ${cdcl_h}
    else
        test `Inquire_file ${f} "oldsrc"` -eq 1 && { continue; }
        if [ `Inquire_file ${f} "csgi"` -eq 1 ]; then
            test ${done_csgi} -eq 1 && { continue; }
            done_csgi=1
        fi
        if [ `Inquire_file ${f} "isgc"` -eq 1 ]; then
            test ${done_isgc} -eq 1 && { continue; }
            done_isgc=1
        fi
        if [ `Inquire_file ${f} "zgpack.c"` -eq 1 ]; then
            echo "#ifdef __GTK_H__" >> ${cdcl_h}
            create_prototype < ${f} >> ${cdcl_h}
            echo "#endif /* __GTK_H__ */" >> ${cdcl_h}
            continue
        fi

        create_prototype < ${f} >> ${cdcl_h}
    fi
done
#
# extract f2csrc
#
cd dcl-${DCLVER}-C
zcat ${DCLF2CDIR}/f2csrc.tar.gz | tar xvf -

echo "done..."
#EOF
