/* MD_Install.rexx */

/*
$VER: MD Install v2.00 (06.Jun.1992)
*/

/* This is an ARexx programme to install MEGADISC onto a hard drive. */
/* Sounds simple, but many factors make this slightly difficult...   */
/* One of these is the icon default tools, which specify :c/fullview */
/* and so fullview must be in the c directory in the root directory  */
/* as must a number of other files... But it can be done! (I hope!)  */

/* Programme:       MD_Install.rexx                                  */
/* Version:         2.00                                             */
/* Author:          Anthony Towns                                    */
/* Date Written:    22.Apr.1992                                      */
/* Date Released:   01.May.1992                                      */
/* Status:          Limited Freeware                                 */

/* To run:                                                           */
/*                                                                   */
/* First insert the Megadiscs you wish to install in seperate drives */
/* and then simply type in "MD_Install.rexx" at the CLI prompt or    */
/* double click the supplied programme icon.                         */

/* Copyright Notice:                                                 */
/*                                                                   */
/* This programme is copyright 1992 Anthony Towns. It may not be     */
/* distributed without prior permission of the author. The programme */
/* file and icon may be distributed by the following:                */
/*      Anthony Towns (the author)                                   */
/*      Megadisc Digital Publishing                                  */
/*    & Those with express written permission authorised and dated   */
/*       by either of the above.                                     */
/* This file may not be modified in any way. The icon file may be    */
/* altered as required.                                              */
/* This programme may be distributed with a Commercial product, but  */
/* no profit may be made by its inclusion.                           */

shelli=stdin
shello=stdout

TRUE=1
FALSE=0

DIR_DELIMITER='/'

RETURN_OK=0
RETURN_WARN=5
RETURN_ERROR=10
RETURN_FAIL=20

/***** Open Libraries *****/

if ~show('l','rexxsupport.library')
then do
    if ~addlib('rexxsupport.library',0,-30,0)
    then do
        say 'Error! Could not open REXXSupport.Library.'
        exit(RETURN_FAIL)
        end
    end

/***** Main Programme *****/

