Cleanup emsdk_env scripts (#539)

Remove the optional argument to contruct_env.  It simple if we
always construct the env in the same place.  Avoid using temp files,
and avoid changing directory.
diff --git a/emsdk.py b/emsdk.py
index 2bf839e..1f1d85b 100755
--- a/emsdk.py
+++ b/emsdk.py
@@ -2490,6 +2490,9 @@
   copy_pregenerated_cache(tools_to_activate)
 
   # Construct a .bat script that will be invoked to set env. vars and PATH
+  # We only do this on windows since emsdk.bat is able to modify the
+  # calling shell environment.  On other platform `source emsdk_env.sh` is
+  # required.
   if WINDOWS:
     env_string = construct_env(tools_to_activate)
     open(EMSDK_SET_ENV, 'w').write(env_string)
@@ -2612,11 +2615,11 @@
       print('')
 
   # A core variable EMSDK points to the root of Emscripten SDK directory.
-  env_vars_to_add = [('EMSDK', to_unix_path(emsdk_path()))]
+  env_vars = [('EMSDK', to_unix_path(emsdk_path()))]
 
   em_config_path = os.path.normpath(dot_emscripten_path())
   if to_unix_path(os.environ.get('EM_CONFIG', '')) != to_unix_path(em_config_path):
-    env_vars_to_add += [('EM_CONFIG', em_config_path)]
+    env_vars += [('EM_CONFIG', em_config_path)]
 
   for tool in tools_to_activate:
     config = tool.activated_config()
@@ -2624,14 +2627,18 @@
       # For older emscripten versions that don't use this default we export
       # EM_CACHE.
       em_cache_dir = os.path.join(config['EMSCRIPTEN_ROOT'], 'cache')
-      env_vars_to_add += [('EM_CACHE', em_cache_dir)]
+      env_vars += [('EM_CACHE', em_cache_dir)]
     envs = tool.activated_environment()
     for env in envs:
       key, value = parse_key_value(env)
       value = to_native_path(tool.expand_vars(value))
-      # Don't set env vars which are already set to the correct value.
-      if key not in os.environ or to_unix_path(os.environ[key]) != to_unix_path(value):
-        env_vars_to_add += [(key, value)]
+      env_vars += [(key, value)]
+
+  # Don't set env vars which are already set to the correct value.
+  env_vars_to_add = []
+  for key, value in env_vars:
+    if key not in os.environ or to_unix_path(os.environ[key]) != to_unix_path(value):
+      env_vars_to_add.append((key, value))
 
   if env_vars_to_add:
     print('Setting environment variables:')
@@ -3006,19 +3013,15 @@
 
     return 0
   elif cmd == 'construct_env':
-    if len(sys.argv) == 2:
-      outfile = EMSDK_SET_ENV
-      # Clean up old temp file up front, in case of failure later before we get
-      # to write out the new one.
-      silentremove(EMSDK_SET_ENV)
-    else:
-      outfile = sys.argv[2]
+    # Clean up old temp file up front, in case of failure later before we get
+    # to write out the new one.
+    silentremove(EMSDK_SET_ENV)
     tools_to_activate = currently_active_tools()
     tools_to_activate = process_tool_list(tools_to_activate, log_errors=True)
     env_string = construct_env(tools_to_activate)
-    open(outfile, 'w').write(env_string)
+    open(EMSDK_SET_ENV, 'w').write(env_string)
     if UNIX:
-      os.chmod(outfile, 0o755)
+      os.chmod(EMSDK_SET_ENV, 0o755)
     return 0
   elif cmd == 'update':
     update_emsdk()
diff --git a/emsdk_env.bat b/emsdk_env.bat
index 1f8d165..c793321 100644
--- a/emsdk_env.bat
+++ b/emsdk_env.bat
@@ -1 +1 @@
-@call "%~dp0emsdk" construct_env %*
+@call "%~dp0emsdk" construct_env
diff --git a/emsdk_env.csh b/emsdk_env.csh
index 54cdf0e..7987a3b 100755
--- a/emsdk_env.csh
+++ b/emsdk_env.csh
@@ -21,16 +21,13 @@
   set SRC="$SRC[2]"
 endif
 set CURDIR=`pwd`
-cd `dirname "$SRC"`
+set DIR=`dirname "$SRC"`
 unset SRC
 
 setenv EMSDK_CSH 1
 
-set tmpfile=`mktemp` || exit 1
-./emsdk construct_env $tmpfile
-source $tmpfile
-rm -f $tmpfile
+$DIR/emsdk construct_env
+source $DIR/emsdk_set_env.csh
+unset DIR
 
 unsetenv EMSDK_CSH
-
-cd "$CURDIR"
diff --git a/emsdk_env.fish b/emsdk_env.fish
index 99119b7..e4aea06 100755
--- a/emsdk_env.fish
+++ b/emsdk_env.fish
@@ -6,12 +6,8 @@
 set -l script (status -f)
 set -l dir (dirname $script)
 
-pushd $dir > /dev/null
-
-./emsdk construct_env
-. ./emsdk_set_env.sh
+$dir/emsdk construct_env
+. $dir/emsdk_set_env.sh
 
 set -e -l script
 set -e -l dir
-
-popd > /dev/null
diff --git a/emsdk_env.ps1 b/emsdk_env.ps1
index fc7f2c7..ab5fc4d 100644
--- a/emsdk_env.ps1
+++ b/emsdk_env.ps1
@@ -1,2 +1,2 @@
 $ScriptDirectory = Split-Path -parent $PSCommandPath
-& "$ScriptDirectory/emsdk.ps1" construct_env $args
+& "$ScriptDirectory/emsdk.ps1" construct_env
diff --git a/emsdk_env.sh b/emsdk_env.sh
index 1e972a3..9c0a5c7 100755
--- a/emsdk_env.sh
+++ b/emsdk_env.sh
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 # This script is sourced by the user and uses
 # their shell. Try not to use bashisms.
 
@@ -15,18 +15,13 @@
 #     ./emsdk_env.sh
 #
 # which won't have any effect.
-SRC="$BASH_SOURCE"
-if [ "$SRC" = "" ]; then
-  SRC="$0"
+DIR="$BASH_SOURCE"
+if [ "$DIR" = "" ]; then
+  DIR="$0"
 fi
-CURDIR="$(pwd)"
-cd "$(dirname "$SRC")"
-unset SRC
+DIR="$(dirname "$DIR")"
 
-tmpfile=`mktemp` || exit 1
 # Force emsdk to use bash syntax so that this works in windows + bash too
-EMSDK_BASH=1 ./emsdk construct_env $tmpfile
-. $tmpfile
-rm -f $tmpfile
-
-cd "$CURDIR"
+EMSDK_BASH=1 $DIR/emsdk construct_env
+. $DIR/emsdk_set_env.sh
+unset DIR