;*********************************************
;
; External command for Davex
;
; sysalias -- create an alias for a SYS file
;             or S16 file
;
; The alias will be an executable file whose
; only function is to launch the original
; file, after possibly setting the prefix
; and passing startup-file information to
; the target.
;
; This permits SYS and S16 files to appear
; to be in several places at once without
; actually taking up much disk space, and
; it also makes it easy to launch apps with
; convenient prefixes and startup pathnames.
;
;*********************************************
;
; Modified 7-Jul-87 DL ==> v1.1
;   -p allowed; specifies prefix
;
; Modified 12-Dec-87 DL ==> v1.2
;   -s allowed; specifies startup path
;
; Modified 15-Oct-89 DL ==> Merlin source
;
; Modified 16-Oct-89 DL ==> v1.3
;  "Catalyst buffer" changed to "startup buffer"
;  If startup buffer is large, creates a
;    smaller one instead of giving up
;  Creates aliases for S16 files, which
;    can set prefix, send Open message,
;    and Quit to original app
;
;*********************************************
;
; Converted to MPW IIgs 21-Sep-92 DAL
;
;*********************************************
;
; Known bugs:
;   The created Alias file should probably check
;   to make sure the file it's running is STILL
;   a SYS file & STILL has a Startup buffer
;
;*********************************************
	.include "Common/2/Globals2.asm"
	.include "Common/2/Apple.Globals2.asm"
	.include "Common/2/Mli.globals2.asm"
	.include "Common/Macros.asm"


.segment	"CODE_A000"

orgadr	= $A000
; org orgadr

;
; Hard-coded offsets into TheImage
;
S16ImageOffset	= $0167
S16OpenOffset	= $02DB
thePathOffset	= $00E5
thePrefixOffset	= $0126
S16QuitToOffset	= $0251
S16PrefixOffset	= $0292

myVersion	= $13
minVersion	= $10
;*********************************************
	rts
	.byte $ee,$ee
	.byte myVersion,minVersion
	.byte %00000000	;hardware req
	.addr descr
	.addr orgadr
	.addr start
	.byte 0,0,0,0
; parameters here
	.byte 0,t_path
	.byte 0,t_path
	.byte $80+'p',t_path
	.byte $80+'s',t_path
	.byte 0,0
descr:	pstr "create alias for SYS or S16 file"
	
;*********************************************
; dum xczpage
path1	= xczpage	;ds 2
path2	= path1+2	;ds 2
stpath	= path2+2	;ds 2
pfxptr	= stpath+2	;ds 2
s16_flag	= pfxptr+2	;ds 2
; dend
;*********************************************
myerror:	jmp xProDOS_err
start:
;
; get info on 1st file to make sure it
; exists and is SYS or S16
;
	lda #0
	jsr xgetparm_n
	sta path1+1
	sty path1
	sta info_path+1
	sty info_path
	lda #10
	sta info_parms
	jsr mli
	.byte mli_gfinfo
	.addr info_parms
	bcs myerror
;
	lsr s16_flag
	lda info_ftype
	cmp #$FF
	beq is_sys
	cmp #$B3
	bne not_s16
	ror s16_flag
	bmi is_sys	;always
;
not_s16:	jsr xmess
	.byte cr
	asc "*** not a SYS or S16 file"
	.byte cr,0
	jmp xerr
;
; create a SYS sysalias file
;
is_sys:
	lda #1
	jsr xgetparm_n
	sta path2+1
	sty path2
	sta cr_path+1
	sty cr_path
	jsr mli
	.byte mli_create
	.addr create_parms
	bcc createdok
	jmp xProDOS_err
createdok:
;
	bit s16_flag
	bmi skip_sysbuff
	lda #$ef
	sta TheImage+4
	jsr startup_sz
	cmp #69
	bcc sysb_ok
;
	jsr xmess
	asc "(note: creating smaller startup buffer)"
	.byte cr,0
	lda #68
;
sysb_ok:
	sta TheImage+5
	cmp #0
	beq no_sysbuff
	dec TheImage+4		;make $EE!
