      SUBROUTINE MAIN
      LOGICAL*1 ARG(40), TEMPL8(40), SCRAT(40), RLIB(40), ARGS(80), MAP(
     *40)
      INTEGER I, GETARG, INT, CREATE, STATUS, SSPAWN, RLIBDN, OPEN, INP
      COMMON / CLD / VERBOS, DEBUG, TASK(40)
      INTEGER VERBOS
      INTEGER DEBUG
      LOGICAL*1 TASK
      COMMON /FLIST/ FLEVEL, FFILES(40, 25)
      INTEGER FLEVEL
      LOGICAL*1 FFILES
      LOGICAL*1 TKB(4)
      LOGICAL*1 EXT(4)
      LOGICAL*1 MAPEXT(4)
      LOGICAL*1 DEBSW(5)
      LOGICAL*1 LIB(4)
      LOGICAL*1 RLIBST(8)
      LOGICAL*1 MAINST(8)
      LOGICAL*1 INFILE(10)
      LOGICAL*1 IMGSW(7)
      DATA TKB(1)/116/,TKB(2)/107/,TKB(3)/98/,TKB(4)/0/
      DATA EXT(1)/116/,EXT(2)/115/,EXT(3)/107/,EXT(4)/0/
      DATA MAPEXT(1)/109/,MAPEXT(2)/97/,MAPEXT(3)/112/,MAPEXT(4)/0/
      DATA DEBSW(1)/47/,DEBSW(2)/100/,DEBSW(3)/97/,DEBSW(4)/44/,DEBSW(5)
     */0/
      DATA LIB(1)/47/,LIB(2)/108/,LIB(3)/98/,LIB(4)/0/
      DATA RLIBST(1)/114/,RLIBST(2)/108/,RLIBST(3)/105/,RLIBST(4)/98/,RL
     *IBST(5)/47/,RLIBST(6)/108/,RLIBST(7)/98/,RLIBST(8)/0/
      DATA MAINST(1)/58/,MAINST(2)/46/,MAINST(3)/109/,MAINST(4)/97/,MAIN
     *ST(5)/105/,MAINST(6)/110/,MAINST(7)/46/,MAINST(8)/0/
      DATA INFILE(1)/116/,INFILE(2)/111/,INFILE(3)/111/,INFILE(4)/108/,I
     *NFILE(5)/115/,INFILE(6)/46/,INFILE(7)/116/,INFILE(8)/107/,INFILE(9
     *)/98/,INFILE(10)/0/
      DATA IMGSW(1)/47/,IMGSW(2)/102/,IMGSW(3)/112/,IMGSW(4)/47/,IMGSW(5
     *)/99/,IMGSW(6)/112/,IMGSW(7)/0/
      FLEVEL = 0
      TASK(1) = 0
      VERBOS = 0
      DEBUG = 0
      RLIBDN = 0
      I=1
23000 IF(.NOT.(GETARG(I, ARG, 40) .NE. -1))GOTO 23002
      IF(.NOT.(ARG(1) .EQ. 45 .OR. ARG(1) .EQ. 43 .OR. ARG(1) .EQ. 63))G
     *OTO 23003
      CALL LDCMD(ARG)
      GOTO 23004
23003 CONTINUE
      CALL FSTACK(ARG)
23004 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      IF(.NOT.(TASK(1) .EQ. 0))GOTO 23005
      I=1
23007 IF(.NOT.(I.LE.FLEVEL))GOTO 23009
      IF(.NOT.(FFILES(1,I) .NE. 45))GOTO 23010
      GOTO 23009
23010 CONTINUE
23008 I=I+1
      GOTO 23007
23009 CONTINUE
      IF(.NOT.(I .LE. FLEVEL))GOTO 23012
      CALL SCOPY(FFILES(1,I), 1, TASK, 1)
23012 CONTINUE
23005 CONTINUE
      IF(.NOT.(TASK(1) .EQ. 0))GOTO 23014
      CALL LDERR
23014 CONTINUE
      CALL SCRATF(TKB, SCRAT)
      INT = CREATE(SCRAT, 2)
      IF(.NOT.(INT .EQ. -3))GOTO 23016
      CALL ERROR(32HError creating TKB command file.)
23016 CONTINUE
      CALL GETDIR(1, 6, RLIB)
      CALL CONCAT(RLIB, INFILE, TEMPL8)
      CALL CONCAT(RLIB, RLIBST, RLIB)
      CALL GENFIL(TASK, EXT, ARG)
      CALL GENFIL(TASK, MAPEXT, ARGS)
      CALL MKLOCL(ARG, TASK)
      CALL REMOVE(TASK)
      CALL PUTLIN(TASK, INT)
      CALL PUTLIN(IMGSW, INT)
      IF(.NOT.(DEBUG .EQ. 1))GOTO 23018
      CALL MKLOCL(ARGS, MAP)
      CALL REMOVE(MAP)
      CALL PUTLIN(DEBSW, INT)
      CALL PUTLIN(MAP, INT)
23018 CONTINUE
      CALL PUTCH(61, INT)
      CALL PUTCH(10, INT)
      I=1
