      SUBROUTINE MAIN
      INTEGER LEVEL, OPENDR, DESC, DEPTH, PTR(5), J, GETPAT, OUTFIL, CRE
     *ATE, GDRPRM, I, LENGTH, MATCH, N, OPEN, GETLIN, GETARG, STATUS, NX
     *TCOL
      LOGICAL*1 PPATH(40), DEFPAT(2), FILE(40), BUF(512), PAT(132), OBUF
     *(512)
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      LOGICAL*1 DIRPAT(4)
      LOGICAL*1 HEAD1(68)
      LOGICAL*1 HEAD2(69)
      DATA DIRPAT(1)/63/,DIRPAT(2)/42/,DIRPAT(3)/47/,DIRPAT(4)/0/
      DATA HEAD1(1)/102/,HEAD1(2)/105/,HEAD1(3)/108/,HEAD1(4)/101/,HEAD1
     *(5)/110/,HEAD1(6)/97/,HEAD1(7)/109/,HEAD1(8)/101/,HEAD1(9)/32/,HEA
     *D1(10)/32/,HEAD1(11)/32/,HEAD1(12)/32/,HEAD1(13)/32/,HEAD1(14)/32/
     *,HEAD1(15)/32/,HEAD1(16)/32/,HEAD1(17)/32/,HEAD1(18)/32/,HEAD1(19)
     */32/,HEAD1(20)/32/,HEAD1(21)/112/,HEAD1(22)/114/,HEAD1(23)/111/,HE
     *AD1(24)/116/,HEAD1(25)/101/,HEAD1(26)/99/,HEAD1(27)/116/,HEAD1(28)
     */105/,HEAD1(29)/111/,HEAD1(30)/110/,HEAD1(31)/32/,HEAD1(32)/32/,HE
     *AD1(33)/32/,HEAD1(34)/32/,HEAD1(35)/32/,HEAD1(36)/32/,HEAD1(37)/10
     *8/,HEAD1(38)/97/,HEAD1(39)/115/,HEAD1(40)/116/,HEAD1(41)/32/,HEAD1
     *(42)/109/,HEAD1(43)/111/,HEAD1(44)/100/,HEAD1(45)/105/,HEAD1(46)/1
     *02/,HEAD1(47)/105/,HEAD1(48)/101/,HEAD1(49)/100/,HEAD1(50)/32/,HEA
     *D1(51)/32/,HEAD1(52)/32/,HEAD1(53)/32/,HEAD1(54)/32/,HEAD1(55)/115
     */,HEAD1(56)/105/,HEAD1(57)/122/,HEAD1(58)/101/,HEAD1(59)/32/,HEAD1
     *(60)/32/,HEAD1(61)/32/,HEAD1(62)/111/,HEAD1(63)/119/,HEAD1(64)/110
     */,HEAD1(65)/101/,HEAD1(66)/114/,HEAD1(67)/10/,HEAD1(68)/0/
      DATA HEAD2(1)/45/,HEAD2(2)/45/,HEAD2(3)/45/,HEAD2(4)/45/,HEAD2(5)/
     *45/,HEAD2(6)/45/,HEAD2(7)/45/,HEAD2(8)/45/,HEAD2(9)/32/,HEAD2(10)/
     *32/,HEAD2(11)/32/,HEAD2(12)/32/,HEAD2(13)/32/,HEAD2(14)/32/,HEAD2(
     *15)/32/,HEAD2(16)/32/,HEAD2(17)/32/,HEAD2(18)/32/,HEAD2(19)/45/,HE
     *AD2(20)/45/,HEAD2(21)/45/,HEAD2(22)/45/,HEAD2(23)/45/,HEAD2(24)/45
     */,HEAD2(25)/45/,HEAD2(26)/45/,HEAD2(27)/45/,HEAD2(28)/45/,HEAD2(29
     *)/45/,HEAD2(30)/45/,HEAD2(31)/45/,HEAD2(32)/45/,HEAD2(33)/32/,HEAD
     *2(34)/32/,HEAD2(35)/45/,HEAD2(36)/45/,HEAD2(37)/45/,HEAD2(38)/45/,
     *HEAD2(39)/45/,HEAD2(40)/45/,HEAD2(41)/45/,HEAD2(42)/45/,HEAD2(43)/
     *45/,HEAD2(44)/45/,HEAD2(45)/45/,HEAD2(46)/45/,HEAD2(47)/45/,HEAD2(
     *48)/45/,HEAD2(49)/45/,HEAD2(50)/45/,HEAD2(51)/45/,HEAD2(52)/32/,HE
     *AD2(53)/45/,HEAD2(54)/45/,HEAD2(55)/45/,HEAD2(56)/45/,HEAD2(57)/45
     */,HEAD2(58)/45/,HEAD2(59)/45/,HEAD2(60)/32/,HEAD2(61)/32/,HEAD2(62
     *)/45/,HEAD2(63)/45/,HEAD2(64)/45/,HEAD2(65)/45/,HEAD2(66)/45/,HEAD
     *2(67)/10/,HEAD2(68)/10/,HEAD2(69)/0/
      DATA DEFPAT/37, 0/
      CALL INITLS(FILE)
      LEVEL = 0
      IF (.NOT.(DOHEAD .EQ. 1))GOTO 23000
      CALL PUTLIN(HEAD1, 2)
      CALL PUTLIN(HEAD2, 2)
23000 CONTINUE
      IF (.NOT.(PACKIT .EQ. 1))GOTO 23002
      CALL INPACK(NXTCOL, 80, OBUF, 2)
23002 CONTINUE
23004 CONTINUE
      LEVEL = LEVEL + 1
      STATUS = GETARG(LEVEL, PPATH, 40)
      IF (.NOT.(STATUS .EQ. -1 .AND. LEVEL .GT. 1))GOTO 23007
      GOTO 23006
23007 CONTINUE
      IF (.NOT.(STATUS .EQ. -1))GOTO 23009
      CALL GWDIR(PPATH, 5)
23009 CONTINUE
23008 CONTINUE
      CALL SCOPY(DEFPAT, 1, FILE, 1)
      IF (.NOT.(OPENDR(PPATH, DESC) .EQ. -3))GOTO 23011
      CALL SCOPY(PPATH, 1, BUF, 1)
      CALL MKPATH(BUF, PPATH)
      CALL EXPPTH(PPATH, DEPTH, PTR, BUF)
      J = PTR(DEPTH)
      JUNK = GTFTOK(PPATH, J, FILE)
      J = PTR(DEPTH)
      PPATH(J) = 0
      IF (.NOT.(OPENDR(PPATH, DESC) .EQ. -3))GOTO 23013
      CALL CANT(PPATH)
23013 CONTINUE
23011 CONTINUE
      IF (.NOT.(DIRSW .EQ. 1))GOTO 23015
      CALL CONCAT(FILE, DIRPAT, FILE)
23015 CONTINUE
      CALL FOLD(FILE)
      IF (.NOT.(GETPAT(FILE, PAT) .EQ. -3))GOTO 23017
      CALL ERROR(16HIllegal pattern.)
23017 CONTINUE
      IF (.NOT.(SORTIT .EQ. 1))GOTO 23019
      OUTFIL = CREATE(SCRAT, 2)
      IF (.NOT.(OUTFIL .EQ. -3))GOTO 23021
      CALL ERROR(30HError creating temporary file.)
23021 CONTINUE
      GOTO 23020
23019 CONTINUE
      OUTFIL = 2
23020 CONTINUE
23023 IF (.NOT.(GDRPRM(DESC, FILE) .NE. -1))GOTO 23024
      I = LENGTH(FILE) + 1
      FILE(I) = 10
      FILE(I+1) = 0
      CALL FOLD(FILE)
      IF (.NOT.(MATCH(FILE, PAT) .EQ. 1))GOTO 23025
      I = LENGTH(FILE)
      FILE(I) = 0
      IF (.NOT.(PACKIT .EQ. 1 .AND. OUTFIL .EQ. 2))GOTO 23027
      CALL DOPACK(FILE, NXTCOL, 80, OBUF, 2)
      GOTO 23028
23027 CONTINUE
      IF (.NOT.(VERBOS .EQ. 1))GOTO 23029
      CALL GDRAUX(DESC, FILE, BUF, PPATH)
      IF (.NOT.(DODATE .EQ. 1))GOTO 23031
      CALL PUTLIN(PPATH, OUTFIL)
23031 CONTINUE
      CALL PUTLIN(FILE, OUTFIL)
      N = 18 - LENGTH(FILE)
      I=1
23033 IF (.NOT.(I .LE. N))GOTO 23035
      CALL PUTCH(32, OUTFIL)
23034 I=I+1
      GOTO 23033
23035 CONTINUE
      CALL PUTLIN(BUF, OUTFIL)
      GOTO 23030
23029 CONTINUE
      CALL PUTLIN(FILE, OUTFIL)
23030 CONTINUE
      CALL PUTCH(10, OUTFIL)
23028 CONTINUE
23025 CONTINUE
      GOTO 23023
23024 CONTINUE
      CALL CLOSDR(DESC)
      IF (.NOT.(SORTIT .EQ. 1))GOTO 23036
      CALL CLOSE(OUTFIL)
      CALL SORTLS(SCRAT, REVERS, BUF)
      OUTFIL = OPEN(SCRAT, 1)
      IF (.NOT.(OUTFIL .EQ. -3))GOTO 23038
      CALL ERROR(37HError in opening sorted scratch file.)
23038 CONTINUE
23040 IF (.NOT.(GETLIN(BUF, OUTFIL) .NE. -1))GOTO 23041
      IF (.NOT.(DODATE .EQ. 1))GOTO 23042
      I = 11 + 1
      GOTO 23043
23042 CONTINUE
      I = 1
23043 CONTINUE
      IF (.NOT.(PACKIT .EQ. 1))GOTO 23044
      I = 1
      JUNK = GETWRD(BUF, I, FILE)
      CALL DOPACK(FILE, NXTCOL, 80, OBUF, 2)
      GOTO 23045
23044 CONTINUE
      CALL PUTLIN(BUF(I), 2)
23045 CONTINUE
      GOTO 23040
23041 CONTINUE
      CALL CLOSE(OUTFIL)
23036 CONTINUE
      IF (.NOT.(PACKIT .EQ. 1))GOTO 23046
      CALL FLPACK(NXTCOL, 80, OBUF, 2)
23046 CONTINUE
23005 GOTO 23004
23006 CONTINUE
      CALL ENDLS
      RETURN
      END
      SUBROUTINE ENDLS
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      CALL REMOVE(SCRAT)
      RETURN
      END
      SUBROUTINE INITLS(ARG)
      INTEGER I, GETARG, INDEX, ISATTY
      LOGICAL*1 ARG(40)
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      LOGICAL*1 USESTR(32)
      DATA USESTR(1)/117/,USESTR(2)/115/,USESTR(3)/97/,USESTR(4)/103/,US
     *ESTR(5)/101/,USESTR(6)/58/,USESTR(7)/32/,USESTR(8)/32/,USESTR(9)/1
     *08/,USESTR(10)/115/,USESTR(11)/32/,USESTR(12)/91/,USESTR(13)/45/,U
     *SESTR(14)/49/,USESTR(15)/100/,USESTR(16)/104/,USESTR(17)/110/,USES
     *TR(18)/114/,USESTR(19)/116/,USESTR(20)/118/,USESTR(21)/93/,USESTR(
     *22)/32/,USESTR(23)/91/,USESTR(24)/112/,USESTR(25)/97/,USESTR(26)/1
     *16/,USESTR(27)/104/,USESTR(28)/93/,USESTR(29)/46/,USESTR(30)/46/,U
     *SESTR(31)/46/,USESTR(32)/0/
      CALL QUERY(USESTR)
      VERBOS = 0
      DOHEAD = 0
      DODATE = 0
      REVERS = 0
      DIRSW = 0
      PACKIT = ISATTY(2)
      SORTIT = 0
      I=1
