data       segment    public
fhandle    dw      ?
hpath      dw      ?
hdta       dw      ?
hatt       db      ?
hsz        dw      ?
data       ends
main       proc    near
           call    init
           call    map
           call    getpath
           call    index
           call    chapters
           call    close
           endp
crlf       proc    near
           mov     ah,02h     ;print
           mov     dl,0dh     ;CR
           int     21h
           mov     dl,0ah     ;LF
           int     21h
           ret
           endp
getpath    proc    near
           mov     ah,2fh     ;get disk transfer area
           int     21h
           add     bx,1ah     ;add 26 (size address)
           mov     hdta,bx    ;save disk transfer area
           mov     ax,1900h   ;get DOS drive no.
           int     21h
           add     al,41h     ;make it a letter
           mov     [2801],al  ;store it
           mov     byte ptr [2802],3ah   ;add a ":"
           mov     byte ptr [2803],5ch   ;add a "\"
           mov     si,2804    ;location for path
           mov     dl,00h
           mov     ah,47h     ;get full path
           int     21h
           jc      errgp1
           cmp     byte ptr [2804],00h
           je      ls2        ;jump if root directory
ls1:       inc     si
           cmp     byte ptr [si],00h
           jne     ls1        ;loop till end of path
           mov     byte ptr [si],5ch   ;add a "\"
           inc     si
ls2:       mov     hpath,si   ;store end of path address
           ret
errgp1:    int     20h
           endp
size       proc    near
           mov     ax,4300h   ;get file attributes
           mov     dx,2801    ;path and filename
           int     21h
           jc      errs1      ;set fail flag
           mov     ax,4e00h   ;get size
           int     21h
           jc      errs1
           mov     si,hdta
           mov     dx,[si]
           mov     hsz,dx
           ret
errs1:     mov     byte ptr [2901],01h
           endp
           ret
           endp
init       proc    near
           mov     ah,09h     ;print claims
           mov     dx,msg001
           int     21h
           call    crlf
           mov     ah,09h     ;print claims
           mov     dx,msg002
           int     21h
           call    crlf
           mov     ah,09h     ;print claims
           mov     dx,msg003
           int     21h
           call    crlf
           jmp     cprt
msg001:    db      'This Program is copyrighted  1992.$'
msg002:    db      'by ROBERT WALLINGFORD, P.E., 2338 N. McVicker Ave., Chicago, IL  60639-2216.$'
msg003:    db      'Distributed as a part of the Shareware  BOOK  collection of programs.$'
cprt:      mov     ah,5bh     ;create file
           mov     cx,0h
           mov     dx,offset bigb    ;name of file
           int     21h
           jc      error1
           mov     fhandle,ax ;store handle
           mov     ah,09h     ;print msg
           mov     dx,msg4
           int     21h
           call    crlf
           jmp     end1
error1:    mov     ah,09h     ;print msg
           mov     dx,msg1
           int     21h
           call    crlf
           mov     ah,09h     ;print msg
           mov     dx,msg2
           int     21h
           call    crlf
           mov     ah,09h     ;print msg
           mov     dx,msg3
           int     21h
           call    crlf
           int     20h
end1:      ret
           endp
msg1:      db      'File already exists.$'
msg2:      db      'To remake BIGBOOK.FIL, you must either delete or rename the old file,$'
msg3:      db      'or move it to another directory or subdirectory, then delete this copy.$'
msg4:      db      'BIGBOOK.FIL  has been created. The map area is now being added.$'
bigb:      db      'BIGBOOK.FIL $'
close      proc    near
           mov     ah,3eh     ;close file
           mov     bx,fhandle ;get handle
           int     21h
           jc      errc
           mov     ah,09h     ;print msg
           mov     dx,msgc
           int     21h
errc:      int     20h        ;end program anyway
           ret
           endp
msgc:      db      'BIGBOOK.FIL  has been successfully closed. Thank You. $'
map        proc    near       ;make index area
           mov     ah,40h     ;write file
           mov     bx,fhandle ;get handle
           mov     cx,000ch   ;number of bytes to write
           mov     dx,offset mp1
           int     21h
           mov     ah,40h     ;write file
           mov     cx,00f4h   ;fill map with spaces
           mov     dx,offset mp2
           int     21h
           mov     ah,40h     ;write file
           mov     cx,0003h   ;number of bytes
           mov     dx,offset mp3
           int     21h
           mov     ah,09h     ;print msg
           mov     dx,msg8
           int     21h
           call    crlf
           mov     ah,09h     ;print msg
           mov     dx,msg9
           int     21h
           call    crlf
           ret
           endp
mp1:       db      3h,1h,42h,4fh,4fh,4bh,1h,0h,0h,0h,3h,1h
mp2:       db      0f4h   dup  (0)
mp3:       db      24h,0dh,0ah
msg8:      db      'Map area, password, and index offset have been added.$'
msg9:      db      'INDEX.FIL  is now being added.$'
addit      proc    near
           mov     ax,3da0h   ;open file to read
           mov     dx,2801    ;path and filename
           int     21h
           jc      errad1     ;set fail flag
           mov     bx,ax
           mov     ax,3f00h   ;read file to buffer
           mov     cx,hsz     ;how many to read
           mov     dx,3000    ;buffer
           int     21h
           mov     ax,3e00h   ;close file
           int     21h
           mov     ax,4000h   ;write file from buffer
           mov     bx,fhandle ;get handle
           mov     cx,hsz     ;how mamy to write
           sub     cx,01h     ;to remove EOF character
           mov     dx,3000    ;buffer
           int     21h
           mov     ah,40h     ;write $, CR, LF, 3 spaces
           mov     bx,fhandle ;get handle
           mov     cx,0006h   ;number of bytes
           mov     dx,offset ep
           int     21h
           mov     ax,4201h   ;get offset values
           mov     bx,fhandle ;get handle
           mov     cx,0000h   ;do not move pointer
           mov     dx,0000h   ;do no move pointer
           int     21h
           mov     word ptr [2904],ax   ;store offset (low)
           mov     word ptr [2902],dx   ;store offset (high)
           ret
