
# Set up for running Visual C++ under Cygwin
# This script must be sourced under zsh.

function computeIntelRoot {
  local iroot ver
  ver=$(regtool list /HKLM/SOFTWARE/Intel/Compilers/C++/)
  iroot=$(regtool get -e /HKLM/SOFTWARE/Intel/Compilers/C++/${ver}/iA32/ProductDir)
  iroot=$(cygpath -m $iroot)
  [[ -d $iroot ]] || iroot="C:/Program Files/Intel/Compiler/C++/10.1.011/IA32"
  print $iroot
}

function computeSDKRoot {
  local sdkroot v vers
  vers=( $(regtool list /HKLM/SOFTWARE/Microsoft/MicrosoftSDK/InstalledSDKs) )
  for v in $vers ; do
    if sdkroot=$(regtool get -e "/HKLM/SOFTWARE/Microsoft/MicrosoftSDK/InstalledSDKs/${v}/Install Dir" 2>/dev/null); then
      sdkroot=$(cygpath -m $sdkroot)
      [[ -d $sdkroot ]] && print $sdkroot
      return 0
    fi
  done
  print "C:/Program Files/Microsoft Platform SDK/"
}

function computeVCRoot {
  local vers v vsroot
  vers=( $(regtool list /HKLM/SOFTWARE/Microsoft/VisualStudio | sort -rn) )
  for v in $vers ; do
    if vsroot=$(regtool get -e "/HKLM/SOFTWARE/Microsoft/VisualStudio/${v}/Setup/Microsoft Visual Studio/ProductDir" 2>/dev/null); then
      vsroot=$(cygpath -m $vsroot)
      [[ -d $vsroot ]] && print $vsroot
      return 0
    fi
  done
  print "C:/Program Files/Microsoft Visual Studio"
}

# Visual Studio, SDK and Intel roots which can be over-ridden.
# The Visual Studio root must exist, but the SDK and Intel roots are optional.
: ${VC_ROOT:=$(computeVCRoot)}

cyg_VC_ROOT=$(cygpath $VC_ROOT)
if [[ ! -d $cyg_VC_ROOT ]]; then
  print -R -u2 "The root of the Visual C++ install, \"$VC_ROOT\", does not exist.  Set the environment variable VC_ROOT."
  return 1
fi

# Set up the environment for a VC build
export INCLUDE="$VC_ROOT/VC98/atl/include;$VC_ROOT/VC98/mfc/include;$VC_ROOT/VC98/include"
export LIB="$VC_ROOT/VC98/mfc/lib;$VC_ROOT/VC98/lib"
# Insert extra directories after /usr/local/bin
path[(r)/usr/local/bin]=( /usr/local/bin "$cyg_VC_ROOT/Common/Tools/WinNT" "$cyg_VC_ROOT/Common/MSDev98/Bin" "$cyg_VC_ROOT/Common/Tools" "$cyg_VC_ROOT/VC98/bin" )

# Add SDK paths only if requested by a command-line argument
if [[ $* == *SDK* ]]; then
: ${SDK_ROOT:=$(computeSDKRoot)}
  cyg_SDK_ROOT=$(cygpath $SDK_ROOT)
  if [[ ! -d $cyg_SDK_ROOT ]]; then
    print -R -u2 "The SDK root directory, \"$SDK_ROOT\", does not exist.  Set the environment variable SDK_ROOT."
    return 2
  fi
  INCLUDE="$SDK_ROOT/Include;$INCLUDE"
  LIB="$SDK_ROOT/Lib;$LIB"
  path[(r)/usr/local/bin]=( /usr/local/bin "$cyg_SDK_ROOT/Bin" "$cyg_SDK_ROOT/Bin/WinNT" )
fi

# Add Intel paths only if requested by a command-line argument
if [[ $* == *intel* ]]; then
: ${INTEL_ROOT:=$(computeIntelRoot)}
  cyg_INTEL_ROOT=$(cygpath $INTEL_ROOT)
  if [[ ! -d $cyg_INTEL_ROOT ]]; then
    print -R -u2 "The intel root directory, \"$INTEL_ROOT\" does not exist.  Set the environment variable INTEL_ROOT."
    return 3
  fi
  INCLUDE="$INTEL_ROOT/;$INCLUDE"
  LIB="$INTEL_ROOT/Lib;$LIB"
  path[(r)/usr/local/bin]=( /usr/local/bin "$cyg_INTEL_ROOT/Bin" )
fi

#print -R INCLUDE=\"$INCLUDE\"
#print -R LIB=\"$LIB\"
#print -R path=\"$path\"

return 0

# Local Variables:
# mode: ksh
# sh-indentation: 2
# indent-tabs-mode: nil
# End:
