* INSTALL.REF 
* version 23.10.2004
*************** Installation of CREFAL. *********************

* $ENTRY Go;

$ENTRY Go { =
     <Prout 'Installation of the optimizing Refal-5 compiler CREFAL. ' >
     <InitBox> <OpSystem <ARG_system1>>;
}

InitBox { = <Br Batch '=' > <Br File '=' >; }

Install { 
  = <Open 'r' <ListingFile> <Heredir> 'doc' <Subdir> 'install.lst'>
    <Parse (Start) <SIGNIF <GetString>>> <FinalMessage> <ClearXXX>;
}

/*
 Syntax of install.lst:
e.text ::= e.block e.text | e.comment 
e.block ::= [ e.system-name e.file-name e.source ] | e.empty
             -- here [ and ] are not metasymbols, they stand for themself;
             -- ] cannot stand inside the block;
e.system-name ::=  unix | windows | windowsNT | windows95 
                  | windows98 | linux | freeBSD
e.comment ::= * e.message
             -- here * has to be on a first position of a line;         
*/

* PARSE the text.
* The format: <MST-parse (e.Ready-part) e.Unready>
Parse {
  (e.1) ' 'e.2 = <Parse (e.1)e.2>;			/* space */
  (e.1) '\t' e.2 = <Parse (e.1)e.2>;    		/* tab	 */
  (e.1) '[' e.2 = <Parse ((e.1) '[') e.2>;
  ((e.1) '[') 'dos' e.2 = <Block ((e.1) '[' 'dos') e.2>;
  ((e.1) '[') 'unix' e.2 = <Block ((e.1) '[' 'unix') e.2>;
  ((e.1) '[') 'linux' e.2 = <Block ((e.1) '[' 'unix') e.2>;
  ((e.1) '[') 'freeBSD' e.2 = <Block ((e.1) '[' 'unix') e.2>;
  ((e.1) '[') 'windowsNT' e.2 = <Block ((e.1) '[' 'windowsNT') e.2>;
  ((e.1) '[') 'mac' e.2 = <Block ((e.1) '[' 'mac') e.2>;
  ((e.1) '[') e.2 = <Error (Passed e.1 '[') (Unpassed e.2)>;
  (e.1) 0 = ;
  (e.1) s.L e.2 = <Error (Passed e.1) (Unpassed s.L e.2)>;
  (e.1) = <Parse (e.1) <SIGNIF <GetString>>>;
};

Block {
 ((e.1) '[' 'any') e.2 = <WriteBlock ((e.1) '[' ) e.2>;
 ((e.1) '[' e.system) e.2, <OurSystem e.system>: True
         = <WriteBlock ((e.1) '[' ) e.2>;
 ((e.1) '[' e.system) e.2 = <IgnoreBlock ((e.1) '[') e.2>;
}

WriteBlock {
  ((e.1)'[') ' 'e.2 = <WriteBlock ((e.1)'[') e.2>;	/* space */
  ((e.1)'[') '\t' e.2 = <WriteBlock ((e.1)'[') e.2>;	/* tab	 */
  ((e.1)'[') ']' e.2 = <Error (Passed e.1 '[') (Unpassed ']' e.2)>;
  ((e.1)'[') 0 e.2 = <Error (Passed e.1 '[') (Unpassed 0 e.2)>;
  ((e.1)'[' e.3) ' ' e.2 = <WriteFile ((e.1) '[' e.3) e.2>;	/* space */
  ((e.1)'[' e.3) '\t' e.2 = <WriteFile ((e.1) '[' e.3) e.2>;	/* tab	 */
  ((e.1) '[' e.3) s.L e.2 = <WriteBlock ((e.1) '[' e.3 s.L) e.2>;

  ((e.1)'[') = <WriteBlock (e.1) <GetString>>;
  ((e.1)'[' e.3) = <WriteFile ((e.1) '[' e.3)>;
};

IgnoreBlock {
  ((e.1)'[') e.2 ']' e.3 = <Parse (e.1) <SIGNIF e.3>>;
  ((e.1)'[') e.2  0 e.3 = <Error (Passed e.1 '[' e.2) (Unpassed 0 e.3)>;
  ((e.1)'[') e.2 = <IgnoreBlock ((e.1)'[') <GetString>>;
};

WriteFile {
  ((e.1)'[' e.file) e.2 = <FileOpen e.file> <WriteFile1 ((e.1)'[') e.2>;
};

FileOpen { e.file = <FileOpen1 e.file> <PutFile e.file>; }
FileOpen1 {
   'crefal' = <Open 'w' <CurrFile> <Heredir> 'crefal'> <PutBatch 'crefal'>
             <PutShellPath>;
   e.file = <Open 'w' <CurrFile> <Heredir> e.file> <PutBatch e.file>;
};