errad1:    mov     byte ptr [2901],01h
           ret
ep:        db      24h,0dh,0ah,20h,20h,20h
           endp
index      proc    near
           mov     byte ptr [2901],00h  ;clear fail flag
           mov     si,hpath
           mov     byte ptr [si],49h   ;enter INDEX.FIL
           inc     si
           mov     byte ptr [si],4eh
           inc     si
           mov     byte ptr [si],44h
           inc     si
           mov     byte ptr [si],45h
           inc     si
           mov     byte ptr [si],58h
           inc     si
           mov     byte ptr [si],2eh
           inc     si
           mov     byte ptr [si],46h
           inc     si
           mov     byte ptr [si],49h
           inc     si
           mov     byte ptr [si],4ch
           inc     si
           mov     byte ptr [si],00h   ;nul terminated ASCII
           call    size
           call    addit
           cmp     byte ptr [2901],00h ;check fail flag
           je      gin1
           mov     ah,09h     ;print msg
           mov     dx,msgin2 
           int     21h
           int     20h        ;end program
gin1:      mov     ah,09h     ;print msg
           mov     dx,msgin1
           int     21h
           call    crlf
           ret
           endp 
msgin1:    db      'INDEX.FIL  has been added and CHAPTER0 offset has been computed and saved.$'
msgin2:    db      'INDEX.FIL  is missing or cannot be opened.$'
           endp 
chapters   proc    near
           mov     si,hpath 
           mov     byte ptr [si],43h   ;add CHAPTER-.P--
           inc     si
           mov     byte ptr [si],48h
           inc     si
           mov     byte ptr [si],41h
           inc     si
           mov     byte ptr [si],50h
           inc     si
           mov     byte ptr [si],54h
           inc     si 
           mov     byte ptr [si],45h
           inc     si 
           mov     byte ptr [si],52h
           inc     si
           mov     hpath,si   ;store chapter address
           mov     byte  ptr [si + 05h],00h
           mov     byte ptr [si + 01h],2eh
           mov     byte ptr [si + 02h],50h
           mov     byte ptr [2906],0ffh   ;initialize chapter
chloop:    mov     al,byte ptr [2907]    ;get page number
           mov     ah,00h
           mov     word ptr [2908],ax
           mov     byte ptr [2907],00h    ;initialize page
           mov     byte ptr [2901],00h    ;clear fail flag
           inc     byte ptr [2906]
           mov     ax,word ptr [2902]
           mov     word ptr [2910],ax     ;old ddh value
           mov     ax,word ptr [2904]
           mov     word ptr [2912],ax     ;old ddl value
           mov     si,hpath
           mov     al,byte ptr [2906]  ;get chapter number
           add     al,30h     ;make it character number
           cmp     al,39h
           jna     number
           add     al,07h     ;to make it a letter
number:    cmp     al,45h
           jne     skipe
           add     al,01h      ;skip chapter E
           inc     byte ptr [2906]
skipe:     mov     byte ptr [si],al
ploop:     inc     byte ptr [2907]    ;new page
           cmp     byte ptr [2906],00h  ;chapter 0 ?
           mov     al,byte ptr [2907]
           mov     byte ptr [2908],al
initlp:    mov     bl,00h
           mov     al,byte ptr [2907]
           mov     si,hpath
pnumb:     cmp     al,0ah
           jb      pnumb1
           sub     al,0ah
           add     bl,01h
           jmp     pnumb
pnumb1:    add     al,30h
           add     bl,30h
           mov     byte ptr [si + 03h],bl
           mov     byte ptr [si + 04h],al
           call    size
           call    addit
           mov     si,hpath
           cmp     byte ptr [2901],00h
           je      reptp
           cmp     byte ptr [2907],01h
           je      done
           call    ofst
           jmp     chloop
done:      ret
reptp:     mov     ah,09h
           mov     dx,msgch1
           int     21h
           mov     ah,02h
           mov     dl,byte ptr [si]
           int     21h
           mov     ah,09h
           mov     dx,msgch2
           int     21h
           mov     ah,02h
           mov     dl,byte ptr [si + 03h]
           int     21h
           mov     ah,02h
           mov     dl,byte ptr [si + 04h]
           int     21h
           mov     ah,09h
           mov     dx,msgch3
           int     21h
           call    crlf 
           jmp     ploop
           ret
           endp
msgch1:    db      'CHAPTER$'
msgch2:    db      '.P$'
msgch3:    db      '  has been added to BIGBOOK.FIL  with its index data block.$'
ofst       proc    near
           mov     al,byte ptr [2906]
           add     al,02h
           mov     ah,00h
           mov     bx,06h
           mul     bx
           mov     dx,ax
           mov     ax,4200h    ;move pointer
           mov     bx,fhandle
           mov     cx,0000h
           dec     word ptr [2908]
           int     21h
           mov     ax,4000h
           mov     bx,fhandle
           mov     cx,0006h
           mov     dx,2908
           int     21h
           mov     ax,4200h
           mov     bx,fhandle
           mov     cx,word ptr [2902]
           mov     dx,word ptr [2904]
           int     21h
           ret
           endp
           end
