	.GOSUB INIT
.NOHELP: .IF P1 = "" .ASKS P1 Device
	.PARSE P1 ":" P1 P9
	.SETS P1 P1+":"
	.TEST P1
	.SETS P9 P1[3:<STRLEN>-1]
	.SETN UNIT 0
	.IF P9 <> "" .SETN UNIT 'P9'
	.SETS P9 P1[1:2]
	.SETF LOGOFF
	.SETS DENS ""
	.SETS INC ""
	.IF P9 <> "DY" .GOTO TRYDX
	.IF UNIT > 1 .GOTO ILLDEV
	.SETS DENS "/DENS=DOUBLE"
	.SETN MAXMXF 481.
	.SETN DEFMXF 60.
	.GOTO FLOPPY
.TRYDX:	.IF P9 <> "DX" .GOTO TRYDK
	.IF UNIT > 1 .GOTO ILLDEV
	.SETS DENS "/DENS=SINGLE"
	.TEST P1
	.SETS P1 "DY"+P1[3:<STRLEN>] 
	.SETN MAXMXF 238.
	.SETN DEFMXF 29.
.FLOPPY:.SETN DEFINF 16.
	.SETS INC "/EXT=1"
	.GOTO START
.TRYDK:	.IF P9 <> "DK" .GOTO TRYDL
	.IF UNIT > 3 .GOTO ILLDEV
	.IFF <PRIVIL> .AND .IF UNIT <= 1 .GOTO ILLDEV
	.SETN MAXMXF 2357.
	.SETN DEFMXF 500.
	.SETN DEFINF 147.
	.GOTO START
.TRYDL:	.IF P9 <> "DL" .GOTO TRYDD
	.IF UNIT > 2 .GOTO ILLDEV
	.IF UNIT <> 2 .GOTO DL01
	.SETN MAXMXF 5034.
	.SETN DEFMXF 629.
	.SETN DEFINF 314.
	.GOTO START
.DL01:	.IF UNIT = 0 .AND .IFF <PRIVIL> .GOTO ILLDEV
	.SETN MAXMXF 10074.
	.SETN DEFMXF 1259.
	.SETN DEFINF 629.
	.GOTO START
.TRYDD:	.IF P9 <> "DD" .GOTO TRYDM
	.IF UNIT > 1 .GOTO ILLDEV
	.SETT LOGOFF
	.SETN MAXMXF 247.
	.SETN DEFMXF 30.
	.SETN DEFINF 16.
	.GOTO START
.TRYDM:	.IF P9 <> "DM" .GOTO TRYMM
	.IFF <PRIVIL> .GOTO ILLDEV
	.SETN MAXMXF 26466.
	.SETN DEFMXF 3000.
	.SETN DEFINF 500.
	.GOTO START
.TRYMM:	.IF P9 = "MM" .AND .IF UNIT = 0 .GOTO START
.ILLDEV:
	; Invalid device or unit, or privilege violation for 'P1'.
	.SETS P1 ""
	.GOTO NOHELP	
.START:	ALL 'P1'
	MOU 'P1'/FOREIGN
	.IF <EXSTAT> <> 1 .GOTO ILLDEV
	.SETF FORMAT
	.IF P9 <> "DL" .AND .IF P9 <> "DD" .AND .IF P9 <> "MM" .ASK FORMAT Format 'P1'
	.ASKS [::"RSX"] RTRSDO DOS, RT-11 or RSX-11M (FILES-11) format
	.SETS RTRSDO RTRSDO[1:2]
.; NOTRSX means RT-11, DOS, or magtape
	.SETT NOTRSX
	.IF P9 = "MM" .GOTO ASKMOU
	.IF RTRSDO = "RS" .SETF NOTRSX
	.IFT NOTRSX .GOTO RT11A
	.IF P3 = "" .ASKN [5.:MAXMXF:DEFMXF] MXF Max No of files on volume
	.IF P3 <> "" .SETN MXF 'P3'.
	.IF DEFINF > MXF-5 .SETN DEFINF MXF-5
	.IF P4 = "" .ASKN [5.:MXF-5.:DEFINF] INF Initial size of index file
	.IF P4 <> "" .SETN INF 'P4'.
	.IF INF > MXF-5 .SETN INF MXF-5
	.IF P2 = "" .ASKS [1:12.] P2 Volume name
.RT11A:	.IF RTRSDO <> "RT" .GOTO RT11A1
	.ASKN [70.:2170.:140.] MXF Max No of files on volume
	.SETN MXF (MXF+69.)/70.
	.SETS NDSWT "/NU:'MXF'"
