# 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 prod vers v vsroot
  vers=( $(regtool list /HKLM/SOFTWARE/Microsoft/VisualStudio | sort -rn) )
  # Under Visual Studio 6.0 the key is apparently:
  #  HKLM/SOFTWARE/Microsoft/VisualStudio/6.0/Setup/Microsoft Visual Studio/ProductDir
  # Under Visual Studio 8.0, the key is apparently:
  #  HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/VisualStudio/8.0/Setup/VS/ProductDir
  for v in $vers ; do
    for prod in "VS" "Microsoft Visual Studio" ; do
      if vsroot=$(regtool get -e "/HKLM/SOFTWARE/Microsoft/VisualStudio/${v}/Setup/$prod/ProductDir" 2>/dev/null) 2>/dev/null; then
        vsroot=$(cygpath -m $vsroot)
        [[ -d $vsroot ]] && print $vsroot
        return 0
      fi
    done
  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 -u2 -R "The root of the Visual C++ install, \"$VC_ROOT\", does not exist.  Set the environment variable VC_ROOT."
  return 1
fi

# Find vcvars32.bat, which sets up the Visual Studio environment.
cyg_VS_INIT=$(find $cyg_VC_ROOT -iname 'vcvars32.bat' | sed 1q)
if [[ ! -e $cyg_VS_INIT ]]; then
  print -u2 -R "Could not find vcvars32.bat to initialize the Visual Studio environment"
  return 1
fi
EFILE=$$.tmp
VS_INIT="$(cygpath -w $cyg_VS_INIT)"
# This is a little bit gross, because we're exporting the
# entire environment when all we want are the variables set by
# the vcvars32.bat file.  However, a more surgical alternative:
#env - PATH=$PATH cmd >&/dev/null <<EOF
#...
#EOF
# fails, because vcvars32 depends on the environment variable
# VSnnCOMNTOOLS, where nn is the version number.  In order to use
# this approach, we'd have to know the names of all such variables
# to include in the environment, so the command would look like this:
#env - PATH=$PATH VS80COMNTOOLS=$VS80COMNTOOLS VS90COMNTOOLS=$VS90COMNTOOLS ...
# Once you add in the same potential problems with the SDK and Intel
# compiler, I currently think this is a better approach.
cmd >&/dev/null <<EOF
  "$VS_INIT"
  env >$EFILE
EOF

# 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 -u2 -R "The SDK root directory, \"$SDK_ROOT\", does not exist.  Set the environment variable SDK_ROOT."
    rm $EFILE
    return 2
  fi
  cyg_SDK_INIT=$(find $cyg_SDK_ROOT -iname 'SetEnv.cmd' | sed 1q)
  if [[ ! -e $cyg_SDK_INIT ]]; then
    print -u2 -R "Could not find SetEnv.cmd to initialize the platform SDK environment"
    rm $EFILE
    return 2
  fi
  SDK_INIT="$(cygpath -w $cyg_SDK_INIT)"
  cmd >&/dev/null <<EOF
    "$SDK_INIT"
    env >>$EFILE
EOF
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 -u2 -R "The intel root directory, \"$INTEL_ROOT\" does not exist.  Set the environment variable INTEL_ROOT."
    rm $EFILE
    return 3
  fi
  cyg_ICL_INIT=$(find $cyg_INTEL_ROOT -iname 'iclvars.bat' | sed 1q)
  if [[ ! -e $cyg_ICL_INIT ]]; then
    print -u2 -R "Could not find vcvars32.bat to initialize the Intel C Compiler environment"
    rm $EFILE
    return 2
  fi
  ICL_INIT="$(cygpath -w $cyg_ICL_INIT)"
  cmd >&/dev/null <<EOF
    "$ICL_INIT" ia32
    env >>$EFILE
EOF
fi

# Read in the environment file and export all the variables listed there.
for e in "${(f)$(<$EFILE)}"; do
  eval export "$(print -R '$e')"
done >&/dev/null
rm $EFILE

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

return 0

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