23048 IF (.NOT.(GETARG(I, ARG, 40) .NE. -1))GOTO 23050
      CALL FOLD(ARG)
      IF (.NOT.(ARG(1) .EQ. 45))GOTO 23051
      IF (.NOT.(INDEX(ARG, 118) .GT. 0))GOTO 23053
      VERBOS = 1
23053 CONTINUE
      IF (.NOT.(INDEX(ARG, 104) .GT. 0))GOTO 23055
      DOHEAD = 1
23055 CONTINUE
      IF (.NOT.(INDEX(ARG, 116) .GT. 0))GOTO 23057
      DODATE = 1
23057 CONTINUE
      IF (.NOT.(INDEX(ARG, 114) .GT. 0))GOTO 23059
      REVERS = 1
23059 CONTINUE
      IF (.NOT.(INDEX(ARG, 110) .GT. 0 .AND. 0 .EQ. 0))GOTO 23061
      SORTIT = 1
23061 CONTINUE
      IF (.NOT.(INDEX(ARG, 100) .GT. 0))GOTO 23063
      DIRSW = 1
23063 CONTINUE
      IF (.NOT.(INDEX(ARG, 49) .GT. 0))GOTO 23065
      PACKIT = 0
23065 CONTINUE
      CALL DELARG(I)
      I = I + 1
23051 CONTINUE
23049 I=I+1
      GOTO 23048
23050 CONTINUE
      IF (.NOT.(DODATE .EQ. 1))GOTO 23067
      VERBOS = 1
23067 CONTINUE
      IF (.NOT.(DODATE .EQ. 1 .OR. REVERS .EQ. 1))GOTO 23069
      SORTIT = 1
23069 CONTINUE
      IF (.NOT.(VERBOS .EQ. 0))GOTO 23071
      DOHEAD = 0
      GOTO 23072
23071 CONTINUE
      PACKIT = 0
23072 CONTINUE
      IF (.NOT.(SORTIT .EQ. 1))GOTO 23073
      CALL SCRATF(LS0, SCRAT)
23073 CONTINUE
      RETURN
      END
      SUBROUTINE SORTLS(FILE, REVERS, BUF)
      LOGICAL*1 FILE(100), DESCR(7), BUF(100), PROC(40), SPATH(120)
      INTEGER REVERS, I, SSPAWN, LOCCOM
      LOGICAL*1 SUFFIX(7)
      LOGICAL*1 PLUSO(4)
      LOGICAL*1 MR(4)
      LOGICAL*1 SORT(5)
      DATA SUFFIX(1)/46/,SUFFIX(2)/116/,SUFFIX(3)/115/,SUFFIX(4)/107/,SU
     *FFIX(5)/0/,SUFFIX(6)/10/,SUFFIX(7)/0/
      DATA PLUSO(1)/32/,PLUSO(2)/43/,PLUSO(3)/111/,PLUSO(4)/0/
      DATA MR(1)/32/,MR(2)/45/,MR(3)/114/,MR(4)/0/
      DATA SORT(1)/115/,SORT(2)/111/,SORT(3)/114/,SORT(4)/116/,SORT(5)/0
     */
      I = 1
      CALL STCOPY(SORT, 1, BUF, I)
      CALL CHCOPY(32, BUF, I)
      CALL STCOPY(FILE, 1, BUF, I)
      CALL STCOPY(PLUSO, 1, BUF, I)
      CALL STCOPY(FILE, 1, BUF, I)
      IF (.NOT.(REVERS .EQ. 1))GOTO 23075
      CALL STCOPY(MR, 1, BUF, I)
23075 CONTINUE
      BUF(I) = 0
      CALL IMPATH(SPATH)
      IF (.NOT.(LOCCOM(SORT, SPATH, SUFFIX, PROC) .NE. 60))GOTO 23077
      CALL ERROR(30HCannot locate sort image file.)
23077 CONTINUE
      IF (.NOT.(SSPAWN(PROC, BUF, DESCR, 119) .EQ. -3))GOTO 23079
      CALL ERROR(22Herror in spawning sort)
23079 CONTINUE
      RETURN
      END