23020 IF(.NOT.(I.LE.FLEVEL))GOTO 23022
      IF(.NOT.(FFILES(1,I) .EQ. 45))GOTO 23023
      IF(.NOT.(FFILES(3,I) .EQ. 0))GOTO 23025
      CALL PUTLIN(RLIB, INT)
      CALL PUTCH(10, INT)
      RLIBDN = 1
      GOTO 23026
23025 CONTINUE
      CALL MKLOCL(FFILES(3,I), ARG)
      CALL PUTLIN(ARG, INT)
      CALL PUTLIN(LIB, INT)
      CALL PUTCH(10, INT)
23026 CONTINUE
      GOTO 23024
23023 CONTINUE
      CALL MKLOCL(FFILES(1,I), ARG)
      CALL PUTLIN(ARG, INT)
      CALL PUTCH(10, INT)
23024 CONTINUE
23021 I=I+1
      GOTO 23020
23022 CONTINUE
      CALL PUTLIN(RLIB, INT)
      CALL PUTLIN(MAINST, INT)
      CALL PUTCH(10, INT)
      IF(.NOT.(RLIBDN .EQ. 0))GOTO 23027
      CALL PUTLIN(RLIB, INT)
      CALL PUTCH(10, INT)
23027 CONTINUE
      INP = OPEN(TEMPL8, 1)
      IF(.NOT.(INP .EQ. -3))GOTO 23029
      CALL ERROR(38HError opening tools.tkb template file.)
23029 CONTINUE
      CALL FCOPY(INP, INT)
      CALL CLOSE(INP)
      CALL CLOSE(INT)
      I = 1
      CALL STCOPY(TKB, 1, ARGS, I)
      CALL CHCOPY(32, ARGS, I)
      CALL CHCOPY(64, ARGS, I)
      CALL SCOPY(SCRAT, 1, ARGS, I)
      IF(.NOT.(VERBOS .EQ. 1))GOTO 23031
      INT = OPEN(SCRAT, 1)
      IF(.NOT.(INT .EQ. -3))GOTO 23033
      CALL ERROR(34HError in opening TKB command file.)
23033 CONTINUE
      CALL FCOPY(INT, 3)
      CALL CLOSE(INT)
23031 CONTINUE
      STATUS = SSPAWN(5Hlocal, ARGS, ARG, 119)
      IF(.NOT.(STATUS .EQ. -3))GOTO 23035
      CALL REMARK(22HError in spawning TKB.)
23035 CONTINUE
      CALL REMOVE(SCRAT)
      RETURN
      END
      SUBROUTINE LDCMD(ARG)
      LOGICAL*1 ARG(40)
      COMMON / CLD / VERBOS, DEBUG, TASK(40)
      INTEGER VERBOS
      INTEGER DEBUG
      LOGICAL*1 TASK
      IF(.NOT.(ARG(1) .EQ. 45 .AND. (ARG(2) .EQ. 112 .OR. ARG(2) .EQ. 80
     *)))GOTO 23037
      CALL SCOPY(ARG, 3, TASK, 1)
      GOTO 23038
23037 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 63 .AND. ARG(2) .EQ. 0))GOTO 23039
      CALL LDERR
      GOTO 23040
23039 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45 .AND. (ARG(2) .EQ. 108 .OR. ARG(2) .EQ. 76
     *)))GOTO 23041
      CALL FSTACK(ARG)
      GOTO 23042
23041 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45 .AND. (ARG(2) .EQ. 118 .OR. ARG(2) .EQ. 86
     *)))GOTO 23043
      VERBOS = 1
      GOTO 23044
23043 CONTINUE
      IF(.NOT.(ARG(1) .EQ. 45 .AND. (ARG(2) .EQ. 100 .OR. ARG(2) .EQ. 68
     *)))GOTO 23045
      DEBUG = 1
      GOTO 23046
23045 CONTINUE
      CALL REMARK(25HIgnoring invalid argument)
23046 CONTINUE
23044 CONTINUE
23042 CONTINUE
23040 CONTINUE
23038 CONTINUE
      RETURN
      END
      SUBROUTINE FSTACK (IARG)
      INTEGER I
      LOGICAL*1 IARG(40)
      COMMON /FLIST/ FLEVEL, FFILES(40, 25)
      INTEGER FLEVEL
      LOGICAL*1 FFILES
      IF(.NOT.(FLEVEL .LT. 25))GOTO 23047
      FLEVEL = FLEVEL + 1
      I=1
23049 IF(.NOT.(I.LE.40))GOTO 23051
      FFILES(I,FLEVEL) = IARG(I)
23050 I=I+1
      GOTO 23049
23051 CONTINUE
      CALL FOLD(FFILES(1, FLEVEL))
23047 CONTINUE
      RETURN
      END
      SUBROUTINE GENFIL(IN, EXT, OUT)
      INTEGER I, J
      LOGICAL*1 IN(40), EXT(4), OUT(40)
      I=1
23052 IF(.NOT.(IN(I) .NE. 46 .AND. IN(I) .NE. 0))GOTO 23054
      OUT(I) = IN(I)
23053 I=I+1
      GOTO 23052
23054 CONTINUE
      CALL CHCOPY(46, OUT, I)
      CALL SCOPY(EXT, 1, OUT, I)
      RETURN
      END
      SUBROUTINE LDERR
      CALL ERROR(56Husage:  ld [-v] [-d] [-l[library]] [-pprocname] file
     * ...)
      RETURN
      END
