envos2.cmd 26.2 KB
Newer Older
1 2 3 4
/*
echo this is a rexx script!
cancel & quit & exit
*/
5
/* $Id: envos2.cmd 2413 2010-09-11 17:43:04Z bird $ */
6 7 8 9 10 11
/** @file
 * Environment setup script for OS/2.
 */

/*
 *
12
 * Copyright (c) 1999-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
 *
 * This file is part of kBuild.
 *
 * kBuild 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.
 *
 * kBuild 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 kBuild; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


/*
 * Setup the usual suspects.
 */
Address CMD '@echo off';
signal on novalue name NoValueHandler
if (RxFuncQuery('SysLoadFuncs') = 1) then
do
    call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs';
    call SysLoadFuncs;
end

/*
 * Apply the CMD.EXE workaround.
 */
call FixCMDEnv;

/*
 * Globals
 */
skBuildPath       = EnvGet("KBUILD_PATH");
skBuildBinPath    = EnvGet("KBUILD_BIN_PATH");
skBuildType       = EnvGet("KBUILD_TYPE");
skBuildTarget     = EnvGet("KBUILD_TARGET");
skBuildTargetArch = EnvGet("KBUILD_TARGET_ARCH");
skBuildTargetCpu  = EnvGet("KBUILD_TARGET_CPU");
skBuildHost       = EnvGet("KBUILD_HOST");
skBuildHostArch   = EnvGet("KBUILD_HOST_ARCH");
skBuildHostCpu    = EnvGet("KBUILD_HOST_CPU");

/*
 * Process arguments.
 */
fOptFull = 0
fOptLegacy = 0
fOptDbg = 0
fOptQuiet = 0
sOptVars = ""
fOptValueOnly = 0
sShowVarPrefix = "";
fOptOverrideAll = 0
fOptOverrideType = 0;
fSetType = 0;
fOptOverrideTarget = 0;
fOptOverrideTargetArch = 0;
fOptDefault = 0;

parse arg sArgs
do while (sArgs <> '')
    parse value sArgs with sArg sRest
    say 'sArgs='sArgs';'
    say ' sArg='sArg';'
    say 'sRest='sRest';'

    select
        when (sArg = "--debug-script") then do
            fOptDbg = 1;
        end
        when (sArg = "--no-debug-script") then do
            fOptDbg = 0;
        end
        when (sArg = "--quiet") then do
            fOptQuiet = 1;
        end
        when (sArg = "--verbose") then do
            fOptQuiet = 0;
        end
        when (sArg = "--full") then do
            fOptFull = 1;
        end
        when (sArg = "--normal") then do
            fOptFull = 0;
        end
        when (sArg = "--legacy") then do
            fOptLegacy = 1;
        end
        when (sArg = "--no-legacy") then do
            fOptLegacy = 0;
        end
        when (sArg = "--eval") then do
            say "error: --eval is not supported on OS/2."
        end
        when (sArg = "--var") then do
            parse value sRest with sVar sRest2
            sRest = sRest2;
            if (sVar = '') then do
                say "syntax error: --var is missing the variable name";
                call SysSleep 1
                exit 1;
            end
            if (sVar = "all" | sOptVars = "all") then
                sOptVars = "all";
            else
                sOptVars = sOptVars || " " || sVar;
        end
        when (sArg = "--set") then do
            sShowVarPrefix = "SET ";
        end
        when (sArg = "--no-set") then do
            sShowVarPrefix = "";
        end
        when (sArg = "--value-only") then do
            fOptValueOnly = 1;
        end
        when (sArg = "--name-and-value") then do
            fOptValueOnly = 0;
        end
        when (sArg = "--release") then do
            fOptOverrideType = 1;
            fSetType = 1;
            skBuildType = 'release';
        end
        when (sArg = "--debug") then do
            fOptOverrideType = 1;
            fSetType = 1;
            skBuildType = 'debug';
        end
        when (sArg = "--profile") then do
            fOptOverrideType = 1;
            fSetType = 1;
            skBuildType = 'profile';
        end
        when (sArg = "--defaults") then do
            fOptOverrideAll = 1;
            skBuildType = "";
            skBuildTarget = "";
            skBuildTargetArch = "";
            skBuildTargetCpu = "";
            skBuildHost = "";
            skBuildHostArch = "";
            skBuildHostCpu = "";
            skBuildPath = "";
            skBuildBinPath = "";
        end

        when (sArg = "--help" | sArg = "-h" | sArg = "-?" | sArg = "/?" | sArg = "/h") then do
            say "kBuild Environment Setup Script, v0.1.4"
            say ""
            say "syntax: envos2.cmd [options] [command [args]]"
            say "    or: envos2.cmd [options] --var <varname>"
            say ""
            say "The first form will execute the command, or if no command is given"
            say "modify the current invoking shell."
            say "The second form will print the specfified variable(s)."
            say ""
            say "Options:"
            say "  --debug, --release, --profile"
            say "      Alternative way of specifying KBUILD_TYPE."
            say "  --defaults"
            say "      Enforce defaults for all the KBUILD_* values."
            say "  --debug-script, --no-debug-script"
            say "      Controls debug output. Default: --no-debug-script"
            say "  --quiet, --verbose"
            say "      Controls informational output. Default: --verbose"
            say "  --full, --normal"
            say "      Controls the variable set. Default: --normal"
            say "  --legacy, --no-legacy"
            say "      Include legacy variables in result. Default: --no-legacy"
            say "  --value-only, --name-and-value"
            say "      Controls what the result of a --var query. Default: --name-and-value"
            say "  --set, --no-set"
            say "      Whether to prefix the variable output with 'SET'."
            say "      Default: --no-set"
            say ""
            exit 1
        end

        when (sArg = "--") then do
            sArgs = sRest;
            leave;
        end

        when (left(sArg, 2) = '--') then do
            say 'syntax error: unknown option: '||sArg
            call SysSleep 1
            exit 1
        end

        otherwise do
            leave
        end
    end
    sArgs = sRest;
