/*
 * My autoexec, written in Cscript
 */

/* Set an option */
function option(opnum, setting)
{
    syscall(99, opnum, setting);
}

/* Set an assignment */
function assign(old, new)
{
    exec("assign " + old + " " + new);
}

/* Main part */

/* Optional variable declarations */
var wantcron, wantalias;
var readtime, s;
var romver;
var startofday; havesdriver;
var memcheating, command, month, nmonth;
var volume;
var settingfile;

    wantcron = 1;            /* Set if 'cron' to run */
    wantalias = 0;
    readtime = 0;            /* Read time from /usr/lib/timenow */
    volume = 32;            /* Set the beep volume */
    settingfile = "/bbs/users/root/settings";

    romver = syscall(126);
/*    syscall(62, 7, 12);        *//* Turn on memory debug mode */
    exec("fkey 5 makefile");
    exec("fkey 6 /rom/ssrom");
    exec("fkey 8 \"assign /mcd .!cd /s0/mgr!setup!cd /mcd\"");
/*    exec("fkey 9 \"syscall .125 4!go 500000\""); */
    exec("fkey .10 \"assign /work .\"");

/*
    if (syscall(3))
    {
        if (syscall(2) == 'r')
            exec("/s3/rom/ssrom/ssrom");
    }
*/

    /* Is it a power-up? */
    startofday = (romver > 0x41) ? syscall(125, 29) : 1;

    havesdriver = (exec("cd /s0 }null:") >= 0);
    if (havesdriver)
    {
        exec("SDRIVER rw ww");    /* wait states on reads & writes */
        exec("XDRIVER rw ww");    /* wait states on reads & writes */
        assign("/h0", "/s0");
        assign("/h1", "/s1");
    }
    else
    {
        assign("/s0", "/h0");
        assign("/s1", "/h1");
    }

    assign("/hitech",    "/s0/hitech");
    assign("/inc",        "/hitech/include");
    assign("/gsp",        "/s1/gsp");
    assign("/gcc",        "/x0/gcc");
    assign("/ssasminc",    "/s0/srce");
    assign("/usr",        "/s0");
    assign("/etc",        "/usr/lib/etc");
    if (syscall(117, 0, 0x100, 0) > 100)        /* > 100k on /rd? */
    {
        assign("/temp",        "/rd");
        if (syscall(117, 0, 0x100, 0) > 400)    /* > 400k on /rd? */
        {
            exec("copy /gcc/lib/libc.a /gcc/lib/lib1616.a /rd > null:");
        }
    }
    else
        assign("/temp",        "/s1/temp");
    assign("/tmp",        "/temp");
    assign("/rom",        "/s1/rom/ssrom");
    assign("/bbs",        "/s0/app/bbs");
    option(1, 0);        /* Turn off verbose mode */
    option(19, 2);        /* Search xpath directories before . */

    exec("copy /s0/bin/e.xrel /rd } null:");
    exec("xpath /rd /gcc/bin bin bin/xrel bin/shell bin/exec bin/cs /gsp/bin > null:");

    totmem = syscall(62, 0, 10);    /* Real free memory */

    /* Set the memcheating number based on free memory */
    if (totmem < 500000)
        memcheating = 3;
    else if (totmem < 1500000)
        memcheating = 5;
    else if (totmem < 2500000)
        memcheating = 7;
    else memcheating = 9;
    syscall(62, memcheating, 9);

    exec("mwl 0 0");        /* Keep opt68k happy */

    exec("swget");
    option(1, 1);        /* Turn verbose mode back on */

    option(6, 4);        /* Set the eof char */
    option(7, 0x40000);    /* Set EXEC stack space */
    option(15, /*64*/ + 12);/* '5' on numeric k/p generates ^Q, beep,
                                case sensitive */
    option(3, 1);        /* Stack trace on exceptions */

/* Run any pending 'at' jobs */
    if (wantcron)
    {
/*        if (startofday)
          exec("atrun /usr/lib/atjobs/at.* {null: }null: >null: &");
*/
/* Start cron */
/*        exec("cron -econ: <null: >null: }null: &"); */
        option(1, 0);        /* Disable verbose mode */
        exec("copy autoexec1.shell /rd");    /* For warm booting */
        option(1, 1);
    }
    else
    {
        exec(">/rd/autoexec1.shell");
    }

    chdir(".");        /* atrun can cause last directory to change */

/* Set autorepeat */
    if (exec("arep 2 10") >= 0)
    {    /* Set keyclicks */
/*        exec("arep C40");*/
    }

/* Set up command aliasing */
    if (romver > 0x41)
    {
        syscall(125, 19, volume);    /* Set the beep volume */
        if (syscall(89, "newbeep", 0, 0) >= 0)
        {    /* newbeep mrd is installed */
            exec("selbeep /s0/sounds/beeps/* &");
            exec("echo \"selbeep /s0/sounds/beeps/*\" & >>/rd/autoexec1.shell");
        }
    }

    printf("\n");
/* Any mail? */
    exec("mail -q }null:");

    exec("filemode 1 4 /rd/autoexec1.shell"); /* Lock it */
    if (romver > 0x41)
    {
        if (filemode(settingfile + ".shell", "") > 0)
            exec(settingfile);
    }
/* exec("xpath > null:"); */
exec("chdev sa: hfc=0 rxbsize=2048");
exec("chdev sb: hfc=0 rxbsize=2048 baud=57600 rawmode=1");
exec("sdriver rw ww } null:");
exec("xdriver rw ww } null:");
exec("chdev sb: hfc=0 rxbsize=60000 baud=38400 rawmode=1");
exec("fkey 1 \"fastget < sb: ! x34 as.gsp ! tg\"");
exec("fkey 2 \"x34 as.gsp ! tg\"");