#!/devtool/local/bin/perl
# mfcinc        *** internal script (called by vmake !)
#
# @(#)mfcinc		1997-11-18
#
# module frame to c include file
#
# TOOL          directory for tools
# KEEPFLG       optional keep intermediate files flag
#
#
# includes currently unused but syntactically allowed
#
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    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, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

package mfcinc;
use Env;
use File::Copy;
use File::Basename;
use Carp;
use ToolLib;

do "$TOOLVARS";
if ( $@ ) { print "$@"; exit }
ToolTrace("mfcinc called with @ARGV\n");

$USAGE = "usage: mfcinc [-<symbol>=<value>] [-k] source [includes]\n";

do "$TOOL/bin/mf_opts$TOOLEXT";
if ( $@ ) { print "$@"; exit }

if ( @ARGV < 1 ) { print $USAGE; exit 1 }

$SOURCE = shift;
($BASE, $SOURCEPATH, $EXT) = fileparse($SOURCE, '\..*');
$EXT=".h" unless $EXT;
undef $EXT if ($NOEXTENSION == 1 );


my(@INCLUDES) = @ARGV;

@CONCOMOPTS;

if ( $unpack ne "no" )
{
	system("$TOOL/pgm/mfspl $SOURCE $NULL $NULL $BASE.cod") == 0
	   or croak "Error while executing \"$TOOL/pgm/mfspl $SOURCE $NULL $NULL $BASE.cod\"\n".
	   "message: $!\n";
}
else
{
    ToolLib::SrcCopy($SOURCE, "$BASE.cod") || die "Can't copy $SOURCE to $BASE.cod: $!\n" # CTS 1108093
}

# &gar (includes + \n at the end of file, if necessary ->
# else compiler this not accepted) -> 31.08.98
open (FILE_OUT, "+>>$BASE.cod");
seek (FILE_OUT, 0, 2);
seek (FILE_OUT,-1,1);
$last_c=getc(FILE_OUT);
if ($last_c ne "\n")
{
   seek (FILE_OUT, 0, 2);
   print FILE_OUT "\n" if($last_c ne "\n");
   ToolTrace("mfcexp: $BASE$EXT has a incomplete last line\n");
   ToolTrace("mfcexp: \"\\n\" append at $BASE$EXT\n");
}
close FILE_OUT;
# 28.08.98 <- &gar

if ( $concom eq "yes" )
{
    system("$TOOL/pgm/mfconcom", "-n", @CONCOMOPTS, "$BASE.cod", "$BASE$EXT", $MACH, $KIND, $TRACE, $COMP, $OS, $OSSPEC) == 0
        or croak "Error while executing \"$TOOL/pgm/mfconcom -n @CONCOMOPTS $BASE.cod $BASE$EXT $MACH $KIND $TRACE $COMP $OS $OSSPEC\"\n".
        "message: $!\n";
}
else
{
    unlink "$BASE$EXT";
    rename "$BASE.cod", "$BASE$EXT" ;
}

if ( $unpack eq "no" && $checkinc ne "no")
{
    ToolLib::CheckIncludes(\@INCLUDES, "$BASE$EXT", $LAYER);
}

if ($SCRIPT ne "")
{
	@ARGV = ("$BASE$EXT");
	do "$SCRIPT";
	if ( $@ ) { die "$@";   }
}


if ( @KEEPFLG == 0 ) { unlink "$BASE.cod" }

