C******************** START FILE UFSNM0.FOR ; GROUP UFILES ******************
C------------------------------------------------------------
C  UFSNM0
C  CONSTRUCT FILENAME FROM NAME ELEMENTS
C   OUTPUT ZFILE - CONSTRUCTED FILENAME
C   INPUT  ZDEV  - DEVICE NAME (INCLUDING ":")
C          ZDIR  - DIRECTORY NAME (NOT INCLUDING BRACKETS "[ ]")
C          ZPRE  - PREFIX
C          ZSHOT - SHOT (CHARACTER ENCODED)
C          ZEXT  - FILENAME .EXT EXTENSION
C
C  IF NOT ALL ELEMENTS FIT AS MANY ARE INSERTED AS POSSIBLE WITH
C  PRIORITY (HIGH TO LOW):  ZSHOT,ZPRE,ZEXT,ZDIR,ZDEV
C
C  CONSTRUCTED FILENAME (IF ALL ELEMENTS FIT):
C   ZDIR//ZPRE//ZSHOT//'.'//ZEXT ... WHERE ZDIR ENDS IN "/" (EG. MCCUNE/)
C
      SUBROUTINE UFSNM0(ZFILE,ZDEV,ZDIR,ZPRE,ZSHOT,ZEXT)
C
      CHARACTER*(*) ZFILE
      CHARACTER*(*) ZDEV
      CHARACTER*(*) ZDIR
      CHARACTER*(*) ZPRE
      CHARACTER*(*) ZSHOT
      CHARACTER*(*) ZEXT
C
      CHARACTER*24 ERRLBL
      DATA ERRLBL/'UFSNM0: FILENAME ELEMENT'/
C
C  GET LENGTH OF ZFILE
C
      ILEN=LEN(ZFILE)
C
C  FIGURE OUT WHAT TO ENCODE
C
      IENCOD=0
      IDEV=0
      IDIR=0
      ISHOT=0
      IPRE=0
      IEXT=0
C
C  CHECK ELEMENTS IN ORDER OF PRIORITY
C
      IL=IUFTRM(ZSHOT,ERRLBL,IER)
      IF(IL.GT.ILEN) GO TO 100
      ISHOT=IL
      IENCOD=IL
C
      IL=IUFTRM(ZPRE,ERRLBL,IER)
      IF((IENCOD+IL).GT.ILEN) GO TO 100
      IPRE=IL
      IENCOD=IENCOD+IL
C
      IL=IUFTRM(ZEXT,ERRLBL,IER)
      ILP1=0
      IF(IL.GT.0) ILP1=IL+1              ! ALLOW FOR "."
      IF((IENCOD+ILP1).GT.ILEN) GO TO 100
      IEXT=IL
      IENCOD=IENCOD+ILP1
C
      IL=IUFTRM(ZDIR,ERRLBL,IER)
      ILP2=0
      IF(IL.GT.0) ILP2=IL
      IF((IENCOD+ILP2).GT.ILEN) GO TO 100
      IDIR=IL
      IENCOD=IENCOD+ILP2
C
      IL=IUFTRM(ZDEV,ERRLBL,IER)
      IF((IENCOD+IL).GT.ILEN) GO TO 100
      IDEV=IL
      IENCOD=IENCOD+IL
C
 100  CONTINUE
      IEND=0
C
C  BUILD FILENAME IN CORRECT SYNTACTIC ORDER
      IF(IDEV.GT.0) THEN
        ISTART=IEND+1
        IEND=IEND+IDEV
        ZFILE(ISTART:IEND)=ZDEV(1:IDEV)
      ENDIF
C
      IF(IDIR.GT.0) THEN
        ISTART=IEND+1
        IEND=IEND+IDIR
        ZFILE(ISTART:IEND)=ZDIR(1:IDIR)
      ENDIF
C
      IF(IPRE.GT.0) THEN
        ISTART=IEND+1
        IEND=IEND+IPRE
        ZFILE(ISTART:IEND)=ZPRE(1:IPRE)
      ENDIF
C
      IF(ISHOT.GT.0) THEN
        ISTART=IEND+1
        IEND=IEND+ISHOT
        ZFILE(ISTART:IEND)=ZSHOT(1:ISHOT)
      ENDIF
C
      IF(IEXT.GT.0) THEN
        ISTART=IEND+1
        IEND=IEND+IEXT+1
        ZFILE(ISTART:IEND)='.'//ZEXT(1:IEXT)
      ENDIF
C
C  PAD WITH BLANKS IF NECESSARY
C
      IF(IENCOD.LT.ILEN) THEN
        IP1=IENCOD+1
        DO 110 IC=IP1,ILEN
          ZFILE(IC:IC)=' '
 110    CONTINUE
      ENDIF
C
      RETURN
      END
C******************** END FILE UFSNM0.FOR ; GROUP UFILES ******************
