#!/bin/bash

# This script invokes Microsoft Visual Studio C compiler from CygWin shell.
# The script command line parameters format is similar to GCC command line.
# Command line options:
#  -c   compile only
#  -g   generate debug info
#  -O   enable optimizations
#  -o<file>  output file name
#  -D<name>  macro definition
#  -I<dir>   include directory
#  -p<file>  PDB file name

if [ -z "$VSHOME" ] ; then
    for KEY in \
        "Microsoft/VisualStudio/10.0/Setup/VS/ProductDir" \
        "Wow6432Node/Microsoft/VisualStudio/10.0/Setup/VS/ProductDir" \
        "Microsoft/VisualStudio/9.0/Setup/VS/ProductDir" \
        "Wow6432Node/Microsoft/VisualStudio/9.0/Setup/VS/ProductDir"
    do
        KEY="/proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/$KEY"
        if [ -e "$KEY" ] ; then
            KEY="`cat "$KEY"`"
            KEY="`cygpath -u "$KEY"`"
            if [ -d "$KEY" ] ; then
                VSHOME="$KEY"
                break
            fi
        fi
    done
    if [ -z "$VSHOME" ] ; then
        for VAR in VS100COMNTOOLS VS90COMNTOOLS
        do
            DIR="${!VAR}"
            if [ ! -z "$DIR" ] ; then
                DIR="`cygpath -u "$DIR"`"
                if [ -d "$DIR" ] ; then
                    VSHOME="$DIR/../.."
                    break
                fi
            fi
        done
        if [ -z "$VSHOME" ] ; then
            for DIR in \
                "/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 10.0" \
                "/cygdrive/c/Program Files/Microsoft Visual Studio 10.0" \
                "/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 9.0" \
                "/cygdrive/c/Program Files/Microsoft Visual Studio 9.0"
            do
                if [ -d "$DIR" ] ; then
                    VSHOME="$DIR"
                    break
                fi
            done
        fi
    fi
fi

if [ ! -d "$VSHOME" ] ; then
    echo Invalid VSHOME - Microsoft Visual Studio directory name
    exit 1
fi

if [ -z "$WINSDK" ] ; then
    for KEY in \
        "Microsoft/Microsoft SDKs/Windows/CurrentInstallFolder" \
        "Wow6432Node/Microsoft/Microsoft SDKs/Windows/CurrentInstallFolder"
    do
        KEY="/proc/registry/HKEY_LOCAL_MACHINE/SOFTWARE/$KEY"
        if [ -e "$KEY" ] ; then
            KEY="`cat "$KEY"`"
            KEY="`cygpath -u "$KEY"`"
            if [ -d "$KEY" ] ; then
                WINSDK="$KEY"
                break
            fi
        fi
    done
    if [ -z "$WINSDK" ] ; then
        for DIR in \
            "/cygdrive/c/Program Files (x86)/Microsoft SDKs/Windows/v7.1" \
            "/cygdrive/c/Program Files/Microsoft SDKs/Windows/v7.1" \
            "/cygdrive/c/Program Files (x86)/Microsoft SDKs/Windows/v7.0A" \
            "/cygdrive/c/Program Files/Microsoft SDKs/Windows/v7.0A" \
            "/cygdrive/c/Program Files/Microsoft SDKs/Windows/v6.1" \
            "/cygdrive/c/Program Files/Microsoft SDKs/Windows/v6.0A"
        do
            if [ -d "$DIR" ] ; then
                WINSDK="$DIR"
                break
            fi
        done
    fi
fi

if [ ! -d "$WINSDK" ] ; then
    echo Invalid WINSDK - Microsoft SDK directory name
    exit 1
fi

declare -a cmd
cmdpos=0

cflag=0
gflag=0
Oflag=0
language=
oval=

while getopts co:D:I:gOp:x: name
do
    case $name in
    c)
        cmd[cmdpos]="/c"
        cmdpos=`expr $cmdpos + 1`
        cflag=1
        ;;
    g)
        gflag=1
        ;;
    O)
        Oflag=1
        ;;
    o)
        oval="$OPTARG"
        ;;
    D)
        cmd[cmdpos]="/D$OPTARG"
        cmdpos=`expr $cmdpos + 1`
        ;;
    I)
        cmd[cmdpos]="/I`cygpath -m "$OPTARG"`"
        cmdpos=`expr $cmdpos + 1`
        ;;
    p)
        cmd[cmdpos]="/Fd`cygpath -m "$OPTARG"`"
        cmdpos=`expr $cmdpos + 1`
        ;;
    x)
        language="$OPTARG"
        ;;
    *)
        echo Invalid option $name
        exit 2
        ;;
    esac
done

if [ $cflag != 0 ] ; then
  if [ "$language" = "" -o "$language" = "c" ] ; then
    cmd[cmdpos]="/TC"
    cmdpos=`expr $cmdpos + 1`
  elif [ "$language" = "c++" ] ; then
    cmd[cmdpos]="/TP"
    cmdpos=`expr $cmdpos + 1`
  else
    echo "Invalid value of -x"
    exit 1
  fi
fi

shift `expr $OPTIND - 1`

if [ ! -z "$oval" ] ; then
    if [ $cflag = 0 ] ; then
        cmd[cmdpos]="/Fe$oval"
    else
        cmd[cmdpos]="/Fo$oval"
    fi
    cmdpos=`expr $cmdpos + 1`
fi

if [ $gflag = 1 ] ; then
    CFLAGS1="/D_DEBUG /Zi /MTd"
else
    CFLAGS1="/DNDEBUG /GF /Gy /FD /MT"
fi
if [ $Oflag = 0 ] ; then
    CFLAGS2="/Od"
else
    CFLAGS2="/O2 /Ob1"
fi
CFLAGS3="/Oy- /DWIN32 /D_CONSOLE /D_VC80_UPGRADE=0x0600 /D_MBCS /W4"

export VSINSTALLDIR=$(cygpath -aw "$VSHOME")
export VCINSTALLDIR=$(cygpath -aw "$VSHOME/VC")
export LIB=$(cygpath -aw "$VSHOME/VC/lib")\;$(cygpath -aw "$WINSDK/Lib")
export INCLUDE=$(cygpath -aw "$VSHOME/VC/include")\;$(cygpath -aw "$WINSDK/Include")
export PATH="$VSHOME/Common7/IDE:$VSHOME/VC/bin:$VSHOME/Common7/Tools:$VSHOME/VC/VCPackages:$PATH"

cl.exe /nologo $CFLAGS1 $CFLAGS2 $CFLAGS3 "${cmd[@]}" "$@" || exit 1
