 Rem Program inits codename passwords for
 Rem   Dndbbs Version v5.0a Release r2.0 Tweak t5.0a

 Rem $Include: 'dndbbs.inc'

 ' declare error routine
 On Error Goto Error.Routine

 Function DateS$
  DateS$=Format$(Now,"mm-dd-yyyy")
 End Function

 ' check command line
 Var$=Command$
 If Var$="/?" Or Var$="-?" Or Var$="/HELP" Then
    Goto Boot.Usage
 Endif
 Var=Instr(Var$,"/Q")
 If Var Then
    Var$=Left$(Var$,Var-1)+Mid$(Var$,Var+2)
    Quiet.Mode=True
 Endif
 Var=Instr(Var$,"/F")
 If Var Then
    Var$=Left$(Var$,Var-1)+Mid$(Var$,Var+2)
    File.Mode=True
 Endif
 NodeX=Nul
 Var$=Ltrim$(Var$)
 Var$=Rtrim$(Var$)
 If Left$(Var$,3)="/N:" Then
    Var$=Mid$(Var$,4)
    Select Case Len(Var$)
    Case 1
       Select Case Var$
       Case "0" To "9"
          NodeX=Var$
          Var$=Nul
       Case "A" To "Z"
          NodeX=Var$
          Var$=Nul
       Case Else
          Goto Boot.Usage
       End Select
    Case 2
       Var=Int(Val(Var$)+.5)
       If Var>=10 And Var<=99 Then
          NodeX=Mid$(Str$(Var),2)
          Var$=Nul
       Endif
    End Select
 Endif
 If Var$<>Nul Then
    Goto Boot.Usage
 Endif
 Call Get.Config
 If Quiet.Mode=False Then
    Color 15, 0
    Print "Makesys v"+Version$
    Print "Dndpath: "+DND.Path
    If File.Mode Then
       Print "Delete all user files and create from 'Initsys.txt' file(y/n)? ";
    Else
       Print "Delete all user files and create Sysop(y/n)? ";
    Endif
    Locate ,,1
    Do
       Char.Input$ = Nul
       While Char.Input$ = Nul
          Char.Input$ = Inkey$
       Wend
       Select Case Lcase$(Char.Input$)
       Case "n"
          Print "n"
          Print "Makesys terminated normally."
          Print "Returning to system."
          Color 7, 0
          End
       Case "y"
          Print "y"
          Exit Do
       End Select
    Loop
 Endif
 If Dir$(Log.FileName)<>Nul Then
    Kill Log.FileName
 Endif
 If Dir$(User.FileName)<>Nul Then
    Kill User.FileName
 Endif
 If Dir$(UserInv.FileName)<>Nul Then
    Kill UserInv.FileName
 Endif
 Close
 Open Container.FileName For Random Shared As #TempFile1 Len=Len(ContainerRecord)
 For Temp!=1 To Lof(TempFile1)/Len(ContainerRecord)
    Get TempFile1,Temp!,ContainerRecord
    If ContainerRecord.WithinContainer=SFalse Then
       If ContainerRecord.UserNumber<>SFalse Then
          ContainerRecord.UserNumber=SFalse
          Put TempFile1,Temp!,ContainerRecord
       Endif
    Endif
 Next
 Close
 Open User.FileName For Random Shared As #TempFile1 Len=Len(UserRecord)
 Strng="SYSOP"+Space$(25)
 Strng2="Sysop"
 Var2=10
 Gosub MakeSys
 If File.Mode Then
    If Dir$("initsys.txt")<>Nul Then
       Open "initsys.txt" For Input Shared As #TempFile2
       Do While Not Eof(TempFile2)
          Strng2="Fighter"
          Var2=1
          Line Input #TempFile2,Strng
          Strng=Rtrim$(Strng)
          If Len(Strng) Then
             Var=Instr(Strng,",")
             If Var Then
                Var1=Instr(Var+1,Strng,",")
                If Var1 Then
                   Var1$=Mid$(Strng,Var1+1)
                   Strng=Left$(Strng,Var1-1)
                   Var2$=Mid$(Strng,Var+1)
                   Strng=Left$(Strng,Var-1)
                   If Len(Var2$) Then
                      Strng2=Var2$
                   Endif
                   If Val(Var1$)>=1 And Val(Var1$)<=10 Then
                      Var2=Int(Val(Var1$)+.5)
                   Endif
                Endif
             Endif
             Strng=Rtrim$(Strng)
             If Len(Strng) Then
                Strng=Ucase$(Strng)
                Strng=Left$(Strng,30)
                Strng=Strng+Space$(30-Len(Strng))
                Gosub MakeSys
             Endif
          Endif
       Loop
    Endif
 Endif
 Close
 Open "ACCOUNTS.SIC" For Random Shared As #TempFile1 Len=LEN(UserProfile)
 If Lof(TempFile1) > SFalse Then
    For Var! = 1! To Lof(TempFile1)/Len(UserProfile)
       Get #TempFile1,Var!,UserProfile
       Strng=Space$(8)
       For Temp=1 To 8
          Mid$(Strng,Temp,1)=Chr$(Int(Rnd*31+1))
       Next
       Var$=Strng
       Call Cryptograph(Var$)
       UserProfile.Password=Var$
       Put #TempFile1,Var!,UserProfile
    Next
 Endif
 ' reset usage file
 Close
 Open Usage.FileName For Random Shared As #TempFile1 Len=26
 Field #TempFile1,26 As S$
 Lset S$="DND Usage File v"+Version$+Chr$(13)+Chr$(10)+Chr$(26)
 Put #TempFile1,1
 Field #TempFile1,10 As D$, 8 As T$, 8 As X$
 Lset D$=DateS$
 Lset T$=Format$(Now,"hh:mm:ss")
 Lset X$=Mkd$(0#)
 Put #TempFile1,2
 Lset D$="01-01-1980"
 Lset T$=Format$(Now,"hh:mm:ss")
 Lset X$=Mkd$(0#)
 Put #TempFile1,3
 If Quiet.Mode=False then
    Color 14,0
    Print "Sysop created."
    If File.Mode Then
       Print "Initsys.txt codenames loaded."
    Endif
    Color 15,0
    Print "Makesys terminated normally."
    Print "Returning to system."
 Endif
 Color 7,0
 End

Boot.Usage:
 Color 15,0
 Print "Makesys v"+Version$
 Color 14,0
 Print "Deletes and makes user files."
 Print "Command line:"
 Print " /F read codenames from 'Initsys.txt'"
 Print " /Q bypasses prompts."
 Print " /N:# node override 0-9,A-Z,10-99."
 Color 15,0
 Print "Must be run in the Dndbbs directory."
 Color 7,0
 End

MakeSys:
 UserRecord.CodeName=Strng
 ' write random control characters for password.
 Strng=Space$(20)
 For Temp=1 To 20
    Mid$(Strng,Temp,1)=Chr$(Int(Rnd*31+1))
 Next
 Var$=Strng
 Call Cryptograph(Var$)
 UserRecord.PassWord=Var$
 Call Crypto(Strng)
 UserRecord.PassWord3=Strng
 ' write remaining user file variables.
 UserRecord.ClassName=Strng2
 UserRecord.ClassType=Var2
 If Var2>=9 Then
    Var3=250
 Else
    Var3=18
 Endif
 For Temp6=1 To MaxStats
    UserRecord.Stats(Temp6)=Var3
 Next
 If Var2>=9 Then
    Var3=100
 Else
    Var3=False
 Endif
 For Temp6=1 To MaxWeapons
    UserRecord.Weapons(Temp6)=Var3
 Next
 For Var=1 To MaxItems
    UserRecord.Weapon(Var).Index=SFalse
    UserRecord.Weapon(Var).Charges=False
    UserRecord.Weapon(Var).Plus=False
 Next
 UserRecord.Room=1!
 If Var2>=9 Then
    Var3=100
 Else
    Var3=1
 Endif
 UserRecord.Level=Var3
 UserRecord.Experience=DFalse
 UserRecord.Gold=DFalse
 UserRecord.Borrow=False
 UserRecord.NumCalls=False
 If Var2>=9 Then
    Var3=250
 Else
    Var3=25
 Endif
 UserRecord.Fatigue=Var3
 UserRecord.FatigueMax=Var3
 UserRecord.Vitality=Var3
 UserRecord.VitalityMax=Var3
 UserRecord.Magic=Var3
 UserRecord.MagicMax=Var3
 UserRecord.Psionic=Var3
 UserRecord.PsionicMax=Var3
 UserRecord.Poison=False
 UserRecord.Invisible=True
 UserRecord.Race=1
 UserRecord.Proficiency=1
 UserRecord.Bank=False
 UserRecord.PlayersKilled=False
 UserRecord.MonstersKilled=False
 UserRecord.Brief=False
 UserRecord.DateOn=DateS$
 UserRecord.Align1=False
 UserRecord.Align2=False
 If Var2>=9 Then
    Var3=16384
 Else
    Var3=False
 Endif
 UserRecord.Flags=Var3
 UserRecord.Flags2=False
 UserRecord.WishsLeft=3
 UserRecord.MaxCalls=True
 UserRecord.FromHour=False
 UserRecord.FromMin=False
 UserRecord.ToHour=False
 UserRecord.ToMin=False
 UserRecord.SortInv=False
 UserRecord.TimeLimit=SFalse
 UserRecord.TimeUsed=SFalse
 UserRecord.Encumbrance=True
 UserRecord.DateOn=DateS$
 UserRecord.PassWord2=Nul
 UserRecord.Question=Nul
 UserRecord.Answer=Nul
 UserRecord.Prefix1=Nul
 UserRecord.Prefix2=Nul
 UserRecord.TimesLogon=0#
 UserRecord.TimesDied=0#
 UserRecord.First=SFalse
 UserRecord.Last=SFalse
 UserRecord.First2=SFalse
 UserRecord.Last2=SFalse
 UserRecord.NodeList=String$(127,"1")
 UserRecord.NodeList2=0!
 UserRecord.EditList=0!
 UserRecord.StatusVar=0
 UserRecord.Prefix3=Nul
 For VarZ=1 To 8
    UserRecord.ShortcutKey1(VarZ)=Nul
    UserRecord.ShortcutKey2(VarZ)=Nul
 Next
 For VarZ=1 To 10
    UserRecord.FunctionKeys(VarZ)=Nul
 Next
 UserRecord.ActionLineString = ""
 UserRecord.StatusLineString = ""
 For VarZ=1 To 10
    UserRecord.ObjectsCreated(VarZ)=0#
 Next
 UserRecord.WishsLeft=0
 UserRecord.Block1=0
 UserRecord.Block2=0
 UserRecord.Buffer22=0!
 UserRecord.Buffer23=0!
 UserRecord.Buffer24=0#
 UserRecord.Buffer25=0#
 VarX=Lof(TempFile1)/Len(UserRecord)+1
 Put TempFile1,VarX,UserRecord
 Return

Error.Routine:
 Error.Number=Err
 Call Get.Error(Error.Number)
 Color 15,0
 Print Strng+"."
 Color 7,0
 End

Sub Get.Config
 On Local Error Goto Config.Error
 DND.Path=Environ$("DNDBBS")
 If DND.Path<>Nul Then
    If Right$(DND.Path,1)<>"\" Then
       DND.Path=DND.Path+"\"
    Endif
 Endif
 FileName=DND.Path+"DND"+NodeX+".CFG"
 Close #TempFile1
 Open FileName For Input Shared As #TempFile1
 Line Input #TempFile1,Var$
 For Temp1=1 To MaxConfigArray1
    Input #TempFile1,Var!
 Next
 For Temp1=1 To MaxConfigArray2
    Input #TempFile1,Var%
 Next
 For Temp1=1 To MaxConfigArray3
    Line Input #TempFile1,Var$
    Select Case Temp1
    Case 11
       Data.Path1=Var$
    Case 15
       File.Extension1=Var$
    Case 21
       User.FileName=Var$
    Case 51
       Usage.FileName=Var$
    Case 68
       MessWork.FileName=Var$
    Case 73
       Log.FileName=Var$
    Case 80
       Container.FileName=Var$
    Case 86
       UserInv.FileName=Var$
    End Select
 Next
 Container.FileName=Data.Path1+Container.FileName+File.Extension1
 Log.FileName=Data.Path1+Log.FileName+File.Extension1
 Usage.FileName=Data.Path1+Usage.FileName+File.Extension1
 User.FileName=Data.Path1+User.FileName+File.Extension1
 UserInv.FileName=Data.Path1+UserInv.FileName+File.Extension1
 Exit Sub
Config.Error:
 Error.Number=Err
 Call Get.Error(Error.Number)
 Color 15,0
 Strng2=Lcase$(FileName)
 Mid$(Strng2,1,1)=Ucase$(Mid$(Strng2,1,1))
 Print "Error "+Strng+" reading "+Strng2+". Run Dndcnfg."
 Color 7,0
 Print
 End
End Sub

Sub Get.Error(Var)
 Select Case Var
 Case 53
    Strng = "File not found"
 Case 57
    Strng = "Device I/O error"
 Case 61
    Strng = "Disk full"
 Case 62
    Strng = "Input past eof"
 Case 67
    Strng = "Not enough file handles"
 Case 68
    Strng = "Device unavailable"
 Case 70
    Strng = "Permission denied"
 Case 71
    Strng = "Disk not ready"
 Case Else
    Strng = "Error"+Str$(Var)
 End Select
 Strng=Chr$(34)+Strng+Chr$(34)
End Sub