.RT11A1: .IFT LOGOFF .ASK LOGOFF Log off when done
	.IFT NOTRSX .GOTO ASKMOU
	.SETN NUFD 0
	;
	; Enter UFD's to create on this disk, (. = '<UIC>'), CR when done.
	.IF P9 = "DL" .OR .IF P9 = "DK" .OR .IF P9 = "DM" ; [1,3] is created automatically.
	;
.GETUFD:
	.INC NUFD
	.ASKS [0:7] UFD'NUFD' UFD
	.IFT <DEFAUL> .GOTO ASKMOU
	.IF UFD'NUFD' = "." .SETS UFD'NUFD' <UIC>
	.SETS P8 UFD'NUFD'[1:1]
	.IF P8 <> "[" .SETS UFD'NUFD' "["+UFD'NUFD'+"]"
	.GOTO GETUFD
.ASKMOU: .IFF LOGOFF .ASK LVEMOU Leave mounted on exit
	.SETS CLI <CLI>
	.IF <CLI> <> "MCR" MCR SET /CLI=TI:MCR
	.IF P9 = "MM" .GOTO MTAPE
	.IFT FORMAT FMT 'P1''DENS'/@Y/VE
	.IF <EXSTAT> <> 1 .GOTO ERROR
	BAD 'P1'/LI
	.IFT NOTRSX .AND .IF <EXSTAT> <> 1 ; Cannot handle RT-11 or DOS disk with bad blocks correctly.
	.IFT NOTRSX .GOTO INRTDO
	INI 'P1''P2'/INF='INF'./MXF='MXF'.'INC'
	.IF <EXSTAT> <> 1 .GOTO ERROR
	DMO 'P1''P2'/LOCK=NO
	MOU 'P1''P2'/VI
	.IF <EXSTAT> <> 1 .GOTO ERROR
	.IF P9 = "DL" .OR .IF P9 = "DK" .OR .IF P9 = "DM" UFD 'P1'[1,3]
.UFD:
	.DEC NUFD
	.IF NUFD = 0 .GOTO FINISH
	.SETS P8 UFD'NUFD'
	UFD 'P1''P8'
	.IF <EXSTAT> <> 1 .GOTO ERROR
	.GOTO UFD
.ERROR: ; Initialisation abandoned.
	DMO 'P1'
	.SETF LOGOFF
	.SETF LVEMOU
	.GOTO	FINISH
.; Magtape
.MTAPE:	.ASKN [::1600.] DENSTY Recording density: 800/1600 bpi
	.IF DENSTY <> 800. .AND .IF DENSTY <> 1600. .GOTO MTAPE
	.IF RTRSDO = "RT" .SETS P2 "RT11A"
	.IF RTRSDO = "DO" .GOTO ZETAPE
	.SETN NO 0.
	.TEST P2
	.IFT <NUMBER> .SETN NO 0'P2'.
	.IF P2 = "" .ASKN [1.:999.] NO Tape number
	.TEST P2
	.IFT <NUMBER> .OR .IF P2 = "" .SETS P2 "SIR'NO%DZR3'"
	INI 'P1''P2'/DENS='DENSTY'
	.GOTO FINISH
.ZETAPE: .SETS NDSWT "/DNS:'DENSTY'"
.INRTDO: FLX 'P1'/'RTRSDO'/ZE'NDSWT'
.FINISH: DMO 'P1''P2'/LOCK=NO
	DEA 'P1'
	.IFT LOGOFF .XQT BYE
	.IFT LOGOFF .EXIT
	.IF RTRSDO <> "RS" .SETS P2 "/FOREIGN"
	.IFT LVEMOU MOU 'P1''P2'
	.IF CLI <> "MCR" SET /CLI=TI:'CLI'
	.DISABLE DISPLAY
	.EXIT
.;
.INIT:	.ENABLE SUBSTITUTION
	.DISABLE LOWERCASE
	.SETF LVEMOU
	.SETS NDSWT ""
	.IF P1 <> "?" .AND .IF P1 <> "HELP" .RETURN
	;
	; INIT will initialise a disk or DECtape.
	;
	; Call as:
	;
	; @INIT <device> <volume-name> <max no of files> <initial index size>
	;
	; Numbers are in decimal (omit the .).
	; You will be prompted for any omitted parameters.
	;
	.IFT <PRIVIL> ; Valid devices are DK0/1/2/3, DL0/1/2, DD0/1, DX0/1, DY0/1, DM0/1
	.IFF <PRIVIL> ; Valid devices are DK2/3, DD0/1, DX0/1, DY0/1, DL1/2
	; Use DX instead of DY for single-density floppy disks.
	.SETS P1 ""
	.SETS P2 ""
	.SETS P3 ""
	.SETS P4 ""
	.RETURN
