Index: /trunk/env.cmd
===================================================================
--- /trunk/env.cmd	(revision 3027)
+++ /trunk/env.cmd	(revision 3028)
@@ -6,7 +6,25 @@
 /** @file
  *
- * LIBC Development Environment Script.
- *
- * Copyright (c) 1999-2005 Knut St. Osmundsen <bird@anduin.net>
+ * LIBC Development Environment Setup Script.
+ * 
+ *
+ * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net>
+ *
+ *
+ * This file is part of kLIBC.
+ *
+ * kLIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * kLIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with kLIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
@@ -23,19 +41,4 @@
     end
 
-say ' WARNING:'
-say '   This is work in progress, and will probably not suit your'
-say '   environment at the moment!'
-say ''
-
-    /*
-     * Constants.
-     */
-    asBuildTypes.0 = 2;
-    asBuildTypes.1 = 'debug';
-    asBuildTypes.2 = 'release';
-    asStages.0 = 2
-    asStages.1 = 'stage1'
-    asStages.2 = 'stage2'
-
     /*
      * Apply CMD.EXE workaround.
@@ -44,13 +47,16 @@
 
     /*
-     * Parse argument
-     */
-    parse arg sArgs
+     * Parse argument.
+     */
     fRm = 0;
-    fUseStagedGcc = 1;
+    sPathGcc = '';
+    sPathUnix = EnvGet('UNIXROOT');
+    sPathkBuild = EnvGet('PATH_KBUILD');
+    fWithBuiltStuff = 1;
     fUseBuildEnv = 1;
     sBuildType = 'debug';
     sBuildTarget = 'os2';
     sBuildArch = 'x86';
+    parse arg sArgs
     do i = 1 to words(sArgs)
         /* extract word */
@@ -62,9 +68,34 @@
             sArg = strip(strip(sArg, 'L', '/'), 'L', '-');
             select
