include doszip/doszip.inc
include dir.inc
include dos.inc
include fblk.inc
include string.inc
include	conio.inc

	public	cmcompare

_DATA	SEGMENT

cp_identical db	'The two folders seems',10
	     db	'to be identical',0
cp_different db	'Only one panel use Short File Names',0

_DATA	ENDS

_DZIP	SEGMENT

COMPARE_UPDATE:
	xor	ax,ax
	push	si
	push	ax
	push	di
	push	ax
	call	panel_putitem
	call	panel_putitem
	ret

COMPARE_FB:
	push	ds
	push	si
	push	di
	lds	si,[bp-4]
	mov	ax,[bp-16]
	shl	ax,2
	add	si,ax
	lds	si,[si]
	mov	[bp-26],si
	mov	[bp-24],ds
	mov	di,bx
	cld
	lodsw
	mov	dl,al;ES:[DI]
	and	dl,_A_SUBDIR
	add	di,2
	mov	cx,4
    COMPARE_FB_00:
	lodsw
	sub	ax,es:[di]
	jnz	COMPARE_FB_04
	add	di,2
	loop	COMPARE_FB_00
	mov	cx,WMAXPATH
    COMPARE_FB_01:
	lodsb
	or	al,al
	jz	COMPARE_FB_02
	sub	al,es:[di]
	jnz	COMPARE_FB_04
	inc	di
	loop	COMPARE_FB_01
	dec	di
    COMPARE_FB_02:
	cmp	al,es:[di]
	jne	COMPARE_FB_04
	inc	ax
    COMPARE_FB_03:
	pop	di
	pop	si
	pop	ds
	ret
    COMPARE_FB_04:
	xor	ax,ax
	jmp	COMPARE_FB_03

cmcompare PROC DIST
	push	bp
	mov	bp,sp
	sub	sp,26
	push	si
	push	di
	mov	si,panela
	mov	di,panelb
	cmp	si,cpanel
	je	cmcompare_two
	xchg	si,di			; Set SI to current panel
    cmcompare_two:
	call	panel_stateab		; Need two panels
	jnz	cmcompare_long
	jmp	cmcompare_null
    cmcompare_long:
	mov	bx,[si]
	mov	ax,[bx]
	mov	bx,[di]
	mov	dx,[bx]
	and	ax,_W_LONGNAME		; Need equal file names
	and	dx,_W_LONGNAME
	cmp	ax,dx
	je	cmcompare_start
	jmp	cmcompare_19
    cmcompare_start:
	mov	bx,word ptr [si.pn_wsub]
	movmx	[bp-4],[bx.ws_fcb]	; fblk **A
	mov	bx,word ptr [di.pn_wsub]
	movmx	[bp-8],[bx.ws_fcb]	; fblk **B
	mov	ax,[si.pn_fcb_count]
	mov	[bp-10],ax		; count A
	mov	[bp-16],ax
	mov	ax,[di.pn_fcb_count]
	mov	[bp-12],ax		; count B
	mov	[bp-18],ax
	push	ds			; Select all files in both panels
	push	si
	xor	cx,cx
	les	bx,[bp-4]
    cmcompare_03:
	cmp	cx,[bp-16]
	jnb	cmcompare_04
	lds	si,es:[bx]
	or	[si.fb_flag],_A_SELECTED
	add	bx,4
	inc	cx
	test	[si.fb_flag],_A_SUBDIR
	jz	cmcompare_03
	and     [si.fb_flag],not _A_SELECTED
	dec	word ptr [bp-10]
	jmp	cmcompare_03
    cmcompare_04:
	les	bx,[bp-8]
	xor	cx,cx
    cmcompare_05:
	cmp	cx,[bp-18]
	jnb	cmcompare_06
	lds	si,es:[bx]
	or	[si.fb_flag],_A_SELECTED
	add	bx,4
	inc	cx
	test	[si.fb_flag],_A_SUBDIR
	jz	cmcompare_05
	and     [si.fb_flag],not _A_SELECTED
	dec	word ptr [bp-12]
	jmp	cmcompare_05
    cmcompare_06:
	pop	si
	pop	ds	; If both panels have zero files they are identical
	mov	ax,[bp-10]
	add	ax,[bp-12]
	jnz	cmcompare_08
    cmcompare_07:
	mov	dx,offset cp_identical
	jmp	cmcompare_20
    cmcompare_08:		; If one of the panels have zero files
	xor	ax,ax   	; then everything is ok (selected)
	cmp	ax,[bp-10]
	je	cmcompare_09
	cmp	ax,[bp-12]
	jne	cmcompare_10
    cmcompare_09:
	call	COMPARE_UPDATE
	jmp	cmcompare_null
    cmcompare_10:		; Compare file blocks and de-select if equal
	mov	[bp-14],ax	; Number of identical files
	mov	[bp-16],ax	; Loop count A
    cmcompare_11:
	mov	ax,[si.pn_fcb_count]
	mov	ax,[bp-16]
	cmp	ax,[si.pn_fcb_count]
	jnb	cmcompare_14
	xor	ax,ax
	mov	[bp-18],ax
    cmcompare_12:
	mov	ax,[di.pn_fcb_count]
	mov	ax,[bp-18]
	cmp	ax,[di.pn_fcb_count]
	jnb	cmcompare_13
	les	bx,[bp-8]
	shl	ax,2
	add	bx,ax
	movmx	[bp-22],es:[bx]
	les	bx,es:[bx]
	test	byte ptr es:[bx],_A_SUBDIR
	jnz	cmcompare_15
	call	COMPARE_FB
	or	dl,dl
	jnz	cmcompare_15
	or	al,al
	jz	cmcompare_15
	les	bx,[bp-22]
	mov	ax,es:[bx]
	and	ax,not _A_SELECTED
	mov	es:[bx],ax
	les	bx,[bp-26]
	mov	ax,es:[bx]
	and	ax,not _A_SELECTED
	mov	es:[bx],ax
	inc	word ptr [bp-14]
    cmcompare_13:
	inc	word ptr [bp-16]
	jmp	cmcompare_11
    cmcompare_14:
	call	COMPARE_UPDATE
	mov	ax,[bp-10]
	cmp	ax,[bp-12]
	jne	cmcompare_16
	cmp	ax,[bp-14]
	jne	cmcompare_16
	jmp	cmcompare_07
    cmcompare_15:
	inc	word ptr [bp-18]
	jmp	cmcompare_12
    cmcompare_16:
	mov	ax,1
	jmp	cmcompare_end
    cmcompare_null:
	xor	ax,ax
    cmcompare_end:
	pop	di
	pop	si
	mov	sp,bp
	pop	bp
	ret
    cmcompare_19:
	mov	dx,offset cp_different
    cmcompare_20:
	mov	ax,offset cp_compare
	call	@stdmsg
	jmp	cmcompare_null
cmcompare ENDP

_DZIP	ENDS

	END
