#!/bin/sh
# Do a little magic to run perl from anywhere in your path.

lines=`cat $0 | wc -l`
lines=`expr $lines - 16`
checksys=`uname -s`
if [ "$checksys" != "SunOS" ]
then
  tail -n $lines $0 > /tmp/visitperl$$
else
  tail -$lines $0 > /tmp/visitperl$$
fi
echo "__END__" >> /tmp/visitperl$$
echo "$0 $*" >> /tmp/visitperl$$
exec perl /tmp/visitperl$$ $0 ${1+"$@"}

unlink $0;
$0 = shift @ARGV;

#########################################################################
# Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
# LLNL-CODE-425250.
# All rights reserved.
# 
# This file is part of Silo. For details, see silo.llnl.gov.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the disclaimer below.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the disclaimer (as noted
#      below) in the documentation and/or other materials provided with
#      the distribution.
#    * Neither the name of the LLNS/LLNL nor the names of its
#      contributors may be used to endorse or promote products derived
#      from this software without specific prior written permission.
# 
# THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
# "AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
# LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
# LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
# CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
# PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
# NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# This work was produced at Lawrence Livermore National Laboratory under
# Contract No.  DE-AC52-07NA27344 with the DOE.
# 
# Neither the  United States Government nor  Lawrence Livermore National
# Security, LLC nor any of  their employees, makes any warranty, express
# or  implied,  or  assumes  any  liability or  responsibility  for  the
# accuracy, completeness,  or usefulness of  any information, apparatus,
# product, or  process disclosed, or  represents that its use  would not
# infringe privately-owned rights.
# 
# Any reference herein to  any specific commercial products, process, or
# services by trade name,  trademark, manufacturer or otherwise does not
# necessarily  constitute or imply  its endorsement,  recommendation, or
# favoring  by  the  United  States  Government  or  Lawrence  Livermore
# National Security,  LLC. The views  and opinions of  authors expressed
# herein do not necessarily state  or reflect those of the United States
# Government or Lawrence Livermore National Security, LLC, and shall not
# be used for advertising or product endorsement purposes.
#########################################################################
#
# Purpose:  Build the `silo.inc' Fortran include file from the
#           C header files `silo.h' and `silo-f.h' by defining
#           the public Fortran functions and the DB_* and DBOPT_*
#           constants.
#
# Usage:    Run this script with input redirected from `silo.h'
#           and output directed to `silo.inc' as in:
#
#           mv silo.inc silo.inc~
#           mkinc < silo.h > silo.inc
#
# Programmer:   Robb Matzke, Wed Nov 30 10:44:43 PST 1994
#
# Modified:
#
#   Robb Matzie, Mon Dec 19 10:45:49 PST 1994
#   The SDX_ prefix is removed from those #define's.
#
#   Sean Ahern, Wed Apr 19 16:07:19 PDT 1995
#   Added a list of character arrays.
#
#   Sean Ahern, Fri Oct 11 12:22:04 PDT 1996
#   Added parsing of enum datatypes.
#
#   Robb Matzke, 12 Mar 1997
#   The generated files have `-*- fortran -*-' in the first line so
#   emacsen know the type (since `.inc' isn't too standard).
#
#   Robb Matzke, 10 Jun 1997
#   Added a comma to the stop set for matching enum NAME=VALUE pairs.
#   Changed a Perl-5 construct to Perl-4.
#
#   Eric Brugger, Thu Sep  2 08:28:05 PDT 1999
#   I modified the script to output Fortran 90 style comments instead
#   of Fortran 77 style comments.  Sean also changed a couple of lines
#   to eliminate a run time warning.
#
#   Mark C. Miller, Thu Nov  2 14:30:00 PST 2006
#   Made it handle SILO_VERS_... symbols
#
#   Thomas R. Treadway, Thu Oct 11 15:21:03 PDT 2007
#   Modfied to handle new Fortran name-mangling 
#
#########################################################################


local (@parameters, @functions);
local ($sdx) = 0;

local($enum_body) = 0;
local($brace_level) = 0;