no_sysbuff:
skip_sysbuff:
;
; If -s given, copy path into startup buffer
;
	lda #'s'+$80
	jsr xgetparm_ch
	bcs no_stpath
	sta stpath+1
	sty stpath
; copy startup path into one of the images
	bit s16_flag
	bmi stuff_s16st
; stuff SYS startup buffer
	ldy #0
	lda (stpath),y
	cmp TheImage+5
	bcs StPathBig
	tay
copystp:	lda (stpath),y
	sta TheImage+6,y
	dey
	bpl copystp
	jmp no_stpath
StPathBig:	jsr xmess
	.byte cr
	asc "*** startup path too big for buffer"
	.byte cr,0
	jmp xerr
stuff_s16st	= *
	ldy #0
	lda (stpath),y
	cmp #64
	bcs StPathBig
	tay
stuff16a:	lda (stpath),y
	sta TheImage+S16OpenOffset,y
	dey
	bpl stuff16a
no_stpath	= *
;
; copy name of 1st file into thePath
; in code image
;
	ldy #0
	lda (path1),y
	tay
copyname:	lda (path1),y
	sta TheImage+thePathOffset,y
	sta TheImage+S16QuitToOffset,y
	dey
	cpy #-1
	bne copyname
;
; copy prefix into buffer if -p given
;
	lda #'p'+$80
	jsr xgetparm_ch
	bcs nopfx
	sta pfxptr+1
	sty pfxptr
	ldy #0
	lda (pfxptr),y
	cmp #64
	bcc pfxShort
	jsr xmess
	.byte cr
	asc "*** prefix too long"
	.byte cr,0
	jmp xerr
pfxShort	= *
	tay
copyPfx:	lda (pfxptr),y
	sta TheImage+thePrefixOffset,y
	sta TheImage+S16PrefixOffset,y
	dey
	cpy #-1
	bne copyPfx
nopfx	= *
;
; open 2nd file & write it
;
	lda path2+1
	ldy path2
	jsr open
	sta write_ref
	sta write_ref2
	bit s16_flag
	bmi write16
	jsr mli
	.byte mli_write
	.addr write_parms
	bcc writ
err0:	jmp xProDOS_err
write16:	jsr mli
	.byte mli_write
	.addr write_s16
	bcs err0

writ:	lda write_ref
	jsr close
	lda path2+1
	ldy path2
	sta info_path+1
	sty info_path
	lda #7
	sta info_parms
	jsr mli
	.byte mli_sfinfo
	.addr info_parms
	bcc set
	jmp xProDOS_err
set	= *
	rts
;
write_parms:	.byte 4
write_ref:	.res 1
	.addr TheImage
	.addr image_end-image
	.addr 0

write_s16:	.byte 4
write_ref2:	.res 1
	.addr TheImage+s16imageOffset
	.addr s16image_end-s16image
	.addr 0
;***********************************************
create_parms:	.byte 7
cr_path:	.res 2
	.byte %11000011	;unlocked
	.byte $00	;type $00 initially
	.addr 0	;auxtype
	.byte 1	;sttype
	.addr 0,0	;date/time
;
info_parms:	.res 1
info_path:	.res 2
	.res 1	;access
info_ftype:	.res 1
	.res 2	;aux
	.res 1	;sttype
	.res 2	;blocks
	.addr 0,0,0,0	;date/time
;***********************************************
open:	sta open_path+1
	sty open_path
	jsr mli
	.byte mli_open
	.addr open_parms
	bcc opened
proerr:	jmp xProDOS_err
opened:	lda open_ref
	rts
;
open_parms:	.byte 3
open_path:	.res 2
	.addr filebuff
open_ref:	.res 1
;
; close (a)
;
close:	sta close_ref
	jsr mli
	.byte mli_close
	.addr close_parms
	rts
close_parms:	.byte 1
close_ref:	.res 1
;
; get size of SYS startup buffer
;
startup_sz	= *
	lda path1+1
	ldy path1
	jsr open
	sta read_ref
	jsr mli
	.byte mli_read
	.addr read_parms
	bcc read_ok
	jmp xProDOS_err