WriteFile1 {
  ((e.1)'[') e.2 ']' e.3 = <PutString e.2> <Parse (e.1) <SIGNIF e.3>>;
  ((e.1)'[') e.2  0 e.3 = <Error (Passed e.1 '[' e.2) (Unpassed 0 e.3)>;
  ((e.1)'[') e.2 = <PutString e.2> <WriteFile1 ((e.1)'[') <GetString>>;
};

* SIGNIFicant card, i.e. not a comment, not a control card.
* ret: 0 on EOF; next not-empty line
SIGNIF {
  '*'e.1 = <SIGNIF <GetString>>;
        = <SIGNIF <GetString>>;
  ' 'e.1 = <SIGNIF e.1>;		/* space */
  '	' e.1 = <SIGNIF e.1>;		/* tab	 */
   e.1 = e.1;
};

PutBatch {
 e.name , <Dg Batch>: e.batch-files = <Br Batch '=' e.batch-files (e.name)>;
}

PutFile { e.name , <Dg File>: e.file = <Br File '=' e.name>; }

CurrFile {  =  13; };
ListingFile {  =  2; };
XXXFile {  =  3; };

* Read the next string.
GetString { = <Get <ListingFile>>; }
* Write a string.
PutString { 
'setenv ' e.1, <Cp File>: 'crefal' = <PutEnv (<SysPutEnv>) e.1>;
* win-dos-mac
 '@set REF5RSL=' e.1, <Cp File>: 'crefal.bat' 
     = <PutString1 '@set REF5RSL=' <ScpPath>>;
     = ; 
 e.1 = <PutString1 e.1>;
 }

PutString1 { e.1 = <NIL <Put <CurrFile> e.1>>; }
NIL { e.1 = ; }

PutEnv { 
(SetEnv) 'REF5RSL ' e.1 = <PutString1 'setenv REF5RSL ' <ScpPath>>;
(Bash)  'REF5RSL ' e.1 = <PutString1 'REF5RSL=' <ScpPath>>
                         <PutString 'export REF5RSL'>;
}

SysPutEnv { = <SysPutEnv1 <ARG_system>>; } 
SysPutEnv1 { 
 'linux' , <ShellTest 'unix'>: { Tcsh = SetEnv;  /* There is 'setenv' */
                                 Csh  = SetEnv;
                                e.bash_sh = Bash; };
 'unix' =<SysPutEnv1 'linux'>;
 'freeBSD' =<SysPutEnv1 'linux'>;
 e.dos-win-mac = Bash;
}

PutShellPath { = <PutShellPath1 <ARG_system>>; } 
PutShellPath1 { 
 'linux' , <ShellTest 'unix'>: 
    { Tcsh = <System1 'which tcsh > xxx'> <GetShellPath>; 
      Csh = <System1 'which csh > xxx'> <GetShellPath>; 
    e.bash_sh = ; };
 'unix' =<PutShellPath1 'linux'>;
 'freeBSD' =<PutShellPath1 'linux'>;
 e.dos-win-mac = ;
}

GetShellPath {
  = <Open 'r' <XXXFile> <Heredir> 'xxx'> <GetShellPath 1 <Get <XXXFile>>>;
1 e.path = <PutString1 '#!' e.path>;
}

*ClearXXX { = <System <DelFile> ' ' <Heredir> 'xxx'>; }
ClearXXX { = <Open 'w' <XXXFile> <Heredir> 'xxx'>; }

DelFile { = <DelFile1 <ARG_system>>; }
DelFile1 { 
 'unix' =  'rm';
 'linux' = <DelFile1 'unix'>;
 'freeBSD' = <DelFile1 'unix'>;
 e.dos-win-mac = 'del'; 
}

CopyFile { = <CopyFile1 <ARG_system>>; }
CopyFile1 { 
 'unix' =  'cp';
 'linux' = <CopyFile1 'unix'>;
 'freeBSD' = <CopyFile1 'unix'>;
 e.dos-win-mac = 'copy'; 
}

* An help how to execute the installator.
Help {
  =
        <Prout 'The required argument is:'>
	<Prout 'name of your operating system'>
        <Prout ', where the name can be:'>
        <Prout '         windowsNT'>
        <Prout '         windows98'>
        <Prout '         windows95'>
        <Prout '         linux'>
        <Prout '         unix'>
        <Prout '         freeBSD'>
        <Exit 99>;
}

FinalMessage {
 = <Prout 'The Refal-5 compiler CREFAL has just been installed for '
           <ARG_system1> ' - operating system.'
   > 
   <Line>
   <Prout 'You have to set _path_ to the executable Refal-5 modules '>
   <Prout '                     as well as to the CREFAL\'s batch-files!'>
   <Prout 'Please, take care about. See install.txt for additonal comments.'>
   <Line> <OpSystemMessage <ARG_system1>> <Line>
}

