  ******************************
  *      SoftProtect v1.0      *
  * Written by Grzegorz Królik *
  ******************************


     incdir  "Include:"
     include "exec/exec_lib.i"
     include "exec/execbase.i"
     include "libraries/dosextens.i"
     include "libraries/arpbase.i"

call macro
     jsr   _LVO\1(a6)
     endm

     section "SoftProtect v1.0",code

     link    a5,#-32             ;reserve our workspace

     movem.l  d0/a0,-8(a5)       ;save the argument pointer

     move.l  4.w,a6              ;get ExecBase
     move.l  ThisTask(a6),a2     ;get our task structure
     tst.l   pr_CLI(a2)          ;are we CLI or Workbench?
     bne.s   FromCLI             ;we are CLI, so continue

     lea     pr_MsgPort(a2),a0   ;we are Workbench,
     call    WaitPort            ;so handle  messages...
     lea     pr_MsgPort(a2),a0   ; (otherwise visit from guru...)
     call    GetMsg              ;GetMsg function called
     move.l  d0,d2
     call    ReplyMsg            ;ReplyMsg function called
     bra.w   Error               ;quit

FromCLI:
     lea     DOSName(pc),a1      ;this opens the dos.library
     moveq   #36,d0
     call    OpenLibrary         ;OpenLibrary function called
     tst.l   d0                  ;which OS are we under?
     beq.s   OldOS               ;it's 1.2 or 1.3
     clr.l   -12(a5)             ;clear the ARPBase pointer
     move.l  d0,-16(a5)          ;save the DOSBase pointer

     move.l  #MyInfo,d1          ;address of the information string
     move.l  -16(a5),a6
     call    VPrintf             ;VPrintf function called
     tst.l   d0                  ;Did an error occured?
     bmi.s   CloseDOS            ;yes, close the library

     move.l  #Template,d1        ;input template address
     clr.l   -24(a5)
     clr.l   -20(a5)
     lea     -24(a5),a0
     move.l  a0,d2               ;our space
     moveq   #0,d3
     call    ReadArgs            ;ReadArgs function called
     tst.l   d0
     bne.s   Ok                  ;arguments ok

     call    IoErr               ;IoErr function called - error determined
     move.l  d0,d1               ;error code
     move.l  d3,d2
     call    PrintFault          ;PrintFault function called
     bra.s   CloseDOS            ;we can't do anything more

Exit:
     move.l  -28(a5),d1          ;argument address
     call    FreeArgs            ;FreeArgs function called

CloseDOS:
     move.l  a6,a1               ;DOSBase as an argument to CloseLibrary()
     move.l  4.w,a6              ;ExecBase reactivated
     call    CloseLibrary        ;CloseLibrary function called
     bra.s   Error

Ok:
     move.l  d0,-28(a5)          ;remember this to free args later

     cmpi.l  #-1,-24(a5)         ;get current value
     beq.w   Protect

     cmpi.l  #-1,-20(a5)
     beq.w   UnProtect

     tst.l   -12(a5)             ;is the arp.library opened
     beq.s   Exit                ;no, it isn't
     bra.s   CloseARP

OldOS:
     lea     ARPName(pc),a1      ;this open the arp.library
     moveq   #ArpVersion,d0      ;version of the arp.library
     call    OpenLibrary         ;OpenLibrary function called
     tst.l   d0                  ;opened?
     beq.s   NoARP               ;no, inform your user about it
     move.l  d0,-12(a5)          ;save the ARPBase pointer

     lea     MyInfo(pc),a0       ;to be pc-relative
     move.l  d0,a6
     move.l  DosBase(a6),-16(a5) ;get the DOSBase pointer
     call    Printf              ;Printf function called
     tst.l   d0                  ;Did an error occured?
     bmi.s   CloseARP            ;yes, close the library

     movem.l -8(a5),d0/a0        ;get the argument pointer
     sub.l   a1,a1               ;no extra help
     clr.l   -24(a5)
     clr.l   -20(a5)
     lea     -24(a5),a2          ;our space
     lea     Template(pc),a3     ;to be pc-relative
     call    GADS                ;GADS function called
     tst.l   d0
     bge.s   Ok                  ;arguments ok

     move.l  -24(a5),a1          ;error description
     call    Puts                ;Puts function called