read_ok:	lda read_ref
	jsr close
	lda pagebuff
	cmp #$4c
	bne sysb_0
	lda #$ee
	cmp pagebuff+3
	bne sysb_0
	cmp pagebuff+4
	bne sysb_0
	lda pagebuff+5
	rts
sysb_0:	lda #0
	rts
;
read_parms:	.byte 4
read_ref:	.byte 1
	.addr pagebuff
	.addr 10
	.addr 0
;***********************************************
;***********************************************
;
; IMAGE -- this code is written to the alias
; file, with or without a valid startup buffer
; and with the name of the original file
; inserted at the end.
;
; The code will copy itself to $1000 and execute
; there.
;
;***********************************************
TheImage:	proc export

image:	proc export, temporg $1000

;diff1	= $1000-image
;diff2	= $2000-image

	jmp image2+$1000    ;+diff2
	.byte $ee,$00	;maybe made into $ee
	.byte 0	;cb size
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
	.byte 0,0,0,0,0,0,0,0,0,0 ;10 zeroes
image2	= *
	ldx #0
copyme:	lda $2000,x
	sta $1000,x
	lda $2100,x
	sta $1100,x
	dex
	bne copyme
	jmp continue ; +diff1
i_error:	pha
	jsr $fc58
	pla
	jsr $fdda
	jsr $fbdd
	jsr $fbdd
	jsr $fd0c
	jsr mli
	.byte mli_bye
	.addr bye_parms  ; +diff1
;;;	brk
	.byte 0
bye_parms:	.byte 4,0,0,0,0,0,0
continue	= *
	lda thePrefix  ; +diff1
	beq noSetP
	jsr mli
	.byte mli_setpfx
	.addr i_setp  ; +diff1
	bcs i_error
noSetP	= *
;
	jsr mli
	.byte mli_open
	.addr i_openp  ; +diff1
	bcs i_error
;
	lda i_ref  ; +diff1
	sta i_ref2  ; +diff1
	sta i_ref3  ; +diff1
;
	jsr mli
	.byte mli_read
	.addr i_readp  ; +diff1
	bcs i_error
;
	jsr mli
	.byte mli_close
	.addr i_closep  ; +diff1
;
	lda $1005	;startup buff sz
	beq i_nocopy
	lda $1006
	beq i_nocopy
	tay
i_copycb:	lda $1006,y
	sta $2006,y
	dey
	cpy #-1
	bne i_copycb
i_nocopy	= *
;
	ldy thePath    ; +diff1
cppath280:	lda thePath,y  ; +diff1
	sta $280,y
	dey
	cpy #-1
	bne cppath280
	jmp $2000
;
i_openp:	.byte 3
	.addr thePath  ; +diff1
	.addr $C00
i_ref:	.byte 0
;
i_readp:	.byte 4
i_ref2:	.byte 0
	.addr $2000
	.addr $ffff
	.addr 0
;
i_closep:	.byte 1
i_ref3:	.byte 0
;
i_setp:	.byte 1
	.addr thePrefix  ; +diff1
;
thePath:	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0
thePrefix:
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0,0,0,0,0,0
	.byte 0,0,0,0,0
image_end
;********************************************
	export s16image
s16image:
	hex ED0100000000000070010000FF0A0402
	hex 00000100000000000000000000000000
	hex 00000100000000002C00400020202020
	hex 20202020202020202020202020202020
	hex F2700100004BABADE600F00A22A800E1
	hex 090098000000AD2F0129FF00F06B4848
	hex F4000018690A00488D9E0048A2020222
	hex 0000E1F400C04848A20209220000E168
	hex 8DA000688DA200B040F40000F42701AD
	hex A20048ADA00048F40000AD9E0048A202
	hex 28220000E1F40100F40100ADA20048AD
	hex A00048A20115220000E1ADA20048ADA0
	hex 0048A20210220000E122A800E1290090
	hex 0000000000A5000000000000000000E6
	hex 000000000000000000
	hex 51
	export S16QuitTo