OpSystemMessage { 
 'unix', <ShellTest 'unix'>: 
    { Bash = ; Sh = ;
      Tcsh = ; Csh = ;
      Default = <Prout 'Your Unix-system has a unknown _shell_'
                 ', please, take care of _crefal_ batch-file and correct '
                 ' it in according to your _shell_ !'
                >;
   };
 'linux' = <OpSystemMessage 'unix'>;
 'freeBSD' = <OpSystemMessage 'unix'>;
 e.dos-win-mac = ;
} 

ShellTest {
 'unix', <GetShell <GetEnv 'SHELL'>>: True s.shell = s.shell;
 'unix', <GetShell <GetEnv 'shell'>>: True s.shell = s.shell;
 'unix' = Default;
}

GetShell {
 e.path '/bash' = True Bash; e.path '/sh' = True Sh;
 e.path '/tcsh' = True Tcsh; e.path '/csh' = True Csh;
 e.unknwon = Default;
}

Line { =
 <Prout '---------------------------------------------------------------'>;
}

ARG_system1 { = <Arg 1>;}

ARG_system { ,<ARG_system1>:
 { 
   'windows' = 'windowsNT';
   'windows95' = 'dos';
   'windows98' = 'dos';
   e.system = e.system;
 };
}

OpSystem { 
* 'dos' = <Install>;
 'unix' = <Install> <ChangeModes <Dg Batch>>;
 'windowsNT' = <Install>;
 'windows' = <OpSystem 'windowsNT'>;
 'windows95' = <Install>;
 'windows98' = <OpSystem 'windows95'>;
* 'mac' = <Install>;
 'linux' = <OpSystem 'unix'>;
 'freeBSD' = <OpSystem 'unix'>;
 e.X =  <Help>;
}

OurSystem {
e.system, <ARG_system>: e.system = True;
'linux' = <IsUnix <ARG_system>>;
'unix' = <IsUnix <ARG_system>>;
'freeBSD' = <IsUnix <ARG_system>>;
e.system = False;
}

IsUnix {
 'unix' = True;
 'linux' = True;
 'freeBSD' = True;
  e.system = False;
}

Subdir { , <ARG_system>: 
  {
 'unix' = '/';
 'linux' = '/';
 'freeBSD' = '/';
 'mac' = ':';
 e.dos-win = '\\';
  };
}

Heredir { , <ARG_system>: 
  {
 'mac' = ':';
  e.dos-unix-win = ;
  };
}

ChangeDir { , <ARG_system>: 
  {
  e.dos-unix-win = 'cd';
  };
}

* Change modes of batch files to be executed.
ChangeModes {
 (e.name) e.files = <System1 'chmod 774 ' e.name> <ChangeModes e.files>;
  = ;
}

ScpPath { ,<ARG_system>:e.system = <ScpPath1 e.system> <GetScpPath e.system>; }
ScpPath1 { 
 'unix' = <System1 'pwd > xxx' >;
 'linux' = <ScpPath1 'unix'>;
 'freeBSD' = <ScpPath1 'unix'>;
 'windowsNT' = <System1 'cmd.exe /c dir > xxx'>;   
 'windows95' = <System1 'command.exe /c dir > xxx'>;   
 'windows98' = <ScpPath1 'windows95'>;
 e.dos-mac = <System1  'dir > xxx'>;
}

System1 { e.com , <System e.com>:
  { 0 = ;
    e.err = <Line> 
            <Prout 'Sorry: The istallation is not successfull ...'> <Line>
            <Exit e.err>;  
  };
}

GetScpPath {
 ('linux') s.N e.path = e.path;
 ('unix') s.N e.path = <GetScpPath ('linux') s.N e.path>;
 ('freeBSD') s.N e.path = <GetScpPath ('linux') s.N e.path>;
* Path on the fourth line.
 (e.dos-win-mac) 4 e.line = <GetScpPath1 e.line ()>;
 (e.dos-win-mac) s.N e.trash = 
            <GetScpPath (e.dos-win-mac) <Add (s.N) 1> <Get <XXXFile>>>;
 e.system = <Open 'r' <XXXFile> <Heredir> 'xxx'> 
            <GetScpPath (e.system) 1 <Get <XXXFile>>>;
}

GetScpPath2 { 
 e.trash ' ' (e.path) = e.path;       /* space */
 e.trash '\t' (e.path) = e.path;      /* tab */
 e.line s.1 (e.path) = <GetScpPath1 e.line (s.1 e.path)>;
}

GetScpPath1 {
 ' Directory of ' e.path () = e.path;
 e.1 = <GetScpPath2 e.1>;
};

*************************************************************

*_________________
Error { RecImpossible = ; }