do until finis=TRUE

    if win=TRUE
    then do
        if openport(PORT)=FALSE
        then do
            writeln(shello,'Error! Could not open port.')
            exit(RETURN_FAIL)
            end
        else do
            if ~show('i',PORT)
            then do
                address command 'WaitForPort' PORT
                end
            waitpkt(PORT)
            message=getpkt(PORT)
            if upper(message)='QUIT'
            then do
                closeport(PORT)
                exit(RETURN_OK)
                end
            else do
                closeport(PORT)
                end
            window=TRUE
            output='Con Window'
            input=output
            open(output,'CON:0/0/640/100/Window')
            end
        end
    else do
        window=FALSE
        output=stdout
        input=stdin
        end

    md_vols=volumes('MEGADISC',' ')

    vols=words(md_vols)

    do vol_num=1 to vols
        vol.vol_num=word(md_vols,vol_num)
        end

    do vol_num=1 to vols
        writech(output,'Install ' vol.vol_num ' (Y/n) > ')
        if upper(left(readln(input),1))~='N'
        then do
            volume=vol.vol_num
            writeln(output,'Please enter the name of the directory to where')
            writeln(output,'you wish the disk to be installed. (If it does')
            writeln(output,'not already exist it will be created.)')
            writech(output,'> ')

            fulldir=readln(input)
            if upper(right(fulldir,1))~='/'
            then do
                fulldir=insert(fulldir,'/')
                end

            start=1

            do pos=1 to length(fulldir)
                if substr(fulldir,pos,1)=':'
                then do
                    drive=left(fulldir,pos)
                    directory=right(fulldir,length(fulldir)-pos)
                    break
                    end
                end

            fulldir = left(fulldir,length(fulldir)-1)

            if ~exists(fulldir)
            then do
                address command 'makedir' fulldir
                end

            fulldir = insert(fulldir,'/')

            special.1=insert(volume,'C/')
            name.1='C/'
            special.2=insert(volume,'VIRUS/')
            name.2='VIRUS/'

            do number=1 to 2

                if exists(special.number)
                then do
                    writeln(output,'    Installing ' special.number '....')

                    special_to.number=insert(drive,name.number)

                    if ~exists(special_to.number)
                    then do
                        address command 'makedir' special_to.number
                        end

                    dir=insert(showdir(special.number,'ALL',DIR_DELIMITER),DIR_DELIMITER)

                    ld=length(dir)

                    start=1
                    current=0

                    do pos=1 to ld by 1
                        if substr(dir,pos,1)=DIR_DELIMITER
                        then do
                            length=pos-start
                            current=current+1
                            spec_file.current=substr(dir,start,length)
                            start=pos+1
                            end
                        end

                    files=current

                    do file=1 to files
                        writech(output,'        Install ' spec_file.file '? (Y/n) > ')
                        if upper(left(readln(input),1))~='N'
                        then do
                            writech(output,'            Installing' spec_file.file '....')
                            to=insert(special_to.number,spec_file.file)
                            from=insert(special.number,spec_file.file)
                            address command 'copy' from to 'all' 'quiet'
                            writeln(output,'')
                            end
                        end
                    end
                end

            level=1
            start=1

            do pos=1 to length(directory)
                if substr(directory,pos,1)='/'
                then do
                    subdir.level=substr(directory,start,1+pos-start)
                    level=level+1
                    start=pos+1
                    end
                end

            levels=level-1

            check=drive

            do c_level=1 to levels
                check=insert(check,subdir.c_level)
                if ~exists(check)
                then do
                    address command 'makedir' check
                    end
                end

            noinstallcount=1

            noinstall.1='C'

            dir=insert(showdir(volume,'Dir',DIR_DELIMITER),DIR_DELIMITER)

            ld=length(dir)

            start=1
            current=0

            do pos=1 to ld by 1
                if ~compare(substr(dir,pos,1),DIR_DELIMITER)
                then do
                    length=pos-start
                    current=current+1
                    dir.current=substr(dir,start,length)
                    start=pos+1
                    end
                end

            dirs=current

            do current=1 to dirs
                dontinstall=FALSE
                do check=1 to noinstallcount
                    if upper(dir.current)=noinstall.check
                    then do
                        dontinstall=TRUE
                        end
                    end
                if dontinstall=FALSE
                then do
                    writech(output,'    Installing' dir.current '....')
                    to=insert(fulldir,dir.current)
                    from=insert(volume,dir.current)
                    address command 'copy' from to 'all' 'quiet'
                    writeln(output,'')
                    end
                end

            drop dir.

            writech(output,'    Installing Miscellaneous Files ....')

            file=insert(showdir(special.number,'File',DIR_DELIMITER),DIR_DELIMITER)

            ld=length(file)

            start=1
            current=0

            do pos=1 to ld by 1
                if ~compare(substr(file,pos,1),DIR_DELIMITER)
                then do
                    length=pos-start
                    current=current+1
                    file.current=substr(file,start,length)
                    start=pos+1
                    end
                end

            files=current

            do current=1 to files
                to=insert(fulldir,file.current)
                from=insert(volume,file.current)
                address command 'copy' from to 'quiet'
                writeln(output,'')
                end

            drop file.

            writeln(output,'')

            end
        end

    writeln(output,'Megadisc installation complete.')

    writech(output,'Again or Stop? (A/s) > ')

    response=upper(left(readln(input),3))

    if response='S'
    then do
        finis=TRUE
        end
    end

if window=TRUE
then do
    close(output)
    end
drop input
drop output
drop window
exit(RETURN_OK)

/***** Function Manual Pages *****/

/* VOLUMES(name,[pad]) */

/***** Functions *****/

VOLUMES:

    if arg()=0 or arg()>2
    then do
        return
        end

    name=upper(arg(1))

    if arg()=2
    then do
        pad=left(arg(2),1)
        end
    else do
        pad=' '
        end

    vols=showlist('v')

    words=words(vols)

    do number=1 to words
        volume.number = upper(word(vols,number))
        end

    len=length(name)

    fulfill=0

    result=''

    do number=1 to words
        if upper(left(volume.number,len))=name
        then do
            volume=insert(volume.number,':')
            volume=insert(volume,pad)
            result=insert(result,volume)
            end
        end

    drop volume.
    drop volume

    if result~=''
    then do
        result=left(result,length(result)-1)
        end

return result