### Gather the info...
while (<>) {
    next if (/NO_FORTRAN_DEFINE/);
    if (/^#[ \t]*define[ \t]+((SILO_VERS|DB|SDX)(OPT|CSG)?_\w+)[ \t]+([^ \t\n]*)/) {
        local ($name, $value) = ($1, $4);
        $sdx=1 if ($name=~/^SDX/);
        $name =~ s/^SDX_//;
	if ($name eq "SILO_VERS_TAG")
	{
	    next;
	}
	if ($value eq "")
	{
	    $value = 0;
	}
        if (/FORTRAN STRING/)
        {
            push (@strings, join(':',$name,$value));
        } else {
            push (@parameters, join(':',$name,$value));
        }
    }

    if (/^#[ \t]*define[ \t]+\w+[ \t]+FC_FUNC[ \t]*\([ \t]*(\w+)[ \t,]+\w+/) {
        push (@functions, $1);
    }

    # Turn on `enum' handling?
    $enum_body = $brace_level+1 if /^(typedef\s+)?enum\b/;
    $brace_level += tr/{/{/;
    $brace_level -= tr/}/}/;

    # Turn off `enum' handling.  This assumes that the left curly
    # brace for the enum body is on the same line as `enum'.
    $enum_body = 0 if $brace_level<$enum_body;

    # The enum body must be indented.  The `VAR=VALUE' form must be used.
    if ($enum_body && /^\s+(DB_\w+)\s*=\s*([^ \t\n,]+)\s*,?/) {
       push (@parameters, join (':', $1, $2));
    }
}

#
# Write a Fortran comment describing this file.
#
$title = ($sdx?"SDX":"SILO");
$date = `date`;
chop($date);
$user = $ENV{USER};
print <<"EOF";
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccc -*- fortran -*-
!
!     $title include file for Fortan applications.
!
! Programmer:   $user $date
!
! WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
!    This file is generated automatically from C header files in the
!    SILO distribution.  Do not make changes to this file manually,
!    as those changes will disappear.
! WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
!
! This file was generated by the following commands...
EOF

if ($sdx) { print <<"EOF";
!       mv sdx.inc sdx.inc~
!       cat sdx.h sdx_server.h | mkinc > sdx.inc
!
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


EOF
} else { print <<"EOF";
!       mv silo.inc silo.inc~
!       cat silo.h silo_f.h | mkinc > silo.inc
!
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc


EOF
}

### Write `external' statements for the functions...
print "!...Functions.\n";
$line = "";
foreach $var (sort @functions) {
    if (length($line)+2+length($var)>72) {
        print "$line\n";
        $line = "";
        }
    if (0==length($line)) {
        $line = "      external $var";
    } else {
        $line .= ", " . $var;
    }
}
print "$line\n" if (0!=length($line));

### Write `integer' statements for the functions...
print "\n";
$line = "";
foreach $var (sort @functions) {
    if (length($line)+2+length($var)>72) {
        print "$line\n";
        $line = "";
        }
    if (0==length($line)) {
        $line = "      integer  $var";
    } else {
        $line .= ", " . $var;
    }
}
print "$line\n" if (0!=length($line));

### Write `integer' statements for the constants...
print "\n\n!...Parameters.\n";
$line = "";
foreach $var (sort @parameters) {
    local ($name, $value) = split (':', $var);
    if (length($line)+2+length($name)>72) {
        print "$line\n";
        $line = "";
        }
    if (0==length($line)) {
        $line = "      integer  $name";
    } else {
        $line .= ", " . $name;
    }
}
print "$line\n" if (0!=length($line));

### Write `character' statements for the constants...
foreach $var (sort @strings)
{
    local ($name, $value) = split (':', $var);
    print "      character*".length($value)."  $name\n";
}

### Write `parameter' statements for the constants...
### Don't sort these, because some $value's may depend on
### previous parameter statements.
print "\n";
push(@temparray,@parameters);
push(@temparray,@strings);
foreach $var (@temparray) {
    local ($name, $value) = split (':', $var);
    if ($value =~ /^0x[0-9A-F]*/) {
        $value = hex($value);
    } elsif ($value =~ /^0[0-7]*/) {
        $value = oct($value);
    } elsif ($value =~ /^DB_HDF5_OPTS(.*)/) {
        local ($foo, $bar) = split ('\(', $value);
        local ($g1, $g2) = split ( '\)', $bar);
        $value = "DB_HDF5X+$g1*2048";
    }
    $line = sprintf("      parameter (%-25s = %-15s)", $name, $value);
    if (length($line)>72) {
        local $off=0;
        while ($off < length($line)) {
            $curline = substr($line, $off, $off+64);
            if ($off == 0) {
                printf "$curline\n";
            } else {
                printf "     c$curline\n";
            }
            $off += 64;
        }
    } else {
        printf "$line\n";
    }
}

print "\n\n! End.\n";

exit 0;