end
sCommand = strip(sArgs);

/*
 * Deal with legacy environment variables.
 */
if (\fOptOverrideAll) then do
    if (EnvGet("PATH_KBUILD") <> '') then do
        if (skBuildPath <> '' & skBuildPath <> EnvGet("PATH_KBUILD")) then do
            say "error: KBUILD_PATH ("||skBuildPath||") and PATH_KBUILD ("||EnvGet("PATH_KBUILD")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildPath = EnvGet("PATH_KBUILD");
    end

    if (EnvGet("PATH_KBUILD_BIN") <> '') then do
        if (skBuildPath <> '' & skBuildBinPath <> EnvGet("PATH_KBUILD_BIN")) then do
            say "error: KBUILD_BIN_PATH ("||skBuildBinPath||") and PATH_KBUILD_BIN ("||EnvGet("PATH_KBUILD_BIN")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildBinPath = EnvGet("PATH_KBUILD_BIN");
    end

    if (EnvGet("BUILD_TYPE") <> '') then do
        if (skBuildType <> '' & skBuildType <> EnvGet("BUILD_TYPE")) then do
            say "error: KBUILD_TYPE ("||skBuildType||") and BUILD_TYPE ("||EnvGet("BUILD_TYPE")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildType = EnvGet("BUILD_TYPE");
    end

    if (EnvGet("BUILD_TARGET") <> '') then do
        if (skBuildTarget <> '' & skBuildTarget <> EnvGet("BUILD_TARGET")) then do
            say "error: KBUILD_TARGET ("||skBuildTarget||") and BUILD_TARGET ("||EnvGet("BUILD_TARGET")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildTarget = EnvGet("BUILD_TARGET");
    end

    if (EnvGet("BUILD_TARGET_ARCH") <> '') then do
        if (skBuildTargetArch <> '' & skBuildTargetArch <> EnvGet("BUILD_TARGET_ARCH")) then do
            say "error: KBUILD_TARGET_ARCH ("||skBuildTargetArch ||") and BUILD_TARGET_ARCH ("||EnvGet("BUILD_TARGET_ARCH")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildTargetArch = EnvGet("BUILD_TARGET_ARCH");
    end

    if (EnvGet("BUILD_TARGET_CPU") <> '') then do
        if (skBuildTargetCpu <> '' & skBuildTargetCpu <> EnvGet("BUILD_TARGET_CPU")) then do
            say "error: KBUILD_TARGET_CPU ("||skBuildTargetCpu ||") and BUILD_TARGET_CPU ("||EnvGet("BUILD_TARGET_CPU")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildTargetCpu = EnvGet("BUILD_TARGET_CPU");
    end

    if (EnvGet("BUILD_PLATFORM") <> '') then do
        if (skBuildHost <> '' & skBuildHost <> EnvGet("BUILD_PLATFORM")) then do
            say "error: KBUILD_HOST ("||skBuildHost||") and BUILD_PLATFORM ("||EnvGet("BUILD_PLATFORM")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        if (skBuildHost = '' & EnvGet("BUILD_PLATFORM") = "OS2") then do
            say "error: BUILD_PLATFORM=OS2! Please unset it or change it to 'os2'."
            call SysSleep 1;
            exit 1;
        end
        skBuildHost = EnvGet("BUILD_PLATFORM");
    end

    if (EnvGet("BUILD_PLATFORM_ARCH") <> '') then do
        if (skBuildHostArch <> '' & skBuildHostArch <> EnvGet("BUILD_PLATFORM_ARCH")) then do
            say "error: KBUILD_HOST_ARCH ("||skBuildHostArch ||") and BUILD_PLATFORM_ARCH ("||EnvGet("BUILD_PLATFORM_ARCH")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildHostArch = EnvGet("BUILD_PLATFORM_ARCH");
    end

    if (EnvGet("BUILD_PLATFORM_CPU") <> '') then do
        if (skBuildHostCpu <> '' & skBuildHostCpu <> EnvGet("BUILD_PLATFORM_CPU")) then do
            say "error: KBUILD_HOST_CPU ("||skBuildHostCpu ||") and BUILD_PLATFORM_CPU ("||EnvGet("BUILD_PLATFORM_CPU")||") disagree."
            call SysSleep 1;
            exit 1;
        end
        skBuildHostCpu = EnvGet("BUILD_PLATFORM_CPU");
    end