S16QuitTo:
	hex 000033343536
	hex 37383132333435363738313233343536
	hex 37383132333435363738313233343536
	hex 37383132333435363738313233343536
	hex 37383132333435363738
	hex 50
	Export S16Prefix
S16Prefix:
	hex 0000333435
	hex 36373831323334353637383132333435
	hex 36373831323334353637383132333435
	hex 36373831323334353637383132333435
	hex 3637383132333435363738
	hex 4F
	hex 0000000001000000
	export S16Open
S16Open:	hex 000033343536373831323334
	hex 35363738313233343536373831323334
	hex 35363738313233343536373831323334
	hex 35363738313233343536373831323334
	hex 3536373800F504000D009800F502F045
	hex 002701F504008A009000F504009000A5
	hex 00F504009A00E600F70F000000000C03
	hex 12243C40484B4F56676B767A00
	export s16image_end
s16image_end:
;****************************************************
;****************************************************
;           keep s16image
;****************************************************
;*
;* S16 launcher image for Davex 8 sysalias command
;*
;* Dave Lyons, 15-Oct-89
;*
;* Optionally does a SET_PREFIX(0,...)
;* Optionally does a MessageCenter(Add,1,...)
;* Does a QUIT
;*
;****************************************************
;Kool       start
;
;tool       = $e10000
;P16        = $e100a8
;
;           phk
;           plb
;
;* set prefix 0
;           lda PrefixPath
;           beq noSetPfx
;           jsl p16
;           dc  i'$0009'
;           dc  i4'PfxParms'
;noSetPfx   anop
;
;* add message 1 to MessageCenter
;           lda OpenPath
;           and #$00ff
;           beq noMessage
;
;           pha
;           pha           ;space for result
;           pea 0
;           clc
;           adc #10
;           pha           ;size = pathlen + 10
;           sta theLength
;           pha           ;space for user id
;           ldx #$0202    ;MMStartUp()
;           jsl tool
;           pea $C000     ;locked+fixed
;           pha
;           pha
;           ldx #$0902    ;NewHandle
;           jsl tool
;           pla
;           sta theHand
;           pla
;           sta theHand+2
;           bcs noMessage
;
;           pea OpenBlock|-16
;           pea OpenBlock
;           lda theHand+2
;           pha
;           lda theHand
;           pha
;           pea 0
;           lda theLength
;           pha
;           ldx #$2802    ;PtrToHand
;           jsl tool
;
;           pea 1         ;add message
;           pea 1         ;type 1
;           lda theHand+2
;           pha
;           lda theHand
;           pha
;           ldx #$1501    ;MessageCenter   act type msg
;           jsl tool
;
;           lda theHand+2
;           pha
;           lda theHand
;           pha
;           ldx #$1002    ;DisposeHandle
;           jsl tool
;
;noMessage  anop
;
;* quit to the destination S16 (or EXE?) file
;           jsl p16
;           dc  i'$0029'
;           dc  i4'QuitParms'
;           brk 0
;
;QuitParms  dc i4'QuitPath'
;           dc i'0,0'
;
;PfxParms   dc i'0'
;           dc i4'PrefixPath'
;
;theLength  dc i'0'
;theHand    dc i4'0'
;
;           dc c'Q'
;QuitPath   dc i1'0,0'
;           dc c'345678123456781234567812345678'
;           dc c'12345678123456781234567812345678'
;
;           dc c'P'
;PrefixPath dc i1'0,0'
;           dc c'345678123456781234567812345678'
;           dc c'12345678123456781234567812345678'
;
;           dc c'O'
;OpenBlock  dc i4'0'
;           dc i'1'   ;msg type 1
;           dc i'0'   ;Open (not Print)
;OpenPath   dc i1'0,0'
;           dc c'345678123456781234567812345678'
;           dc c'12345678123456781234567812345678'
;           dc i1'0'
;
;           end
;***************************************************
