#!/usr/bin/perl
########################################################################
#
#                  CTC++ System, ctcpost (1.1)
#                  CTC++ Perl frontend (Cygwin)
#
# RCSfile     : $RCSfile: ctcpost $
# Version     : $Revision: 1.1 $ $Date: 2017/06/20 12:31:11 $
# Last Edited : 4.10.2017
# Author      : $Author: roland $
#
#               Copyright (c) 2013-2017 Verifysoft Technology GmbH
##########################################################################
# Additional notice to users:
# Verifysoft Technology provides support for this ctc cygwin component of
# the CTC++ System in the same way as Verifysoft Technology provides support
# for the other components of the CTC++ System. Because this component is
# provided in a source file form, you are able to modify this file.
# However, if you do any such modifications, Verifysoft Technology may not
# be capable of providing you support, and Verifysoft Technology reserves
# the right not to support you with this component, if you have done your
# own modifications to this file.
#
# Verifysoft Technology would however be pleased to receive your bug
# corrections/reports and enhancement suggestions and will consider them
# in future versions of this tool.
#
##########################################################################



##########################################################################
# subroutines
##########################################################################
sub makeopts;
sub expand_opts;
sub escape_opt;
sub cygpath;

##########################################################################
# global "constants"
##########################################################################
$version            = "ctcpost cygwin v1.1";

##########################################################################
# execution starts here !
##########################################################################

$ctchome = Cygwin::win_to_posix_path($ENV{CTCHOME});

 ARG:
if ($^O eq "cygwin") {
    for ($i=0;$i<=$#ARGV;$i++) {
	if ($ARGV[$i] eq '-ff' ||
	    $ARGV[$i] eq '-fd' ||
	    $ARGV[$i] eq '-fc' ||
	    $ARGV[$i] eq '-fmcdc' ||
	    $ARGV[$i] eq '--nhe' ||
	    $ARGV[$i] eq '-h' ||
	    $ARGV[$i] eq '-V' ||
	    $ARGV[$i] eq '-no-templates' ||
	    $ARGV[$i] eq '-no-warnings') {
	} elsif (substr($ARGV[$i],0,1) eq '@') {
	    my $rsp = substr($ARGV[$i],1);
	    if (!-f $rsp) {
		$rsp=cygpath($rsp);
		if (-f $rsp) {
		    $ARGV[$i]='@'.$rsp;
		}
	    }
	} elsif($ARGV[$i] eq '-i' ||
		$ARGV[$i] eq '-C' ||
		$ARGV[$i] eq '-w' ||
		$ARGV[$i] eq '-f' ||  #Don't convert given source files name
		$ARGV[$i] eq '-nf')   #They have been passed as is to ctc
	{
	    $i++;
	} elsif($ARGV[$i] eq '-c') {
	    $i++;
	    my $conf = $ARGV[$i];
	    $ARGV[$i] = cygpath($conf);
	} elsif($ARGV[$i] eq '-p' || 
		$ARGV[$i] eq '-u' ||
		$ARGV[$i] eq '-t' ||
		$ARGV[$i] eq '-x' ||
		$ARGV[$i] eq '-j' ||
		$ARGV[$i] eq '-a' ) {
	    # Nothing, file name coming
	} elsif ($ARGV[$i] eq '-') {
	} else {
	    my $exe = cygpath($ARGV[$i]);
	    $ARGV[$i]=$exe;
	}
    }
}

exec($ctchome."/ctcpost.exe", @ARGV);

# We should never hit this
exit 1; 

1;
##########################################################################
# SUBROUTINES
##########################################################################

##########################################################################
# SUBROUTINE: cygpath
# PURPOSE   : Wrapper to "cygpath"
# ARGUMENT  : - $file 
# RETURNS   : $file with Windows conventions
##########################################################################
sub cygpath
{
    my ($file) = @_;

    return Cygwin::posix_to_win_path($file) if $^O eq "cygwin";
    # External 
    #    $tt = qx#cygpath -m $file#;
    #    $tt =~ s/\015?\012$//;

    return $file;
}


# EOF $RCSfile: ctcpost $