-                when (sArg = 'disable-staged-gcc') then
-                    fUseStagedGcc = 0;
-                when (sArg = 'enable-staged-gcc') then
-                    fUseStagedGcc = 1;
-
+                when (sArg = 'gcc') then
+                do
+                    if i = words(sArgs) then 
+                    do
+                        say 'syntax error: '''sArg''' is missing the path.'
+                        exit(12);                         
+                    end
+                    i = i + 1;
+                    sPathGcc = word(sArgs, i);
+                end
+                when (sArg = 'unixroot') then
+                do
+                    if i = words(sArgs) then 
+                    do
+                        say 'syntax error: '''sArg''' is missing the path.'
+                        exit(12);                         
+                    end
+                    i = i + 1;
+                    sPathUnix = word(sArgs, i);
+                end
+                when (sArg = 'kbuild' | sArg = 'kBuild') then
+                do
+                    if i = words(sArgs) then 
+                    do
+                        say 'syntax error: '''sArg''' is missing the path.'
+                        exit(12);                         
+                    end
+                    i = i + 1;
+                    sPathkBuild = word(sArgs, i);
+                end
                 when (sArg = 'uninstall') then
                     fRM = 1;
@@ -80,13 +111,13 @@
         end
         else
+        do
             sBuildType = ToLower(sArg);
-    end
-
-    if (sBuildType <> 'release' & sBuildType <> 'debug') then
-    do
-        call syntax;
-        exit(12);
-    end
-
+            if (sBuildType <> 'release' & sBuildType <> 'debug') then
+            do
+                say 'syntax error: Invalid build type '''sBuildType'''. valid types are: release and debug'
+                exit(12);
+            end
+        end
+    end
 
     /*
@@ -95,21 +126,50 @@
      */
     parse source . . sSrc
-    sPathRoot   = filespec('drive', sSrc) || strip(filespec('path', sSrc), 'T', '\');
-    sPathRootF  = translate(sPathRoot, '/', '\');
-
-    /*
-     * Figure where the tools directory is.
+    sPathRoot   = ToDosSlash(FixPath(filespec('drive', sSrc) || strip(filespec('path', sSrc), 'T', '\')));
+    sPathRootF  = ToUnixSlash(sPathRoot);
+    if (substr(sPathRootF, 1, 2) = '//') then 
+    do
+        say 'error: script invoked with UNC path. that won''t work.'
+        exit(12);
+    end
+
+    /*
+     * Figure where the UNIXROOT is.
      *
-     * The tools directory is where we've got all the precompiled kNIX
-     * binaries and headers which are required to bootstrap kNIX. In
-     * GenToo terminology this is 'stage1'.
-     */
-    sPathTools  = sPathRoot || '\tools';
-    sPathToolsF = translate(sPathTools, '/', '\');
-    sPathToolsFD= translate(filespec('path', sPathTools), '/', '\') || 'tools';
+     * The UNIXROOT is one or our parent directories, it is where all the 
+     * unix/gnu tools are installed. It is also where you will install
+     * the packages that we compiler. If not specified we will got hunting 
+     * for it...
+     */
+    if (sPathUnix = '') then
+    do
+        sPathUnix = ToDosSlash(sPathRoot);
+        do while (1)
+            say 'sPathUnix='sPathUnix
+            if (DirExists(sPathUnix'\usr\bin')) then 
+                leave;
+
+            /* next */
+            sTmp = filespec('drive', sPathUnix)||strip(filespec('path', sPathUnix), 'T', '\');
+            if (sTmp = sPathUnix) then
+            do
+                say 'error: cannot find a suitable unix root directory. Use the --unixroot <path> option.'
+                exit(12)
+            end
+            if (length(sTmp) == 2) then 
+                sTmp = sTmp||'\';
+            sPathUnix = sTmp;
+        end
+        say 'info: detected unix root at '''sPathUnix'''.';
+    end
+    sPathUnix = ToDosSlash(FixPath(sPathUnix));
+    sPathUnixF = ToUnixSlash(sPathUnix);
+    sPathUnixFD= ToUnixSlash(filespec('path', sPathUnix)||filespec('name', sPathUnix));
+    if (ToLower(sPathUnixF) <> substr(ToLower(sPathRootF), 1, length(sPathUnixF))) then 
+        say 'warning: the source tree is not under the unix root.';
 
     /*
      * Figure where the output directory is.
-     * (kBuild actually decides this)
+     * (kBuild actually decides this, so kee it in sync.)
      */
     sPathOut  = sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sBuildType
@@ -119,12 +179,11 @@
      * Check for kBuild.
      */
-    sPathkBuild = EnvGet('PATH_KBUILD');
     if (sPathkBuild = '') then
         sPathkBuild = sPathRoot'\kBuild';
-    sPathkBuild  = translate(sPathkBuild, '\', '/');
-    sPathkBuildF = translate(sPathkBuild, '/', '\');
+    sPathkBuild  = ToDosSlash(FixPath(sPathkBuild));
+    sPathkBuildF = ToUnixSlash(sPathkBuild);
     if (\FileExists(sPathkBuild'\footer.kmk')) then
     do
-        say 'fatal error: cannot find kBuild!';
+        say 'fatal error: cannot find a kBuild valid installation!';
         say 'check the build instructions (web) for how to get and install kBuild!';
         exit(8);
@@ -132,186 +191,54 @@
 
     /*
+     * Check the gcc path.
+     */
+    if (FileExists(sPathGcc'/usr/bin/g++.exe')) then 
+        sPathGcc = sPathGcc'/usr';
+    sPathGcc = FixPath(sPathGcc);
+    if (\FileExists(sPathGcc'/bin/g++.exe')) then 
+    do
+        say 'fatal error: cannot find g++.exe at the specific gcc location: 'sPathGcc;
+        say 'Use the --gcc <path> option to point to a proper gcc installation.'
+        exit(8);
+    end
+
+    /*
      * Setup the environment.
      */
     /* cleanup */
+    asBuildTypes.0 = 2;
+    asBuildTypes.1 = 'debug';
+    asBuildTypes.2 = 'release';
+    asStages.0 = 2
+    asStages.1 = 'stage1'
+    asStages.2 = 'stage2'
     do i = 1 to asBuildTypes.0
-        sMode = asBuildTypes.i;
-        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\dist\bin;'
-        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\dist\usr\omfhackbin;'
-        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\dist\usr\bin;'
-        call EnvAddFront 1, 'PATH',                 sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/bin;'
-        call EnvAddFront 1, 'PATH',                 sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/bin;'
-        call EnvAddFront 1, 'C_INCLUDE_PATH',       sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/include;'
-        call EnvAddFront 1, 'CPLUS_INCLUDE_PATH',   sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/include;'
-        call EnvAddFront 1, 'COBJ_INCLUDE_PATH',    sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/include;'
-        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/lib;'
-        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/lib;'
-        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sMode'/dist/usr/lib/gcc-lib/i386-pc-os2-emx/3.3.5;'
+        sType = asBuildTypes.i;
+        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\dist\bin;'
+        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\dist\usr\omfhackbin;'
+        call EnvAddFront 1, 'PATH',                 sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\dist\usr\bin;'
+        call EnvAddFront 1, 'PATH',                 sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/bin;'
+        call EnvAddFront 1, 'PATH',                 sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/bin;'
+        call EnvAddFront 1, 'C_INCLUDE_PATH',       sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/include;'
+        call EnvAddFront 1, 'CPLUS_INCLUDE_PATH',   sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/include;'
+        call EnvAddFront 1, 'COBJ_INCLUDE_PATH',    sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/include;'
+        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/lib;'
+        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/lib;'
+        call EnvAddFront 1, 'LIBRARY_PATH',         sPathRootF'/out/'sBuildTarget'.'sBuildArch'/'sType'/dist/usr/lib/gcc-lib/i386-pc-os2-emx/3.3.5;'
         do j = 1 to asStages.0
-            call EnvAddFront 1,'BEGINLIBPATH',      sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\obj\gcc\gcc\'asStages.j';'
+            call EnvAddFront 1,'BEGINLIBPATH',      sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\obj\gcc\gcc\'asStages.j';'
         end
-        call EnvAddFront 1,  'BEGINLIBPATH',        sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\dist\lib;'
-        call EnvAddFront 1,  'BEGINLIBPATH',        sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sMode'\dist\usr\lib;'
+        call EnvAddFront 1,  'BEGINLIBPATH',        sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\dist\lib;'
+        call EnvAddFront 1,  'BEGINLIBPATH',        sPathRoot'\out\'sBuildTarget'.'sBuildArch'\'sType'\dist\usr\lib;'
     end
     if (fUseBuildEnv) then
+    do
         'call BuildEnv.cmd unixõ vac365õ vac308õ toolkit452õ toolkit40õ emxõ emxpgccõ gcc302õ gcc303õ gcc321õ gcc322õ gcc335õ'
-
-    /* Basics */
-    if (fUseBuildEnv & \fRm) then
-        'call BuildEnv.cmd cvs~ emx~ ';/*svn~'*/
-
-    /* We do not want any influence from the emx, that's only runtime stuff. */
-    call EnvSet 0, 'LDPATH'
-    call EnvSet 0, 'LIBRARY_PATH'
-    call EnvSet 0, 'C_INCLUDE_PATH'
-    call EnvSet 0, 'CPLUS_INCLUDE_PATH'
-    call EnvSet 0, 'COBJ_INCLUDE_PATH'
-
-    /* GCC */
-    if (fUseStagedGcc | fRm) then
-        call GCC3xx 'gcc335', 'install', fRm, 1, 'gcc335'
-    else
-    do
-        'call BuildEnv.cmd gcc335'
-        call EnvAddFront fRm, 'LIBRARY_PATH', translate(EnvGet('PATH_IGCC'), '/', '\')'/lib;'
-        call EnvAddFront fRm, 'LIBRARY_PATH', translate(EnvGet('PATH_IGCC'), '/', '\')'/lib/gcc-lib/i386-pc-os2-emx/3.3.5/;'
-    end
-
-    /* ilink */
-    if (fileExists(sPathTools'\x86.os2\vacpp\ilink\ilink.exe')) then
-    do
-        call EnvAddFront fRm, 'PATH',               sPathTools'\x86.os2\vacpp\ilink;';
-        call EnvAddFront fRm, 'DPATH',              sPathTools'\x86.os2\vacpp\ilink;';
-        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathTools'\x86.os2\vacpp\ilink;';
-        call EnvSet      fRm, 'EMXOMFLD_LINKER',    'ilink'
-        call EnvSet      fRm, 'EMXOMFLD_TYPE',      'VAC365'
-    end
-
-    /* icsdebug */
-    if (fileExists(sPathTools'\x86.os2\vacpp\icsdebug\bin\icsdebug.exe')) then
-    do
-        call EnvAddFront fRm, 'PATH',               sPathTools'\x86.os2\vacpp\icsdebug\bin;';
-        call EnvAddFront fRm, 'DPATH',              sPathTools'\x86.os2\vacpp\icsdebug\help;';
-        call EnvAddFront fRm, 'HELP',               sPathTools'\x86.os2\vacpp\icsdebug\help;';
-        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathTools'\x86.os2\vacpp\icsdebug\dll;';
-        call EnvAddFront fRm, 'LOCPATH',            sPathTools'\x86.os2\vacpp\icsdebug\locale;';
-    end
-
-    /* idebug */
-    if (fileExists(sPathTools'\x86.os2\vacpp\idebug\bin\idebug.exe')) then
-    do
-        call EnvAddFront fRm, 'PATH',               sPathTools'\x86.os2\vacpp\idebug\bin;';
-        call EnvAddFront fRm, 'DPATH',              sPathTools'\x86.os2\vacpp\idebug\help;';
-        call EnvAddFront fRm, 'HELP',               sPathTools'\x86.os2\vacpp\idebug\help;';
-        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathTools'\x86.os2\vacpp\idebug\dll;';
-        call EnvAddFront fRm, 'NLSPATH',            sPathTools'\x86.os2\vacpp\icsdebug\msg\%N;';
-    end
-
-    /* other */
-    call EnvSet      fRm, 'BUILD_PROJECT',      'GCCOS2'
-    call EnvSet      fRm, 'BUILD_MODE',         translate(sBuildType) /* deprecated */
-    call EnvSet      fRm, 'PATH_TOP',           sPathRootF
-    call EnvSet      fRm, 'PATH_TOPD',          substr(sPathRootF, 3)
-    call EnvSet      fRm, 'PATH_OBJ',           sPathOutF
-    call EnvSet      fRm, 'PATH_OBJD',          substr(sPathOutF, 3)
-
-    /* kBuild. */
-    call EnvSet      fRm, 'PATH_KBUILD',        sPathkBuildF
-    call EnvAddFront fRm, 'PATH',               sPathkBuild'\bin\os2.x86;'
-    call EnvAddFront fRm, 'BEGINLIBPATH',       sPathkBuild'\bin\os2.x86;'
-    call EnvSet      fRm, 'BUILD_TYPE',         sBuildType
-    call EnvSet      fRm, 'BUILD_TARGET',       sBuildTarget
-    call EnvSet      fRm, 'BUILD_TARGET_ARCH',  'x86'
-    call EnvSet      fRm, 'BUILD_TARGET_CPU',   'i386'
-    call EnvSet      fRm, 'BUILD_PLATFORM',     sBuildTarget
-    call EnvSet      fRm, 'BUILD_PLATFORM_ARCH','x86'
-    call EnvSet      fRm, 'BUILD_PLATFORM_CPU', 'i386'
-
-    /* unixroot - todo: check for this and don't override existing unixroot! */
-    call EnvSet      fRm, 'UNIXROOT',           sPathToolsF
-    call EnvAddFront 1,   'PATH',               '.;' /* the sucker gets removed by 'buildenv toolkit40õ' */
-    call EnvAddFront fRm, 'PATH',               sPathToolsF'/usr/bin;'
-    call EnvAddFront fRm, 'PATH',               sPathTools'\usr\bin;'
-    call EnvAddFront fRm, 'PATH',               sPathToolsF'/bin;'
-    call EnvAddFront fRm, 'PATH',               sPathTools'\bin;'
-    call EnvAddFront fRm, 'BEGINLIBPATH',       sPathTools'\lib;'sPathTools'\bin;'sPathTools'\usr\bin;'sPathTools'\usr\lib;'
-
-    do i = 1 to asStages.0
-        call EnvAddFront fRm,'BEGINLIBPATH',    sPathOut'\gcc\gcc\'asStages.i';'
-    end
-
-    /* hmm do I actually want this in the path? */
-/* at present we don't
-    call EnvAddFront fRm, 'C_INCLUDE_PATH',     sPathToolsF'/usr/include;'
-    call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sPathToolsF'/usr/include;'
-    call EnvAddFront fRm, 'OBJC_INCLUDE_PATH',  sPathToolsF'/usr/include;'
-    call EnvAddFront fRm, 'LIBRARY_PATH',       sPathToolsF'/usr/lib;'
-*/
-
-    /* build tools? */
-    call EnvSet      fRm, 'PATH_BUILTUNIX',     sPathOutF'/dist'
-    call EnvAddFront fRm, 'PATH',               sPathOutF'/dist/usr/bin;'
-    call EnvAddFront fRm, 'PATH',               sPathOut'\dist\usr\bin;'
-    call EnvAddFront fRm, 'PATH',               sPathOutF'/dist/bin;'
-    call EnvAddFront fRm, 'PATH',               sPathOut'\dist\bin;'
-    call EnvAddFront fRm, 'PATH',               sPathOut'\dist/usr\omfhackbin;' /* remove me! */
-    call EnvAddFront fRm, 'BEGINLIBPATH',       sPathOut'\dist\usr\lib;'
-    call EnvAddFront fRm, 'C_INCLUDE_PATH',     sPathOutF'/dist/usr/include;'
-    call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sPathOutF'/dist/usr/include;'
-    call EnvAddFront fRm, 'OBJC_INCLUDE_PATH',  sPathOutF'/dist/usr/include;'
-    call EnvAddFront fRm, 'LIBRARY_PATH',       sPathOutF'/dist/usr/lib;'
-    call EnvAddFront fRm, 'LIBRARY_PATH',       sPathOutF'/dist/usr/lib/gcc-lib/i386-pc-os2-emx/3.3.5;'
-
-    call EnvSet      1,   'AC_PREFIX'
-    call EnvSet      1,   'AC_MACRODIR'
-    call EnvSet      fRm, 'PATH_SEPARATOR',     ';'
-    call EnvSet      fRm, 'CONFIG_SHELL',       sPathToolsF'/bin/sh.exe'
-    call EnvSet      fRm, 'MAKESHELL',          sPathToolsF'/bin/sh.exe'
-
-    /* this messes up the EMX build, so unset it instead:
-     * call EnvSet      fRm, 'EMXSHELL',           sPathTools'\bin\sh.exe' */
-    call EnvSet      1,   'EMXSHELL'
-    call EnvSet      fRm, 'SHELL',              sPathToolsF'/bin/sh.exe'
-    /*call EnvSet      fRm, 'MAKE',               sPathkBuildF'/bin/os2.x86/kmk.exe'*/
-    call EnvSet      fRM, 'PERLLIB_PREFIX',     'L:/Perl/lib;'sPathToolsF'/usr/lib/perl5'
-    call EnvSet      fRM, 'PERL_SH_DIR',        sPathToolsF'/bin'
-    call EnvSet      fRM, 'PERL_BADLANG',       '0'
-    call EnvSet      fRM, 'TCL_LIBRARY',        sPathToolsF'/usr/lib/tcl8.0'
-
-    call EnvSet      fRm, 'HOSTTYPE',           'i386'
-    sHostName = EnvGet('HOSTNAME');
-    if (sHostName = '') then
-    do
-        say 'warning: hostname not set. using ''blah'' as hostname.';
-        sHostName = 'blah';
-        call EnvSet fRm, 'HOSTNAME', sHostName;
-    end
-    if (EnvGet('LOGNAME') = '') then
-        call EnvSet fRm, 'LOGNAME', sHostName;
-
-    if (\fRm) then
-    do
-        sTmpF = translate(EnvGet('TMP'), '/', '\');
-        call EnvSet     0, 'TMP',      sTmpF;
-        call EnvSet     0, 'TMPDIR',   sTmpF;
-        call EnvSet     0, 'TEMP',     sTmpF;
-        call EnvSet     0, 'TEMPDIR',  sTmpF;
-    end
-
-    /* libtool */
-    call EnvSet      fRm, 'LT_OS2_DLL_PREFIX',  'I'
-    call EnvSet      1, 'LT_OS2_DLL_MAPPED', ''
-
-    /* helpers for scripts */
-    call EnvSet      fRm, 'AWK',                sPathToolsF'/usr/bin/gawk.exe'
-    call EnvSet      fRm, 'GAWK',               sPathToolsF'/usr/bin/gawk.exe'
-    call EnvSet      fRm, 'SH',                 sPathToolsF'/bin/sh.exe'
-    call EnvSet      fRm, 'ASH',                sPathToolsF'/bin/ash.exe'
-    call EnvSet      fRm, 'BASH',               sPathToolsF'/bin/bash.exe'
-
-    /* Workaround for make handle leak. */
-    call EnvSet      fRm, 'EMXOPT',     '-c -n -h1024';
+         call EnvAddFront 1,   'PATH',               '.;' /* the sucker gets removed by 'buildenv toolkit40õ' */
+    end
 
     /* unset some varible which may f**k up. */
+    call EnvSet      1, 'AC_PREFIX'
+    call EnvSet      1, 'AC_MACRODIR'
     call EnvSet      1, 'CC',           ''
     call EnvSet      1, 'CFLAGS',       ''
@@ -326,4 +253,145 @@
     call EnvSet      1, 'BISON_HAIRY',  ''
     call EnvSet      1, 'BISON_SIMPLE', ''
+    call EnvSet      1, 'EMXSHELL',     ''  /* messes up stuff */  
+
+    /* bird basics  */
+    if (fUseBuildEnv & \fRm) then
+        'call BuildEnv.cmd cvs~ emx~ ';/*svn~'*/
+
+    /* We do not want any influence from the emx, that's only runtime stuff. */
+    call EnvSet     1, 'LDPATH'
+    call EnvSet     1, 'LIBRARY_PATH'
+    call EnvSet     1, 'C_INCLUDE_PATH'
+    call EnvSet     1, 'CPLUS_INCLUDE_PATH'
+    call EnvSet     1, 'COBJ_INCLUDE_PATH'
+
+    /* icsdebug */
+    if (fileExists(sPathUnix'\opt\icsdebug\bin\icsdebug.exe')) then
+    do
+        call EnvAddFront fRm, 'PATH',               sPathUnix'\opt\icsdebug\bin;';
+        call EnvAddFront fRm, 'DPATH',              sPathUnix'\opt\icsdebug\help;';
+        call EnvAddFront fRm, 'HELP',               sPathUnix'\opt\icsdebug\help;';
+        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathUnix'\opt\icsdebug\dll;';
+        call EnvAddFront fRm, 'LOCPATH',            sPathUnix'\opt\icsdebug\locale;';
+    end
+
+    /* idebug */
+    if (fileExists(sPathUnix'\opt\idebug\bin\idebug.exe')) then
+    do
+        call EnvAddFront fRm, 'PATH',               sPathUnix'\opt\idebug\bin;';
+        call EnvAddFront fRm, 'DPATH',              sPathUnix'\opt\idebug\help;';
+        call EnvAddFront fRm, 'HELP',               sPathUnix'\opt\idebug\help;';
+        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathUnix'\opt\idebug\dll;';
+        call EnvAddFront fRm, 'NLSPATH',            sPathUnix'\opt\icsdebug\msg\%N;';
+    end
+
+    /* kBuild. */
+    call EnvSet      fRm, 'PATH_KBUILD',        sPathkBuildF
+    call EnvAddFront fRm, 'PATH',               sPathkBuild'\bin\os2.x86;'
+    call EnvAddFront fRm, 'BEGINLIBPATH',       sPathkBuild'\bin\os2.x86;'
+    call EnvSet      fRm, 'BUILD_TYPE',         sBuildType
+    call EnvSet      fRm, 'BUILD_TARGET',       sBuildTarget
+    call EnvSet      fRm, 'BUILD_TARGET_ARCH',  'x86'
+    call EnvSet      fRm, 'BUILD_TARGET_CPU',   'i386'
+    call EnvSet      fRm, 'BUILD_PLATFORM',     sBuildTarget
+    call EnvSet      fRm, 'BUILD_PLATFORM_ARCH','x86'
+    call EnvSet      fRm, 'BUILD_PLATFORM_CPU', 'i386'
+
+    /* unixroot (assumes perl, tcl and stuff...) */
+    call EnvSet      fRm, 'UNIXROOT',           sPathUnixF
+    call EnvAddFront fRm, 'PATH',               sPathUnixF'/usr/local/bin;'
+    call EnvAddFront fRm, 'PATH',               sPathUnix'\usr\local\bin;'
+    call EnvAddFront fRm, 'PATH',               sPathUnixF'/usr/bin;'
+    call EnvAddFront fRm, 'PATH',               sPathUnix'\usr\bin;'
+    call EnvAddFront fRm, 'PATH',               sPathUnixF'/bin;'
+    call EnvAddFront fRm, 'PATH',               sPathUnix'\bin;'
+    call EnvAddFront fRm, 'BEGINLIBPATH',       sPathUnix'\lib;'sPathUnix'\usr\lib;'sPathUnix'\usr\local\lib;'
+
+    call EnvAddFront fRm, 'C_INCLUDE_PATH',     sPathUnixF'/usr/include;'sPathUnixF'/usr/local/include;'
+    call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sPathUnixF'/usr/include;'sPathUnixF'/usr/local/include;'
+    call EnvAddFront fRm, 'OBJC_INCLUDE_PATH',  sPathUnixF'/usr/include;'sPathUnixF'/usr/local/include;'
+    call EnvAddFront fRm, 'LIBRARY_PATH',       sPathUnixF'/lib;'sPathUnixF'/usr/lib;'sPathUnixF'/usr/local/lib;'
+
+    call EnvSet      fRm, 'PATH_SEPARATOR',     ';'
+    sShell = '';
+    if (sShell = '' & FileExists(sPathUnixF'/bin/ash.exe'))  then sShell = sPathUnixF'/bin/ash.exe';
+    if (sShell = '' & FileExists(sPathUnixF'/bin/sh.exe'))   then sShell = sPathUnixF'/bin/sh.exe';
+    if (sShell = '' & FileExists(sPathUnixF'/bin/bash.exe')) then sShell = sPathUnixF'/bin/bash.exe';
+    if (sShell = '')                                         then sShell = sPathkBuildF'/bin/os2.x86/kmk_ash.exe';
+    call EnvSet      fRm, 'CONFIG_SHELL',       sShell
+    call EnvSet      fRm, 'MAKESHELL',          sShell
+    call EnvSet      fRm, 'SHELL',              sShell
+
+    call EnvSet      fRM, 'PERLLIB_PREFIX',     'L:/Perl/lib;'sPathUnixF'/usr/lib/perl5'
+    call EnvSet      fRM, 'PERL_SH_DIR',        sPathUnixF'/bin'
+    call EnvSet      fRM, 'PERL_BADLANG',       '0'
+    call EnvSet      fRM, 'TCL_LIBRARY',        sPathUnixF'/usr/lib/tcl8.0'
+
+    /* unixroot: must have all the temp variables with unix slashes. */
+    if (\fRm) then
+    do
+        sTmpF = ToUnixSlash(FixPath(EnvGet('TMP')));
+        if (sTmpF = '')         then sTmpF = sPathUnixF'/tmp';
+        if (\DirExists(sTmpF))  then say 'warning: TMP='''sTmpF''' doesn''t exist!';
+        call EnvSet     fRM, 'TMP',      sTmpF;
+        call EnvSet     fRM, 'TEMP',     sTmpF;
+        call EnvSet     fRM, 'TMPDIR',   sTmpF;
+        if (  pos(filespec('drive', sTmpF), SysDriveMap(filespec('drive', sTmpF), "REMOTE")) > 0,
+            & word(SysDriveInfo(filespec('drive', sTmpF)), 4) = 'RAMDISK') then
+        do
+            say 'warning: detected RAMFS, setting TMPDIR to ''.'' to make sure yacc will work.'
+            call EnvSet fRM, 'TMPDIR',   '.';
+        end
+    end
+
+    /* unixroot: hostname */
+    sHostName = EnvGet('HOSTNAME');
+    if (sHostName = '') then
+    do
+        say 'warning: hostname not set. using ''localhost'' as hostname.';
+        sHostName = 'localhost';
+        call EnvSet fRm, 'HOSTNAME', sHostName;
+    end
+
+    /* unixroot: there must a user and home directory. */
+    if (EnvGet('USER') = '') then
+        call EnvSet fRm, 'USER', EnvGet('USERNAME');
+    if (EnvGet('USER') = '') then
+        call EnvSet fRm, 'USER', EnvGet('LOGNAME');
+    if (EnvGet('USER') = '') then
+        call EnvSet fRm, 'USER', sHostName;
+    if (EnvGet('USERNAME') = '') then
+        call EnvSet fRm, 'USERNAME', EnvGet('USER');
+    if (EnvGet('LOGNAME') = '') then
+        call EnvSet fRm, 'LOGNAME', EnvGet('USER');
+    if (EnvGet('HOME') = '') then
+        call EnvSet fRm, 'HOME', ToUnixSlash(EnvGet('TMP'));
+
+    /* unixroot: libtool */
+    call EnvSet      fRm, 'LT_OS2_DLL_PREFIX',  'k'
+    call EnvSet      1, 'LT_OS2_DLL_MAPPED', ''
+
+    /* Workaround for make handle leak. */
+    call EnvSet      fRm, 'EMXOPT',     '-c -n -h1024';
+
+    /* GCC */
+    call GCC3xx 'gcc335', fRm, sPathGcc
+
+    /* The built stuff? */
+    if (fWithBuiltStuff) then 
+    do
+        call EnvSet      fRm, 'PATH_BUILTUNIX',     sPathOutF'/dist'
+        call EnvAddFront fRm, 'PATH',               sPathOutF'/dist/usr/bin;'
+        call EnvAddFront fRm, 'PATH',               sPathOut'\dist\usr\bin;'
+        call EnvAddFront fRm, 'PATH',               sPathOutF'/dist/bin;'
+        call EnvAddFront fRm, 'PATH',               sPathOut'\dist\bin;'
+        call EnvAddFront fRm, 'PATH',               sPathOut'\dist\usr\omfhackbin;' /* remove me! */
+        call EnvAddFront fRm, 'BEGINLIBPATH',       sPathOut'\dist\usr\lib;'
+        call EnvAddFront fRm, 'C_INCLUDE_PATH',     sPathOutF'/dist/usr/include;'
+        call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sPathOutF'/dist/usr/include;'
+        call EnvAddFront fRm, 'OBJC_INCLUDE_PATH',  sPathOutF'/dist/usr/include;'
+        call EnvAddFront fRm, 'LIBRARY_PATH',       sPathOutF'/dist/usr/lib;'
+        call EnvAddFront fRm, 'LIBRARY_PATH',       sPathOutF'/dist/usr/lib/gcc-lib/i386-pc-os2-emx/3.3.5;'
+    end
 
     /* fix LIBRARY_PATH (gcc configure is kind of picky about this). */
@@ -580,34 +648,23 @@
 DirExists: procedure
     parse arg sDir, sComplain
-    rc = SysFileTree(sDir, 'sDirs', 'DO');
-    if (rc = 0 & sDirs.0 = 1) then
-        return 1;
-    if (sComplain <> '') then
+    rc = SysFileTree(sDir, 'asDirs', 'DO');
+    if (rc = 0 & asDirs.0 = 1) then
+        rc = 1;
+    else
+        rc = 0;
+    if (rc = 0 & sComplain <> '') then
         say sComplain ''''sDir'''.';