end

/*
 * Set default build type.
 */
if (skBuildType = '') then
    skBuildType = 'release';
if (fOptDbg <> 0) then say "dbg: KBUILD_TYPE="||skBuildType

/*
 * Determin the host platform (OS/2)
 */
if (skBuildHost = '') then
    skBuildHost = 'os2';
if (fOptDbg <> 0) then say "dbg: KBUILD_HOST="||skBuildHost

if (skBuildHostArch = '') then do
    select
        when (skBuildHostCpu = 'i386',
            | skBuildHostCpu = 'i486',
            | skBuildHostCpu = 'i586',
            | skBuildHostCpu = 'i686',
            | skBuildHostCpu = 'i786',
            | skBuildHostCpu = 'i886',
            | skBuildHostCpu = 'i986') then do
            skBuildHostArch = "x86";
        end
        otherwise do
            skBuildHostArch = "x86";
        end
    end
end
if (fOptDbg <> 0) then say "dbg: KBUILD_HOST_ARCH="||skBuildHostArch

if (skBuildHostCpu = '') then
    skBuildHostCpu = 'blend';
if (fOptDbg <> 0) then say "dbg: KBUILD_HOST_CPU="||skBuildHostCpu


/*
 * The target platform.
 * Defaults to the host when not specified.
 */
if (skBuildTarget = '') then
    skBuildTarget = skBuildHost;
if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET="||skBuildTarget

if (skBuildTargetArch = '') then
    skBuildTargetArch = skBuildHostArch;
if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET_ARCH="||skBuildTargetArch

if (skBuildTargetCpu = '') then do
    if (skBuildTargetArch = skBuildHostArch) then
        skBuildTargetCpu = skBuildHostCpu;
    else
        skBuildTargetCpu = "blend";
end
if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET_CPU="||skBuildTargetCpu


/*
 * Determin KBUILD_PATH from the script location and calc KBUILD_BIN_PATH from there.
 */
if (skBuildPath = '') then do
    skBuildPath = GetScriptDir()