CloseARP:
     move.l  a6,a1               ;ARPBase as an argument to CloseLibrary()
     move.l  4.w,a6              ;ExecBase reactivated
     call    CloseLibrary        ;CloseLibrary function called

Error:
     unlk    a5                  ;free reserved workspace
     moveq   #0,d0               ;error = 0
     rts                         ;return

NoARP:
     lea     DOSName(pc),a1
     call    OldOpenLibrary      ;OldOpenLibrary function called
     tst.l   d0
     beq.s   Error               ;error in open
     move.l  d0,a6               ;get the DOSBase
     call    Output              ;Output function called
     tst.l   d0                  ;found?
     beq.w   CloseDOS            ;not found
     move.l  d0,d1               ;get the output handle
     move.l  #ArpText,d2         ;text address
     move.l  #26,d3              ;text length
     call    Write               ;Write function called
     bra.w   CloseDOS            ;exit

Protect:
     btst    #3,$bfe201          ;is the disk in df0: write protected?
     bne.s   PrintON             ;yes, it is
     ori.b   #12,$bfe201         ;bset #2,$bfe201, bset #3,$bfe201
     andi.b  #243,$bfe001        ;bclr #2,$bfe001, bclr #3,$bfe001
     moveq   #25,d1              ;time = 1/2 sec
     move.l  -16(a5),a6          ;get the DOSBase
     call    Delay               ;Delay function called
     ori.b   #4,$bfe001          ;bset #2,$bfe001
     moveq   #25,d1
     call    Delay               ;Delay function called
     andi.b  #251,$bfe201        ;bclr #2,$bfe201

PrintON:
     move.l  #StatInfo,d1        ;address of the text
     move.l  #ON,-32(a5)        ;save the arguments value
     bra.s   Print               ;print the status info

UnProtect:
     btst    #3,$bfe201          ;is the disk in df0: write protected?
     beq.s   PrintOFF            ;no, it isn't
     ori.b   #12,$bfe201         ;bset #2,$bfe201, bset #3,$bfe201
     andi.b  #243,$bfe001        ;bclr #2,$bfe001, bclr #3,$bfe001
     moveq   #25,d1              ;time = 1/2 sec
     move.l  -16(a5),a6          ;get the DOSBase
     call    Delay               ;Delay function called
     ori.b   #4,$bfe001          ;bset #2,$bfe001
     moveq   #25,d1
     call    Delay               ;Delay function called
     andi.b  #243,$bfe201        ;bclr #2,$bfe201, bclr #3,$bfe201

PrintOFF:
     move.l  #StatInfo,d1        ;address of the text
     move.l  #OFF,-32(a5)         ;save the arguments value

Print:
     tst.l   -12(a5)             ;are we under OS 1.2 or 1.3?
     bne.s   FromARP             ;yes, so use the ARP function
     lea     -32(a5),a0          ;get the arguments value
     move.l  a0,d2
     call    VPrintf             ;VPrintf function called
     bra.w   Exit                ;exit

FromARP:
     move.l  d1,a0               ;get address of the text
     lea     -32(a5),a1          ;get the arguments value
     move.l  -12(a5),a6          ;ARPBase reactivated
     call    Printf              ;Printf function called
     bra.w   CloseARP            ;exit


VerStr:   dc.b  0,"$VER: SoftProtect 1.0 (20.07.95)",0
DOSName:  DOSNAME
ARPName:  ArpName
ArpText:  dc.b  "you need arp.library V34+",10,0
MyInfo:   dc.b  10,155,"1;33mSoftProtect",155,"0m"," PUBLIC DOMAIN "
          dc.b  "© 1995 by Grzegorz Królik",10,10,0
StatInfo: dc.b  "Write Protection is %s",10,0
ON:       dc.b  "ON",0
OFF:      dc.b  "OFF",0
Template: dc.b  "P=PROTECT/S,U=UNPROTECT/S",0
          end