-return 0;
-
-
-
-/*
- * EMX/GCC 3.x.x - this environment must be used 'on' the ordinary EMX.
- * Note! bin.new has been renamed to bin!
- * Note! make .lib of every .a! in 4OS2: for /R %i in (*.a) do if not exist %@NAME[%i].lib emxomf %i
- */
-GCC3xx: procedure expose aCfg. aPath. sPathFile sPathTools sPathToolsF
-    parse arg sToolId,sOperation,fRM,fQuiet,sPathId
-
-    /*
-     * EMX/GCC main directory.
-     */
-    /*sGCC = PathQuery(sPathId, sToolId, sOperation);
-    if (sGCC = '') then
-        return 1;
-    /* If config operation we're done now. */
-    if (pos('config', sOperation) > 0) then
-        return 0;
-    */
-    sGCC = sPathTools'\x86.os2\gcc\staged'
-    sGCCBack    = translate(sGCC, '\', '/');
-    sGCCForw    = translate(sGCC, '/', '\');
+    drop asDirs.;
+return rc;
+
+
+/**
+ * GCC 3.3.x.
+ */
+GCC3xx: procedure expose aCfg. aPath. sPathFile sPathUnix sPathUnixF
+    parse arg sToolId, fRM, sPathGcc
+
+    sGCCBack    = ToDosSlash(sPathGcc);
+    sGCCForw    = ToUnixSlash(sPathGcc);
     chMajor     = '3';
     chMinor     = left(right(sToolId, 2), 1);
@@ -615,13 +672,9 @@
     sVer        = chMajor'.'chMinor'.'chRel
 
-    call EnvSet      fRM, 'PATH_IGCC',      sGCCBack;
-    call EnvSet      fRM, 'CCENV',          'IGCC'
-    call EnvSet      fRM, 'BUILD_ENV',      'IGCC'
-    call EnvSet      fRM, 'BUILD_PLATFORM', 'OS2'
-
+    call EnvSet      fRM, 'PATH_IGCC',          sGCCBack;
     call EnvAddFront fRM, 'BEGINLIBPATH',       sGCCBack'\lib;'
     call EnvAddFront fRM, 'PATH',               sGCCForw'/bin;'sGCCBack'\bin;'
     call EnvAddFront fRM, 'C_INCLUDE_PATH',     sGCCForw'/include'
-    call EnvAddFront fRM, 'LIBRARY_PATH',       sGCCForw'/lib/gcc-lib/i386-pc-os2-emx/'sVer';'sGCCForw'/lib;'sGCCForw'/lib/mt;'
+    call EnvAddFront fRM, 'LIBRARY_PATH',       sGCCForw'/lib/gcc-lib/i386-pc-os2-emx/'sVer';'sGCCForw'/lib;'
     call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sGCCForw'/include;'
     call EnvAddFront fRm, 'CPLUS_INCLUDE_PATH', sGCCForw'/include/c++/'sVer'/backward;'
@@ -630,23 +683,18 @@
     call EnvSet      fRM, 'PROTODIR',           sGCCForw'/include/cpp/gen'
     call EnvSet      fRM, 'OBJC_INCLUDE_PATH',  sGCCForw'/include'
-    call EnvAddFront fRM, 'INFOPATH',           sGCCForw'/info'
-    call EnvSet      fRM, 'EMXBOOK',            'emxdev.inf+emxlib.inf+emxgnu.inf+emxbsd.inf'
-    call EnvAddFront fRM, 'HELPNDX',            'emxbook.ndx', '+', 1
-
-    /*
-     * Verify.
-     */
-    if (pos('verify', sOperation) <= 0) then
-        return 0;
-
-    if (rc = 0) then
-        rc = CheckCmdOutput('g++ --version', 0, fQuiet, sVer);
-    if (rc = 0) then
-    do
-        sVerAS = '2.14';
-        rc = CheckCmdOutput('as --version', 0, fQuiet, 'GNU assembler 'sVerAS);
-    end
-return rc;
-
+
+    if (1) then 
+    do
+        say 'info: Using wl.exe as OMF linker.';
+        call EnvSet  fRM, 'EMXOMFLD_TYPE',      'WLINK'
+        call EnvSet  fRM, 'EMXOMFLD_LINKER',    'wl.exe'
+    end
+    else
+    do
+        say 'info: Using ilink.exe (v5) as OMF linker.';
+        call EnvSet  fRM, 'EMXOMFLD_TYPE',      'VAC365'
+        call EnvSet  fRM, 'EMXOMFLD_LINKER',    'ilink.exe'
+    end
+return 0;
 
 
@@ -668,4 +716,43 @@
 
 /**
+ * Fixes the casing of a path.
+ * Returns a DOS path.
+ */
+FixPath: procedure
+    parse arg sPath
+
+    sPath = ToDosSlash(sPath);
+    sDrv = translate(strip(filespec('drive', sPath)));
+    if (sDrv = '' & left(sPath, 2) = '\\') then
+        sDrv = '\\' || translate(word(strip(translate(sPath, ' ', '\')), 1));
+    sPath = substr(sPath, length(sDrv) + 1);
+    if (sPath = '\' | sPath = '/') then
+        return sDrv||sPath;
+
+    /* split the path into words and take them on one by one. */
+    sPath = strip(translate(sPath, '  ', '\/'));
+    sNewPath = sDrv;
+    do i = 1 to words(sPath)
+        sCur = word(sPath, i);
+        sTmp = sNewPath'\'sCur;
+        rc = SysFileTree(sTmp'?', 'asEntries', 'BO');
+        if (rc = 0) then 
+        do
+            do j = 1 to asEntries.0
+                sName = filespec('name', asEntries.j);
+                if (ToLower(sName) = ToLower(sCur)) then 
+                do
+                    sTmp = asEntries.j;
+                    leave
+                end
+            end
+            drop asEntries.;
+        end
+        sNewPath = sTmp;
+    end
+return sNewPath;
+
+
+/**
  * Translate a string to lower case.
  */
@@ -674,2 +761,18 @@
 return translate(sString, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
 
+
+/**
+ * Translate a path to unix slashes.
+ */
+ToUnixSlash: procedure
+  parse arg sPath
+return translate(sPath, '/', '\');
+
+
+/**
+ * Translate a path to dos slashes.
+ */
+ToDosSlash: procedure
+  parse arg sPath
+return translate(sPath, '\', '/');
+