end
skBuildPath = translate(skBuildPath, '/', '\')
if (  FileExists(skBuildPath||"/footer.kmk") = 0,
    | FileExists(skBuildPath||"/header.kmk") = 0,
    | FileExists(skBuildPath||"/rules.kmk") = 0) then do
    say "error: KBUILD_PATH ("skBuildPath||") is not pointing to a popluated kBuild directory."
    call SysSleep 1
    exit 1
end
if (fOptDbg <> 0) then say "dbg: KBUILD_PATH="||skBuildPath;

if (skBuildBinPath = '') then do
    skBuildBinPath = skBuildPath||'/bin/'||skBuildHost||'.'||skBuildHostArch;
end
skBuildBinPath = translate(skBuildBinPath, '/', '\')
if (fOptDbg <> 0) then say "dbg: KBUILD_BIN_PATH="||skBuildBinPath;

/*
 * Add the bin/x.y/ directory to the PATH and BEGINLIBPATH.
 * NOTE! Once bootstrapped this is the only thing that is actually necessary.
 */
sOldPath = EnvGet("PATH");
call EnvAddFront 0, "PATH", translate(skBuildBinPath, '\', '/');
sNewPath = EnvGet("PATH");
call EnvSet 0, "PATH", sOldPath;
if (fOptDbg <> 0) then say "dbg: PATH="||sNewPath;

sOldBeginLibPath = EnvGet("BEGINLIBPATH");
call EnvAddFront 0, "BEGINLIBPATH", translate(skBuildBinPath, '\', '/');
sNewBeginLibPath = EnvGet("BEGINLIBPATH");
call EnvSet 0, "BEGINLIBPATH", sOldBeginLibPath;
if (fOptDbg <> 0) then say "dbg: BEGINLIBPATH="||sNewBeginLibPath;

/*
 * Sanity check
 */
if (DirExists(skBuildBinPath) = 0) then
    say "warning: The bin directory for the build platform doesn't exist. ("||skBuildBinPath||")";
else
do
    sPrograms = "kmk kDepPre kDepIDB kmk_append kmk_ash kmk_cat kmk_cp kmk_echo kmk_install kmk_ln kmk_mkdir kmk_mv kmk_redirect kmk_rm kmk_rmdir kmk_sed";
    do i = 1 to words(sPrograms)
        sProgram = word(sPrograms, i);
        if (FileExists(skBuildBinPath||"\"||sProgram||".exe") = 0) then
            say "warning: The "||sProgram||" program doesn't exit for this platform. ("||skBuildBinPath||")";
    end
end


/*
 * The environment is in place, now take the requested action.
 */
iRc = 0;
if (sOptVars <> '') then
do
    if (sOptVars = "all") then
        sOptVars = "KBUILD_PATH KBUILD_BIN_PATH KBUILD_TYPE ",
                || "KBUILD_TARGET KBUILD_TARGET_ARCH KBUILD_TARGET_CPU ",
                || "KBUILD_HOST KBUILD_HOST_ARCH KBUILD_HOST_CPU ";

    /* Echo variable values or variable export statements. */
    do i = 1 to words(sOptVars)
        sVar = word(sOptVars, i)
        sVal = '';
        select
            when (sVar = "PATH") then               sVal = sNewPath;
            when (sVar = "BEGINLIBPATH") then       sVal = sNewBeginLibPath;
            when (sVar = "KBUILD_PATH") then        sVal = skBuildPath;
            when (sVar = "KBUILD_BIN_PATH") then    sVal = skBuildBinPath;
            when (sVar = "KBUILD_TYPE") then        sVal = skBuildType;
            when (sVar = "KBUILD_HOST") then        sVal = skBuildHost;
            when (sVar = "KBUILD_HOST_ARCH") then   sVal = skBuildHostArch;
            when (sVar = "KBUILD_HOST_CPU") then    sVal = skBuildHostCpu;
            when (sVar = "KBUILD_TARGET") then      sVal = skBuildTarget;
            when (sVar = "KBUILD_TARGET_ARCH") then sVal = skBuildTargetArch;
            when (sVar = "KBUILD_TARGET_CPU") then  sVal = skBuildTargetCpu;
            otherwise do
                say "error: Unknown variable "||sVar||" specified in --var request."
                call SysSleep 1
                exit 1
            end

        end
        if (fOptValueOnly <> 0) then
            say sVal
        else
            say sShowVarPrefix||sVar||"="||sVal;
    end
end
else
do
    /* Wipe out all variables - legacy included - with --default. */
    if (fOptOverrideAll <> 0) then do
        call EnvSet 0, KBUILD_PATH, ''
        call EnvSet 0, KBUILD_BIN_PATH, ''
        call EnvSet 0, KBUILD_HOST, ''
        call EnvSet 0, KBUILD_HOST_ARCH, ''
        call EnvSet 0, KBUILD_HOST_CPU, ''
        call EnvSet 0, KBUILD_TARGET, ''
        call EnvSet 0, KBUILD_TARGET_ARCH, ''
        call EnvSet 0, KBUILD_TARGET_CPU, ''

        call EnvSet 0, PATH_KBUILD, ''
        call EnvSet 0, PATH_KBUILD_BIN, ''
        call EnvSet 0, BUILD_PLATFORM, ''
        call EnvSet 0, BUILD_PLATFORM_ARCH, ''
        call EnvSet 0, BUILD_PLATFORM_CPU, ''
        call EnvSet 0, BUILD_TARGET, ''
        call EnvSet 0, BUILD_TARGET_ARCH, ''
        call EnvSet 0, BUILD_TARGET_CPU, ''
    end

    /* Export the variables. */
    call EnvSet 0, "PATH", sNewPath
    call EnvSet 0, "BEGINLIBPATH", sNewBeginLibPath
    if (fOptOverrideType <> 0) then call EnvSet 0, "KBUILD_TYPE", skBuildType
    if (fOptFull <> 0) then do
        call EnvSet 0, KBUILD_PATH,         skBuildPath
        call EnvSet 0, KBUILD_HOST,         skBuildHost
        call EnvSet 0, KBUILD_HOST_ARCH,    skBuildHostArch
        call EnvSet 0, KBUILD_HOST_CPU,     skBuildHostCpu
        call EnvSet 0, KBUILD_TARGET,       skBuildTarget
        call EnvSet 0, KBUILD_TARGET_ARCH,  skBuildTargetArch
        call EnvSet 0, KBUILD_TARGET_CPU,   skBuildTargetCpu

        if (fOptLegacy <> 0) then do
            call EnvSet 0, PATH_KBUILD,         skBuildPath
            call EnvSet 0, BUILD_PLATFORM,      skBuildHost
            call EnvSet 0, BUILD_PLATFORM_ARCH, skBuildHostArch
            call EnvSet 0, BUILD_PLATFORM_CPU,  skBuildHostCpu
            call EnvSet 0, BUILD_TARGET,        skBuildTarget
            call EnvSet 0, BUILD_TARGET_ARCH,   skBuildTargetArch
            call EnvSet 0, BUILD_TARGET_CPU,    skBuildTargetCpu
        end
    end

    /*
     * Execute left over arguments.
     */
    if (strip(sCommand) <> '') then do
        if (fOptQuiet <> 0) then say "info: Executing command: "|| sCommand
        address CMD sCommand
        iRc = rc;
        if (fOptQuiet <> 0 & iRc <> 0) then say "info: rc="||iRc||": "|| sCommand
    end
end
if (fOptDbg <> 0) then say "dbg: finished (rc="||rc||")"
exit (iRc);


/*******************************************************************************
*   Procedure Section                                                          *
*******************************************************************************/

/**
 * Give the script syntax
 */
syntax: procedure
    say 'syntax: envos2.cmd [command to be executed and its arguments]'
    say ''
return 0;


/**
 * No value handler
 */
NoValueHandler:
    say 'NoValueHandler: line 'SIGL;
exit(16);



/**
 * Add sToAdd in front of sEnvVar.
 * Note: sToAdd now is allowed to be alist!
 *
 * Known features: Don't remove sToAdd from original value if sToAdd
 *                 is at the end and don't end with a ';'.
 */
EnvAddFront: procedure
    parse arg fRM, sEnvVar, sToAdd, sSeparator

    /* sets default separator if not specified. */
    if (sSeparator = '') then sSeparator = ';';

    /* checks that sToAdd ends with an ';'. Adds one if not. */
    if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
        sToAdd = sToAdd || sSeparator;

    /* check and evt. remove ';' at start of sToAdd */
    if (substr(sToAdd, 1, 1) = ';') then
        sToAdd = substr(sToAdd, 2);

    /* loop thru sToAdd */
    rc = 0;
    i = length(sToAdd);
    do while i > 1 & rc = 0
        j = lastpos(sSeparator, sToAdd, i-1);
        rc = EnvAddFront2(fRM, sEnvVar, substr(sToAdd, j+1, i - j), sSeparator);
        i = j;
    end

return rc;

/**
 * Add sToAdd in front of sEnvVar.
 *
 * Known features: Don't remove sToAdd from original value if sToAdd
 *                 is at the end and don't end with a ';'.
 */
EnvAddFront2: procedure
    parse arg fRM, sEnvVar, sToAdd, sSeparator

    /* sets default separator if not specified. */
    if (sSeparator = '') then sSeparator = ';';

    /* checks that sToAdd ends with a separator. Adds one if not. */
    if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
        sToAdd = sToAdd || sSeparator;

    /* check and evt. remove the separator at start of sToAdd */
    if (substr(sToAdd, 1, 1) = sSeparator) then
        sToAdd = substr(sToAdd, 2);

    /* Get original variable value */
    sOrgEnvVar = EnvGet(sEnvVar);

    /* Remove previously sToAdd if exists. (Changing sOrgEnvVar). */
    i = pos(translate(sToAdd), translate(sOrgEnvVar));
    if (i > 0) then
        sOrgEnvVar = substr(sOrgEnvVar, 1, i-1) || substr(sOrgEnvVar, i + length(sToAdd));

    /* set environment */
    if (fRM) then
        return EnvSet(0, sEnvVar, sOrgEnvVar);
return EnvSet(0, sEnvVar, sToAdd||sOrgEnvVar);


/**
 * Add sToAdd as the end of sEnvVar.
 * Note: sToAdd now is allowed to be alist!
 *
 * Known features: Don't remove sToAdd from original value if sToAdd
 *                 is at the end and don't end with a ';'.
 */
EnvAddEnd: procedure
    parse arg fRM, sEnvVar, sToAdd, sSeparator

    /* sets default separator if not specified. */
    if (sSeparator = '') then sSeparator = ';';

    /* checks that sToAdd ends with a separator. Adds one if not. */
    if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
        sToAdd = sToAdd || sSeparator;

    /* check and evt. remove ';' at start of sToAdd */
    if (substr(sToAdd, 1, 1) = sSeparator) then
        sToAdd = substr(sToAdd, 2);

    /* loop thru sToAdd */
    rc = 0;
    i = length(sToAdd);
    do while i > 1 & rc = 0
        j = lastpos(sSeparator, sToAdd, i-1);
        rc = EnvAddEnd2(fRM, sEnvVar, substr(sToAdd, j+1, i - j), sSeparator);
        i = j;
    end

return rc;


/**
 * Add sToAdd as the end of sEnvVar.
 *
 * Known features: Don't remove sToAdd from original value if sToAdd
 *                 is at the end and don't end with a ';'.
 */
EnvAddEnd2: procedure
    parse arg fRM, sEnvVar, sToAdd, sSeparator

    /* sets default separator if not specified. */
    if (sSeparator = '') then sSeparator = ';';

    /* checks that sToAdd ends with a separator. Adds one if not. */
    if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
        sToAdd = sToAdd || sSeparator;

    /* check and evt. remove separator at start of sToAdd */
    if (substr(sToAdd, 1, 1) = sSeparator) then
        sToAdd = substr(sToAdd, 2);

    /* Get original variable value */
    sOrgEnvVar = EnvGet(sEnvVar);

    if (sOrgEnvVar <> '') then
    do
        /* Remove previously sToAdd if exists. (Changing sOrgEnvVar). */
        i = pos(translate(sToAdd), translate(sOrgEnvVar));
        if (i > 0) then
            sOrgEnvVar = substr(sOrgEnvVar, 1, i-1) || substr(sOrgEnvVar, i + length(sToAdd));

        /* checks that sOrgEnvVar ends with a separator. Adds one if not. */
        if (sOrgEnvVar = '') then
            if (right(sOrgEnvVar,1) <> sSeparator) then
                sOrgEnvVar = sOrgEnvVar || sSeparator;
    end

    /* set environment */
    if (fRM) then return EnvSet(0, sEnvVar, sOrgEnvVar);
return EnvSet(0, sEnvVar, sOrgEnvVar||sToAdd);


/**
 * Sets sEnvVar to sValue.
 */
EnvSet: procedure
    parse arg fRM, sEnvVar, sValue

    /* if we're to remove this, make valuestring empty! */
    if (fRM) then
        sValue = '';
    sEnvVar = translate(sEnvVar);

    /*
     * Begin/EndLibpath fix:
     *      We'll have to set internal these using both commandline 'SET'
     *      and internal VALUE in order to export it and to be able to
     *      get it (with EnvGet) again.
     */
    if ((sEnvVar = 'BEGINLIBPATH') | (sEnvVar = 'ENDLIBPATH')) then
    do
        if (length(sValue) >= 1024) then
            say 'Warning: 'sEnvVar' is too long,' length(sValue)' char.';
        return SysSetExtLibPath(sValue, substr(sEnvVar, 1, 1));
    end

    if (length(sValue) >= 1024) then
    do
        say 'Warning: 'sEnvVar' is too long,' length(sValue)' char.';
        say '    This may make CMD.EXE unstable after a SET operation to print the environment.';
    end
    sRc = VALUE(sEnvVar, sValue, 'OS2ENVIRONMENT');
return 0;


/**
 * Gets the value of sEnvVar.
 */
EnvGet: procedure
    parse arg sEnvVar
    if ((translate(sEnvVar) = 'BEGINLIBPATH') | (translate(sEnvVar) = 'ENDLIBPATH')) then
        return SysQueryExtLibPath(substr(sEnvVar, 1, 1));
return value(sEnvVar,, 'OS2ENVIRONMENT');


/**
 * Checks if a file exists.
 * @param   sFile       Name of the file to look for.
 * @param   sComplain   Complaint text. Complain if non empty and not found.
 * @returns TRUE if file exists.
 *          FALSE if file doesn't exists.
 */
FileExists: procedure
    parse arg sFile, sComplain
    rc = stream(sFile, 'c', 'query exist');
    if ((rc = '') & (sComplain <> '')) then
        say sComplain ''''sFile'''.';
return rc <> '';


/**
 * Checks if a directory exists.
 * @param   sDir        Name of the directory to look for.
 * @param   sComplain   Complaint text. Complain if non empty and not found.
 * @returns TRUE if file exists.
 *          FALSE if file doesn't exists.
 */
DirExists: procedure
    parse arg sDir, sComplain
    rc = SysFileTree(sDir, 'sDirs', 'DO');
    if (rc = 0 & sDirs.0 = 1) then
        return 1;
    if (sComplain <> '') then
        say sComplain ''''sDir'''.';
return 0;


/**
 *  Workaround for bug in CMD.EXE.
 *  It messes up when REXX have expanded the environment.
 */
FixCMDEnv: procedure
/* do this anyway
    /* check for 4OS2 first */
    Address CMD 'set 4os2test_env=%@eval[2 + 2]';
    if (value('4os2test_env',, 'OS2ENVIRONMENT') = '4') then
        return 0;
*/

    /* force environment expansion by setting a lot of variables and freeing them.
     * ~6600 (bytes) */
    do i = 1 to 100
        Address CMD '@set dummyenvvar'||i'=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    end
    do i = 1 to 100
        Address CMD '@set dummyenvvar'||i'=';
    end
return 0;


/**
 * Translate a string to lower case.
 */
ToLower: procedure
    parse arg sString
return translate(sString, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');


/**
 * Gets the script directory.
 */
GetScriptDir: procedure
    /*
     * Assuming this script is in the root directory, we can determing
     * the abs path to it by using the 'parse source' feature in rexx.
     */
    parse source . . sScript
    sScriptDir = filespec('drive', sScript) || strip(filespec('path', sScript), 'T', '\');
return ToLower(sScriptDir);