#!/bin/sh
# Generate a MSVC++ nmake file from a Unix makefile

# Author: Christopher Hylands
# Version: $Id$

# Copyright (c) 1999 The Regents of the University of California.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the above
# copyright notice and the following two paragraphs appear in all copies
# of this software.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 
# FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 
# THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 
# SUCH DAMAGE.
# 
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
# PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.


# This shell script reads in a Unix style makefile and generates a Microsoft
# Visual C nmake style makefile
#
# Usage:
# makevcgen makefile

infile=$1

if [ ! -r $infile ]; then
    echo "$0: '$infile' is not readable"
    exit 2
fi

outfile=$infile.vc

echo "# Do not Edit - This file is a MS Visual C++ file that is" > $outfile
echo "#  automatically generated by makevcgen from a Unix makefile" >> $outfile

# We use sed here because it sed is included in the default installation
# of Cygwin under NT.  Perl would be a good choice for this type of
# manipulation, but it is not shipped with Cygwin.

sed -e '
# Convert forward slashes to backslashes.  Use @ to separate the regexps
# rather than the usual /
s@/@\\@g

# Convert GNU conditionals to VC conditionals
s/^[        ]*ifeq[         ]*(\([^,]*\),\([^)]*\))/!if "\1" == "\2" #ifeq/
s/^[     ]*ifneq[        ]*(\([^,]*\),\([^)]*\))/!if "\1" != "\2" #ifneq/
s/^[    ]*ifndef\(.*\)$/!ifndef \1/
s/^[         ]*ifdef\(.*\)$/!ifdef \1/
s/^[   ]*else/!else/
s/^[       ]*endif/!endif/

# Rather than setting a variable to nothing, undefine it
s/^[     ]*\([A-Za-z0-9_]*\)[     ]*=[   ]*$/!undef \1/

# Globally convert dependency rules that refer to .o to .obj
s/\.o/.obj/g

# Strip out leading whitespace in makefile variable assignments
s/^[       ]+\([A-Za-z0-9_]+\)[     ]*=/\1 =/
s/^[   ]+\([A-Za-z0-9_]+\)[   ]*=/\1 =/
s/^[   ]*\([A-Za-z0-9_]+\)[    ]*\+=/\1 = $(\1)/

# If we include any makefiles, then append .vc to the name.
s/^[-       ]*include\(.*\)$/!include \1.vc/

'  < $infile >> $outfile

# Uncomment this line for debugging
#diff $infile $outfile
