' ctBackup, Copyright (C) 2000-2005 by Jan Vorel

' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' This is a shared modul, class modul, usercontrol or form. It is also
' published under the GNU General Public License.

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues


DECLARE FUNCTION replace$ (xchar1 AS STRING, xchar2 AS STRING, xchar3 AS STRING)
DECLARE SUB EXCOPY (l&, z!, sp!, f$, nowrite!)
DECLARE SUB reboot ()
DECLARE FUNCTION getfileattr% (filename$)
DECLARE FUNCTION setfileattr% (filename$, attribute%)
DECLARE SUB editor (m$(), ex!, ey!, wnx!, wny!, vsx!, vsy!, scr$, einf!, md!, t1!, t0!, c1!, c0!, c2!, pathbackup$)
DECLARE SUB destroywindow (refdata AS STRING)
DECLARE FUNCTION ende! (a$, lx!)
DECLARE SUB docopy (sz&, what&, ges&, z!, sp!, f$)
DECLARE FUNCTION mButtons% ()
DECLARE SUB mouseoff ()
DECLARE SUB mouse (x%, y%, li%)
DECLARE SUB mouseon ()
DECLARE FUNCTION EINGABE$ (zeile!, spalte!, s$, l2!, l!, einf!, x$)
DECLARE FUNCTION dirx$ (f$, a%)
DECLARE FUNCTION createwindow$ (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE)
DECLARE SUB showwindow (refdata AS STRING, modus AS INTEGER)

'$INCLUDE: 'QB.BI'

CONST info1$ = "ctBackup Version 1.21d"
CONST info2$ = "Copyright 2000-2005 by Jan Vorel; See 'ctBackup.txt' for details"
CONST info3$ = "Published under the GNU General Public License; See 'License.txt' for Details."

'CONST maininfo$ = "ctBackup 1.21     -     http://www.ctuser.net/ctbackup     -     info@ctuser.net"
'CONST maininfo$ = "ctBackup 1.21       -       http://www.ctuser.net       -        info@ctuser.net"
CONST maininfo$ = "ctBackup 1.21d       -       http://www.ctuser.net       -       info@ctuser.net"
'CONST maininfo$ = "ctBackup 1.21 [beta 1]      -      http://www.ctuser.net    -    info@ctuser.net"

CONST cmdinfo1$ = "Optional command line parameters:"
CONST cmdinfo2$ = "/T:xx            wait for xx seconds"
CONST cmdinfo3$ = "/A[:description] autocreate backup"
CONST cmdinfo4$ = "/AF[:Filename]   use specific filename for autobackup"
CONST cmdinfo5$ = "/AO[T]           creating autobackup once a day only; AOT to use timer anyway"
CONST cmdinfo6$ = "/ANOTIMER        Don't start timer to enter start setup if autobackup fails"
CONST cmdinfo7$ = "/P:path          specify backup location"
CONST cmdinfo8$ = "/CR              clear screen on startup"
CONST cmdinfo9$ = "/CE              clear screen when finished"
CONST cmdinfoa$ = "/M:xxxx          set maximum number of files (default 256)"
CONST cmdinfob$ = "/MS              skip files when limit is reached"
CONST cmdinfoa1$ = "Description or filename commands:"
CONST cmdinfoa2$ = " $D! = Date (yyyy-mm-dd)     $DD = Day    $DM = Month    $DY = Year"
CONST cmdinfoa3$ = " $T! = Time (hh-mm-ss)       $TH = Hour   $TM = Minute   $TS = Second"
CONST cmdslashb$ = "The '/B:<path>' command line parameter has been removed!"
CONST hitbkey1$ = "Hit "
CONST hitbkey2$ = "Shift+B"
CONST hitbkey3$ = " to enter menu ("
CONST hitbkey4$ = " seconds left)... "
CONST AutoCap1$ = "AutoBackup "
CONST AutoCap2$ = ""
CONST mmenu1$ = "    Create new backup   "
CONST mmenu2$ = "     Restore backup     "
CONST mmenu3$ = "      Delete backup     "
CONST mmenu4$ = "    Change file list    "
CONST mmenu5$ = "          Quit          "
CONST reboot0$ = "   Press Shift+R to reboot  "
CONST reboot1$ = "   Press "
CONST reboot2$ = "Shift+R"
CONST reboot3$ = " to reboot  "
CONST reboot4$ = "Rebooting..."
CONST restore1$ = "No backups found"
CONST restore2$ = "Restore backup?"
CONST restore3$ = "Restoring backup..."
CONST restore4$ = "Error opening backup file"
CONST restore5$ = "Backup file damaged"
CONST restore6$ = "Error restoring backup"
CONST restore7$ = "Error creating file"
CONST restore8$ = "Backup restored"
CONST delete1$ = "Delete backup?"
CONST delete2$ = "Error deleting backup"
CONST ask1$ = "     Yes    "
CONST ask2$ = "     No     "
CONST create1$ = "Enter description"
CONST createsugcap1$ = "Backup "
CONST createsugcap2$ = "   "
CONST createsugcap3$ = ""
CONST create2$ = "Enter filename"
CONST create3$ = "Creating backup..."
CONST create4$ = "Error opening file"
CONST create5$ = "Error reading file"
CONST create6$ = "Error creating backup"
CONST create7$ = "no files"
CONST create8$ = "too many files"
CONST create9$ = "Backup created" ' successfully
CONST createa$ = "AutoBackup skipped (already done today)"
CONST filelist1$ = "Cannot create "
CONST filelist2$ = ""
CONST filelist3$ = "Cannot create "
CONST filelist4$ = " ("
CONST filelist5$ = ")"
CONST filelist6$ = "Cannot create filelist"
CONST editor1$ = " Menu "
CONST editor2$ = "      Save & Exit       "
CONST editor3$ = "         Cancel         "
CONST editor4$ = "     Discard changes    "


'CONST info3$ = "Published under the GNU General Public License; See 'License.txt' for Details."
'CONST cmdinfo1$ = "Command line options:"
'CONST cmdinfo2$ = "/T:xx            wait for xx seconds"
'CONST cmdinfo3$ = "/A[:description] autocreate backup"
'CONST cmdinfo4$ = "/AO[T]           creating autobackup once a day only; AOT to use timer anyway"
'CONST cmdinfo5$ = "/ANOTIMER        Don't start timer to enter start setup if autobackup fails"
'CONST cmdinfo6$ = "/AF[:Filename]   use specific filename instead"
'CONST cmdinfo7$ = "/P:path          specify backup location"
'CONST cmdinfo8$ = "/CR              clear screen on startup"
'CONST cmdinfo9$ = "/CE              clear screen when finished"
'CONST cmdinfoa$ = "/M:xxxx          set maximum number of files (default 256)"
'CONST cmdinfob$ = "/MS              skip files when limit is reached"
'CONST cmdinfoa1$ = "Description or filename commands:"
'CONST cmdinfoa2$ = " $D! = Date (yyyy-mm-dd)     $DD = Day    $DM = Month    $DY = Year"
'CONST cmdinfoa3$ = " $T! = Time (hh-mm-ss)       $TH = Hour   $TM = Minute   $TS = Second"
'CONST createa$ = "AutoBackup already done today"





CONST cfgfilename$ = "CTBACKUP.LST"
CONST datefile$ = "CTBACKUP.DT"

'CONST maxbck = 512
'CONST maxbck = 1024
maxbck = 256
CONST l = 24


maxbck = INSTR(COMMAND$, "/M:")
IF maxbck = 0 THEN
  maxbck = 256
  ELSE
  a$ = MID$(COMMAND$, maxbck + 3)
  maxbck = INT(VAL(LEFT$(a$, INSTR(a$ + " ", " ") - 1)))
  END IF
IF maxbck < 16 THEN maxbck = 16


DIM m$(maxbck)
DIM n&(maxbck)
DIM w$(6)
mb = mButtons%
RESET
'ps = CSRLIN
'IF ps = 1 THEN ps = 3: PRINT : PRINT





COLOR 7, 0
IF INSTR(COMMAND$, "/CR") > 0 THEN CLS
IF CSRLIN = 1 THEN PRINT
IF CSRLIN = 2 THEN PRINT
PRINT info1$
PRINT info2$
PRINT info3$
PRINT


IF INSTR(COMMAND$, "/P:") > 0 THEN
  a$ = MID$(COMMAND$, INSTR(COMMAND$, "/P:") + 3)
  pathbackup$ = RTRIM$(LEFT$(a$, INSTR(a$ + "/", "/") - 1)) + "\"
  END IF

IF NOT (INSTR(COMMAND$ + " ", "/A ") + INSTR(COMMAND$, "/A:")) = 0 THEN
  autobackup = 1
  IF NOT INSTR(COMMAND$, "/AO") = 0 THEN
    ON ERROR GOTO Er10
    RESET
    OPEN pathbackup$ + datefile$ FOR INPUT AS 1
    LINE INPUT #1, a$
    RESET
    IF a$ = DATE$ THEN
      IF NOT INSTR(COMMAND$, "/AOT") = 0 THEN
	autobackup = 3
	ELSE
	PRINT createa$: END
	END IF
      END IF
    END IF
  END IF

rs10:


IF INSTR(COMMAND$, "?") > 0 THEN
  PRINT cmdinfo1$
  PRINT cmdinfo2$
  PRINT cmdinfo3$
  PRINT cmdinfo4$
  PRINT cmdinfo5$
  PRINT cmdinfo6$
  PRINT cmdinfo7$
  PRINT cmdinfo8$
  PRINT cmdinfo9$
  PRINT cmdinfoa$
  PRINT cmdinfob$
  PRINT
  PRINT cmdinfoa1$
  PRINT cmdinfoa2$
  PRINT cmdinfoa3$
  END
  END IF

ps = CSRLIN

IF INSTR(COMMAND$, "/B:") > 0 THEN
  PRINT cmdslashb$
  END
  END IF

ON ERROR GOTO errto1
RESET
OPEN pathbackup$ + cfgfilename FOR INPUT AS 1
RESET
rsrto1:

ON ERROR GOTO 0


IF INSTR(COMMAND$, "/T:") = 0 THEN
  IF autobackup = 2 THEN
    GOTO ext
    ELSEIF autobackup = 1 THEN
    GOTO makxx
    ELSE
    GOTO menx
    END IF
  END IF
a$ = MID$(COMMAND$, INSTR(COMMAND$, "/T:") + 3)
dotim = INT(VAL(LEFT$(a$, INSTR(a$ + " ", " ") - 1)))
IF dotim < 0 OR dotim > 30 THEN dotim = 5


'PRINT
NoFWarn:
t& = TIMER * 100: a$ = "": c$ = ""
DO
  LOCATE , 1: PRINT hitbkey1$;
  COLOR 15, 0: PRINT hitbkey2$;
  a$ = STR$(dotim - CINT(ABS(TIMER * 100 - t&) / 100))
  COLOR 7, 0: PRINT hitbkey3$ + LTRIM$(RTRIM$(a$)) + hitbkey4$;
 
  a$ = INKEY$: IF a$ = "B" OR a$ = "b" THEN autobackup = 0: EXIT DO
  IF a$ = CHR$(27) THEN LOCATE , 1: PRINT SPACE$(80); : LOCATE , 1: END
  IF ABS(TIMER * 100 - t&) > dotim * 100 THEN
    LOCATE , 1: PRINT SPACE$(80); : LOCATE , 1
    IF autobackup = 1 THEN
      GOTO makxx
      ELSEIF autobackup = 3 THEN
      PRINT createa$
      END IF
    END
    END IF
  LOOP

LOCATE , 1: PRINT SPACE$(80); : LOCATE , 1

menx:

w$(0) = createwindow(1, 1, 1, 80)
COLOR 11, 1: LOCATE 1, 1: PRINT maininfo$;

pm = 10
w$(1) = createwindow(pm, 9, pm + 6, 12 + l): m = 1
PALETTE 2, 11
PALETTE 4, 63
PALETTE 5, 8
PALETTE 6, 24

menu:


m$(1) = mmenu1$
m$(2) = mmenu2$
m$(3) = mmenu3$
m$(4) = mmenu4$
m$(5) = mmenu5$

FOR a = 1 TO 5
  LOCATE pm + a, 11: PRINT m$(a)
  NEXT a


IF rebooter = 1 THEN
  LOCATE 17, 9: COLOR 3, 1: PRINT reboot1$;
  COLOR 11: PRINT reboot2$;
  COLOR 3: PRINT reboot3$;
  END IF


nowarn = 0
DO
  COLOR 1, 3: LOCATE pm + m, 11: PRINT m$(m)
  a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
  DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
    a$ = INKEY$: IF mb THEN mouse x%, y%, t%
    LOOP: IF mb THEN mouseoff
  COLOR 11, 1: LOCATE pm + m, 11: PRINT m$(m)
  IF t% THEN
    e = INT(x% / 8) + 1: f = INT(y% / 8) + 1: a$ = "": c$ = "": t% = 1
    IF e > 8 AND e < 37 AND f > pm AND f < pm + 6 THEN IF m = f - pm THEN a$ = CHR$(13) ELSE m = f - pm
    IF e > 8 AND e < 37 AND f = pm + 7 THEN a$ = "R"
    END IF
  IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1)
  IF a$ = "R" THEN EXIT DO
  IF c$ = "G" OR c$ = "I" THEN m = 1
  IF c$ = "O" OR c$ = "Q" THEN m = 5
  IF c$ = "H" THEN m = m - 1: IF m = 0 THEN m = 5
  IF c$ = "P" THEN m = m + 1: IF m = 6 THEN m = 1
  IF a$ = CHR$(27) THEN a$ = CHR$(13): m = 5
  IF a$ = CHR$(13) THEN EXIT DO
  LOOP

COLOR 7, 0



IF m < 5 AND a$ <> "R" THEN
  showwindow w$(1), 0
  FOR a = 1 TO 5
    IF a = m THEN COLOR 5, 6 ELSE COLOR 2, 5
    LOCATE pm + a, 11: PRINT m$(a)
    NEXT a
  IF rebooter = 1 THEN COLOR 3, 5: LOCATE 17, 9: PRINT reboot0$;
  END IF

IF a$ = CHR$(13) AND m = 1 THEN GOTO mak
IF a$ = CHR$(13) AND m = 2 THEN delmodx = 0: GOTO lst
IF a$ = CHR$(13) AND m = 3 THEN delmodx = 1: GOTO lst
IF a$ = CHR$(13) AND m = 4 THEN GOTO editr

ext:
IF autobackup = 0 THEN
  destroywindow w$(1)
  destroywindow w$(0)
  IF rebooter = 1 THEN
    destroywindow w$(5)
    END IF
  COLOR 7, 0
  PALETTE 2, 2
  PALETTE 4, 4
  PALETTE 5, 5
  END IF

LOCATE ps, 1
IF rebooter = 1 AND a$ = "R" THEN
  PRINT reboot4$
  rebooter = 0
  reboot '1
  ELSE
  IF INSTR(COMMAND$, "/CE") > 0 THEN COLOR 7, 0: CLS
  END IF

IF autobackup = 1 THEN
  PRINT LEFT$(log$ + log2$ + SPACE$(80), 80);
  LOCATE , 1: PRINT
  END IF
END

editr:
ON ERROR GOTO errto3
RESET
OPEN pathbackup$ + cfgfilename FOR APPEND AS 1
RESET

COLOR 0, 4
w$(2) = createwindow(4, 41, 23, 76)
editor m$(), 5, 43, 18, 32, 64, 256, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz !#$()_-.\:~'=+*?", 1, 22, 0, 4, 1, 3, 11, pathbackup$
COLOR 7, 0
destroywindow w$(2)
showwindow w$(1), 1
GOTO menu



lst:
n = 0
a$ = dirx(pathbackup$ + "*.BK4", 63)

DO
  a$ = MID$(a$, 10)
  IF LTRIM$(a$) = "" THEN EXIT DO
  IF LEFT$(a$, 1) <> "." THEN
    RESET: OPEN pathbackup$ + a$ FOR BINARY AS 1
    'b$ = SPACE$(43)
    b$ = SPACE$(53)
    GET #1, , b$
    IF LEFT$(b$, 3) = CHR$(26) + CHR$(3) + CHR$(4) THEN
      c$ = UCASE$(MID$(b$, 6) + " - " + a$)
      FOR a = 1 TO n
	IF c$ < UCASE$(m$(a)) THEN
	  n = n + 1
	  FOR c = n TO a + 1 STEP -1
	    m$(c) = m$(c - 1)
	    NEXT
	  m$(a) = MID$(b$, 6) + " - " + a$
	  EXIT FOR
	  END IF
	IF a = n THEN
	  n = n + 1
	  m$(n) = MID$(b$, 6) + " - " + a$
	  END IF
	NEXT
      IF n = 0 THEN n = n + 1: m$(n) = MID$(b$, 6) + " - " + a$
      IF n = maxbck THEN EXIT DO
      END IF
    END IF
  a$ = dirx$("", 0)
  LOOP
RESET

IF n = 0 THEN
  IF nowarn = 0 THEN
    log$ = restore1$
    log2$ = ""
    GOSUB errsummon
    END IF
  showwindow w$(1), 1
  m = 1
  GOTO menu
  END IF
 
IF n > 17 THEN mx = 17 ELSE mx = n
blatt = 0: x = 1

w$(2) = createwindow(4, 41, 23, 76)
LOCATE 22, 41: PRINT CHR$(192) + STRING$(76 - 41 - 1, CHR$(196)) + CHR$(217);
LOCATE 23, 41: PRINT SPACE$(76 - 41 + 1);

DO
  COLOR 11, 1
  FOR a = 1 TO 17
    IF a + blatt > n THEN m$(a + blatt) = ""
    LOCATE 4 + a, 43: PRINT LEFT$(m$(a + blatt) + SPACE$(32), 32);
    NEXT a
  DO
    COLOR 1, 3: LOCATE 4 + x, 43: PRINT LEFT$(m$(x + blatt), 32)
    COLOR 11, 1: LOCATE 23, 43: PRINT MID$(m$(x + blatt) + SPACE$(32), 33, 32)
    a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
    DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
      a$ = INKEY$: IF mb THEN mouse x%, y%, t%
      LOOP: IF mb THEN mouseoff
    LOCATE 4 + x, 43: PRINT LEFT$(m$(x + blatt), 32)
    IF t% THEN
      e = INT(x% / 8) + 1: f = INT(y% / 8) + 1: a$ = "": c$ = "": t% = 1
      'IF e > 8 AND e < 37 AND f > pm AND f < pm + 6 THEN a$ = CHR$(27)
     
     
      IF f = 4 AND e > 40 AND e < 77 THEN c$ = "I"
      IF f > 4 AND f < 23 AND e > 42 AND e < 75 AND f - 4 + blatt <= n AND f - 4 <= mx THEN a = f - 4: IF x = a THEN a$ = CHR$(13) ELSE x = a
      IF f = 23 AND e > 40 AND e < 77 THEN c$ = "Q"
      END IF
    IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1)
    IF c$ = "G" THEN IF x > 1 THEN x = 1 ELSE IF blatt > 0 THEN blatt = 0: EXIT DO
    IF c$ = "I" THEN
      IF blatt > 0 THEN
	IF blatt < mx THEN
	  blatt = 0
	  ELSE
	  blatt = blatt - mx
	  END IF
	EXIT DO
	ELSE
	x = 1
	END IF
      END IF
    IF c$ = "O" THEN IF x < mx THEN x = mx ELSE IF blatt + mx < n THEN blatt = n - mx: EXIT DO
    IF c$ = "Q" THEN
      IF blatt + mx < n THEN
	IF blatt + 2 * mx < n THEN
	  blatt = blatt + mx
	  ELSE
	  blatt = n - mx
	  END IF
	EXIT DO
	ELSE
	x = mx
	END IF
      END IF
    IF c$ = "H" THEN IF x > 1 THEN x = x - 1 ELSE IF blatt > 0 THEN blatt = blatt - 1: EXIT DO
    IF c$ = "P" THEN IF x < mx THEN x = x + 1 ELSE IF blatt + mx < n THEN blatt = blatt + 1: EXIT DO
    IF a$ = CHR$(27) THEN EXIT DO
    IF a$ = CHR$(13) OR c$ = "S" THEN EXIT DO
    LOOP
  IF a$ = CHR$(27) THEN EXIT DO
  IF a$ = CHR$(13) OR c$ = "S" THEN EXIT DO
  LOOP

IF a$ = CHR$(27) THEN
  destroywindow w$(2)
  showwindow w$(1), 1
  GOTO menu
  END IF
IF a$ = CHR$(13) THEN delmod = delmodx
IF c$ = "S" THEN delmod = 1


showwindow w$(2), 0
LOCATE 22, 41: PRINT CHR$(192) + STRING$(76 - 41 - 1, CHR$(196)) + CHR$(217);
LOCATE 23, 41: PRINT SPACE$(76 - 41 + 1);
LOCATE 23, 43: PRINT MID$(m$(x + blatt) + SPACE$(32), 33, 32)

FOR a = 1 TO 17
  IF a = x THEN COLOR 5, 6 ELSE COLOR 2, 5
  'LOCATE 4 + a, 43: PRINT LEFT$(m$(a + blatt), 32);
  LOCATE 4 + a, 43: PRINT LEFT$(m$(a + blatt), 32);
  NEXT a


a$ = createwindow(pm + 1, 45, pm + 4, 48 + l)
'filn$ = MID$(m$(blatt + x), 42)
filn$ = MID$(m$(blatt + x), 52)
IF delmod = 1 THEN
  a$ = LEFT$(delete1$, l)
  ELSE
  a$ = LEFT$(restore2$, l)
  END IF
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
LOCATE pm + 2, 47: PRINT a$
sel = 1
n$(0) = ask1$
n$(1) = ask2$
DO
  FOR a = 0 TO 1
    IF a = sel THEN COLOR 1, 3 ELSE COLOR 11, 1
    LOCATE pm + 3, 47 + a * 12: PRINT n$(a);
    NEXT a
 
  a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
  DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
    a$ = INKEY$: IF mb THEN mouse x%, y%, t%
    LOOP: IF mb THEN mouseoff
  IF t% THEN
    e = INT(x% / 8) + 1: f = INT(y% / 8) + 1: a$ = "": c$ = "": t% = 1
    IF f = 13 AND e > 46 AND e < 59 THEN IF sel = 0 THEN a$ = CHR$(13) ELSE sel = 0
    IF f = 13 AND e > 58 AND e < 71 THEN IF sel = 1 THEN a$ = CHR$(13) ELSE sel = 1
    END IF
  IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1)
  IF c$ = "G" THEN sel = 0
  IF c$ = "O" THEN sel = 1
  IF a$ = CHR$(9) OR c$ = CHR$(15) OR c$ = "H" OR c$ = "K" OR c$ = "M" OR c$ = "P" THEN sel = 1 - sel
  IF a$ = CHR$(27) OR a$ = CHR$(13) THEN EXIT DO
  LOOP
IF a$ = CHR$(13) AND sel = 0 AND delmod = 1 THEN
  log$ = delete2$
  log2$ = pathbackup$ + filn$
  ON ERROR GOTO ErrLog
  RESET: KILL pathbackup$ + filn$
  ON ERROR GOTO 0
  'destroywindow w$(2)
  nowarn = 1
  END IF
IF sel = 1 OR delmod = 1 OR a$ = CHR$(27) THEN
  destroywindow w$(2)
  GOTO lst
  END IF


w$(3) = createwindow(pm + 1, 45, pm + 4, 48 + l)
filn$ = MID$(m$(blatt + x), 52)

a$ = LEFT$(restore3$, l)
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
LOCATE pm + 2, 47: PRINT a$
a$ = LEFT$(filn$, INSTR(filn$, ".") - 1)
LOCATE pm + 3, 47 + INT((l - LEN(a$)) / 2): PRINT a$

ON ERROR GOTO ErrLog
log$ = restore4$
log2$ = pathbackup$ + filn$
RESET: OPEN pathbackup$ + filn$ FOR BINARY AS 1
a$ = STRING$(53, CHR$(0))
GET #1, , a$
fl = ASC(MID$(a$, 4, 1)) * 256 + ASC(MID$(a$, 5, 1))
IF fl > maxbck THEN ERROR 14
b$ = STRING$(7 * fl + 5, CHR$(0))
GET #1, , b$
IF NOT RIGHT$(b$, 5) = "EoTOC" THEN
  log$ = restore5$
  ERROR 5
  END IF

l2& = LOC(1)
FOR a = 1 TO fl
  b = ASC(MID$(b$, a * 7 - 6, 1)) * 256 + ASC(MID$(b$, a * 7 - 5, 1))
  m$(a) = STRING$(b, CHR$(0))
  log$ = restore6$
  log2$ = pathbackup$ + filn$
  GET #1, , m$(a)
  m$(a) = MID$(b$, a * 7 - 4, 1) + m$(a)
  n&(a) = ASC(MID$(b$, a * 7 - 3, 1))
  n&(a) = n&(a) * 256 + ASC(MID$(b$, a * 7 - 2, 1))
  n&(a) = n&(a) * 256 + ASC(MID$(b$, a * 7 - 1, 1))
  n&(a) = n&(a) * 256 + ASC(MID$(b$, a * 7, 1))
  ON ERROR GOTO ater
  log$ = restore7$
  log2$ = MID$(m$(a), 2)
  a$ = RTRIM$(MID$(m$(a), 2))
  c$ = a$
  IF LEN(c$) > l THEN c$ = "..." + RIGHT$(c$, l - 3)
  c$ = SPACE$(INT((l - LEN(c$)) / 2)) + c$
  c$ = LEFT$(c$ + SPACE$(l), l)
  btter = CINT(LOC(1) / LOF(1) * l)
  LOCATE pm + 3, 47: COLOR 1, 3: PRINT LEFT$(c$, btter);
  COLOR 11, 1: PRINT MID$(c$, btter + 1)

ater2:
  log$ = restore6$
  log2$ = pathbackup$ + filn$
  CLOSE #2: OPEN a$ FOR OUTPUT AS 2
  ON ERROR GOTO ErrLog
  CLOSE #2: OPEN a$ FOR BINARY AS 2
  ON ERROR GOTO ErrLog
  EXCOPY n&(a), pm + 3, 47, c$, 0
Aterix3:
  l2& = l2& + n&(a) + b
  a% = setfileattr(RTRIM$(MID$(m$(a), 2)), ASC(LEFT$(m$(a), 1)))
  NEXT a

IF LOC(1) <> LOF(1) OR LOC(1) <> l2& THEN
  log$ = restore5$
  ERROR 5
  END IF
RESET

log$ = restore8$
log2$ = ""
destroywindow w$(2)
GOSUB errsummon
'destroywindow w$(1)

IF NOT rebooter = 1 THEN
  rebooter = 1
  w$(5) = createwindow(17, 9, 17, 36)
  END IF
showwindow w$(1), 1
m = 5: GOTO menu


ater:
CLOSE #2
RESUME AterXX

AterXX:
ON ERROR GOTO Aterix
a% = getfileattr(a$)
IF NOT a% = -1 THEN
  IF INT((a% MOD 32) / 8) = 0 THEN
    a% = setfileattr(a$, 32)
    END IF
  END IF
GOTO ater2

Aterix:
RESUME Aterix2

Aterix2:
ON ERROR GOTO ErrLog
EXCOPY n&(a), pm + 3, 47, c$, 1
GOTO Aterix3

makxx:
a$ = DATE$
'STOP
filn$ = LEFT$("AUTO__" + MID$(a$, 4, 2), 8): bz$ = AutoCap1$ + RIGHT$(a$, 4) + "-" + LEFT$(a$, 2) + "-" + MID$(a$, 4, 2) + AutoCap2$
IF NOT INSTR(COMMAND$, "/A:") = 0 THEN
  DO
    a$ = MID$(COMMAND$, INSTR(COMMAND$, "/A:") + 3)
    b$ = TIME$
    a$ = RTRIM$(LEFT$(a$, INSTR(a$ + "/", "/") - 1))
    a$ = replace$(a$, "T!", LEFT$(b$, 2) + "-" + MID$(b$, 4, 2) + "-" + RIGHT$(b$, 2))
    a$ = replace$(a$, "TH", LEFT$(TIME$, 2))
    a$ = replace$(a$, "TM", MID$(TIME$, 4, 2))
    a$ = replace$(a$, "TS", RIGHT$(TIME$, 2))
    b$ = DATE$
    a$ = replace$(a$, "D!", RIGHT$(DATE$, 4) + MID$(DATE$, 3, 4) + LEFT$(DATE$, 2))
    a$ = replace$(a$, "DD", LEFT$(DATE$, 2))
    a$ = replace$(a$, "DM", MID$(DATE$, 4, 2))
    a$ = replace$(a$, "DY", RIGHT$(DATE$, 4))
 
    FOR a = LEN(a$) - 2 TO 1 STEP -1
      IF INSTR("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*()-_=+[]{},.;:'<>~ " + CHR$(34), MID$(a$, a, 1)) = 0 THEN
	EXIT DO
	END IF
      NEXT a
    IF NOT LTRIM$(RTRIM$(a$)) = "" THEN
      bz$ = UCASE$(LEFT$(a$, 1)) + LCASE$(MID$(a$, 2))
      END IF
    EXIT DO
    LOOP
  END IF

IF NOT INSTR(COMMAND$, "/AF:") = 0 THEN
'  a = INSTR(COMMAND$, "/AF:")
'  IF a = 0 THEN
'    a$ = bz$
'    ELSE
    a$ = MID$(COMMAND$, INSTR(COMMAND$, "/AF:") + 4)
    a$ = RTRIM$(LEFT$(a$, INSTR(a$ + "/", "/") - 1))
    a$ = replace$(a$, "T!", TIME$)
    a$ = replace$(a$, "TH", LEFT$(TIME$, 2))
    a$ = replace$(a$, "TM", MID$(TIME$, 4, 2))
    a$ = replace$(a$, "TS", RIGHT$(TIME$, 2))
    a$ = replace$(a$, "D!", DATE$)
    a$ = replace$(a$, "DD", LEFT$(DATE$, 2))
    a$ = replace$(a$, "DM", MID$(DATE$, 4, 2))
    a$ = replace$(a$, "DY", RIGHT$(DATE$, 4))
'    END IF
  FOR a = LEN(a$) TO 1 STEP -1
    IF INSTR("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!-_#~", MID$(a$, a, 1)) = 0 THEN
      a$ = LEFT$(a$, a - 1) + MID$(a$, a + 1)
      END IF
    NEXT a
  a$ = LEFT$(a$, 8)
  IF NOT a$ = "" THEN filn$ = a$
  END IF


bz$ = LEFT$(bz$ + SPACE$(32), 32)
GOTO makx

mak:
spreturnerror = 1
ON ERROR GOTO ErrLogCrt
log$ = create4$
log2$ = pathbackup$ + cfgfilename
RESET: OPEN pathbackup$ + cfgfilename FOR INPUT AS 1
IF EOF(1) THEN
  log$ = create6$
  log2$ = create7$
  ELSE
  log$ = create5$
  END IF
LINE INPUT #1, a$
IF EOF(1) THEN
  log$ = create6$
  log2$ = create7$
  END IF
LINE INPUT #1, a$
RESET
ON ERROR GOTO 0
spreturnerror = 0

filn$ = "": n = 0: ges& = 0: bz$ = "": st& = 0
w$(2) = createwindow(pm - 3, 45, pm, 48 + l)
a$ = LEFT$(create1$, l)
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
LOCATE pm - 2, 47: PRINT a$

alz$ = ""


IF autobackup = 0 THEN
  alx$ = DATE$
  bz$ = createsugcap1$ + RIGHT$(alx$, 4) + "-" + LEFT$(alx$, 2) + "-" + MID$(alx$, 4, 2) + createsugcap2$ + TIME$ + createsugcap3$
  alz$ = bz$
  bz$ = RTRIM$(EINGABE$(pm - 1, 47, bz$, 24, 32, 1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz !$%&/()#_+-.:,;/" + CHR$(34)))
  IF bz$ = "" THEN bz$ = alz$
  END IF


IF bz$ = CHR$(27) THEN
  destroywindow w$(2)
  showwindow w$(1), 1
  GOTO menu
  END IF
bz$ = LEFT$(bz$ + SPACE$(32), 32)

showwindow w$(2), 0
LOCATE pm - 2, 47: PRINT a$
LOCATE pm - 1, 47: PRINT LEFT$(bz$, l)

w$(3) = createwindow(pm + 2, 45, pm + 5, 48 + l)
a$ = LEFT$(create2$, l)
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
LOCATE pm + 3, 47: PRINT a$

DO WHILE (INSTR(filn$, " ") > 0 OR LEN(RTRIM$(filn$)) = 0)
  IF filn$ = "" THEN
    IF RTRIM$(bz$) = alz$ THEN
      filn$ = RIGHT$(alx$, 4) + LEFT$(alx$, 2) + MID$(alx$, 4, 2)
      ELSE
      filn$ = bz$
      END IF
    END IF
  DO WHILE INSTR(filn$, " "): filn$ = LEFT$(filn$, INSTR(filn$, " ") - 1) + MID$(filn$, INSTR(filn$, " ") + 1): LOOP
  filn$ = EINGABE$(pm + 4, 47 + INT((l - 8) / 2), LEFT$(filn$ + SPACE$(8), 8), 8, 8, 1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz !#$()_-.\:")
  IF filn$ = CHR$(27) THEN
    destroywindow w$(3)
    destroywindow w$(2)
    showwindow w$(1), 1
    GOTO menu
    END IF
  LOOP

filn$ = RTRIM$(filn$)

showwindow w$(3), 0
LOCATE pm + 3, 47: PRINT a$
LOCATE pm + 4, 47 + INT((l - 8) / 2): PRINT LEFT$(filn$, l)


w$(4) = createwindow$(pm + 7, 45, pm + 10, 48 + l)
a$ = LEFT$(create3$, l)
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
LOCATE pm + 8, 47: PRINT a$


makx:
ON ERROR GOTO ErrLogCrt
log$ = create4$
log2$ = pathbackup$ + cfgfilename
RESET: OPEN pathbackup$ + cfgfilename FOR INPUT AS 1
log$ = create5$
IF NOT EOF(1) THEN LINE INPUT #1, a$
DO
  ON ERROR GOTO ErrLogCrt
  IF EOF(1) THEN EXIT DO
  log$ = create5$
  log2$ = pathbackup$ + "CTBACKUPLST.LST"
  LINE INPUT #1, a$
  ON ERROR GOTO pp4
  a$ = LTRIM$(RTRIM$(a$))
  IF MID$(a$, 2, 2) = ":\" THEN
    b$ = a$: c$ = ""
    DO
      IF INSTR(b$, "\") = 0 THEN EXIT DO
      c$ = c$ + LEFT$(b$, INSTR(b$, "\"))
      b$ = MID$(b$, INSTR(b$, "\") + 1)
      LOOP
    a$ = dirx$(a$, 63)
    DO
      a$ = UCASE$(LTRIM$(RTRIM$(MID$(a$, 10))))
      IF a$ = "" THEN EXIT DO
      IF NOT LEFT$(a$, 1) = "." THEN
	CLOSE #2
	OPEN c$ + a$ FOR INPUT AS 2
	CLOSE #2
	OPEN c$ + a$ FOR BINARY ACCESS READ AS 2
	a% = getfileattr(c$ + a$)
	IF NOT a% = -1 AND INT((a% MOD 32) / 8) = 0 THEN
	  n = n + 1
	  m$(n) = CHR$(a%) + c$ + a$
	  n&(n) = LOF(2): ges& = ges& + n&(n)
	  IF n = maxbck THEN EXIT DO
	  END IF
	END IF
pp4x:
      a$ = dirx$("", 0)
      LOOP
    END IF
  IF n = maxbck THEN EXIT DO
  LOOP
'ON ERROR GOTO 0
ON ERROR GOTO ErrLogCrt
IF n = 0 THEN
  'GOTO NoFiLBL
  log$ = create6$
  log2$ = create7$
  'ERROR 5
  IF autobackup = 1 AND INSTR(COMMAND$, "/ANOTIMER") = 0 AND dotim = 0 THEN GOTO NoFWarn2
  m = 4
  'GOTO nofilbl
  ERROR 5
  END IF
IF n = maxbck AND INSTR(COMMAND$, "/MS") = 0 THEN
'  ON ERROR GOTO ErrLogCrt
  log$ = create6$
  log2$ = create8$
  IF autobackup = 1 AND INSTR(COMMAND$, "/ANOTIMER") = 0 AND dotim = 0 THEN GOTO NoFWarn2
  'GOTO nofilbl
  ERROR 5
  END IF

ON ERROR GOTO ErrLogCrt
log$ = create6$
log2$ = pathbackup$ + filn$ + ".BK4"
RESET: OPEN pathbackup$ + filn$ + ".BK4" FOR OUTPUT AS 1
RESET: OPEN pathbackup$ + filn$ + ".BK4" FOR BINARY AS 1

a$ = DATE$
b$ = CHR$(26) + CHR$(3) + CHR$(4) + CHR$(INT(n / 256)) + CHR$(n MOD 256) + bz$ + LEFT$(RIGHT$(a$, 4) + "-" + LEFT$(a$, 2) + "-" + MID$(a$, 4, 2) + " " + TIME$ + SPACE$(16), 16)
a$ = STRING$(n * 7 + 58, CHR$(0))
PUT #1, , a$

FOR a = 1 TO n
  b = LEN(m$(a)) - 1
  b$ = b$ + CHR$(INT(b / 256)) + CHR$(b MOD 256) + LEFT$(m$(a), 1)
  m$(a) = MID$(m$(a), 2)
  CLOSE 2: OPEN m$(a) FOR BINARY ACCESS READ AS 2
  IF autobackup = 0 THEN
    a$ = m$(a)
    IF LEN(a$) > l THEN a$ = "..." + RIGHT$(a$, l - 3)
    a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
    a$ = LEFT$(a$ + SPACE$(l), l)
    b = CINT(st& / ges& * l)
    LOCATE pm + 9, 47: COLOR 1, 3
    PRINT LEFT$(a$, b);
    COLOR 11, 1: PRINT MID$(a$, b + 1);
    COLOR 1, 3
    PUT #1, , m$(a)
    docopy st&, n&(a), ges&, 47, l, a$
    ELSE
    a$ = m$(a)
    IF LEN(a$) > 80 THEN
      a$ = "..." + RIGHT$(a$, 77)
      ELSE
      a$ = LEFT$(a$ + SPACE$(80), 80)
      END IF
    b = CINT(st& / ges& * 80)
    LOCATE , 1: COLOR 0, 7
    PRINT LEFT$(a$, b);
    COLOR 7, 0
    PRINT MID$(a$, b + 1);
    COLOR 0, 7
    PUT #1, , m$(a)
    docopy st&, n&(a), ges&, 1, 80, a$
    END IF

  b = INT(n&(a) / &H1000000)
  n&(a) = n&(a) - b * &H1000000
  b$ = b$ + CHR$(b)
  b = INT(n&(a) / &H10000)
  n&(a) = n&(a) - b * &H10000
  b$ = b$ + CHR$(b)
  b = INT(n&(a) / &H100)
  b$ = b$ + CHR$(b) + CHR$(n&(a) - b * &H100)
  NEXT a

b$ = b$ + "EoTOC"
PUT #1, 1, b$
log$ = create9$
m = 5

nofilbl:
RESET

COLOR 7, 0

IF autobackup = 0 THEN
  destroywindow w$(4)
  destroywindow w$(3)
  destroywindow w$(2)
  'createnew = 1
  GOSUB errsummon
  IF m = 4 THEN
    COLOR 2, 5
    LOCATE pm + 1, 11: PRINT mmenu1$
    COLOR 5, 6
    LOCATE pm + 4, 11: PRINT mmenu4$
    m = 4
    GOTO editr
    END IF
  showwindow w$(1), 1
  GOTO menu
  ELSE
  log$ = log$ + ": "
  IF NOT INSTR(COMMAND$, "/AO") = 0 THEN
    ON ERROR GOTO Er11
    RESET
    OPEN pathbackup$ + datefile$ FOR OUTPUT AS 1
    a$ = DATE$
    PRINT #1, a$
    RESET
    END IF

rs11:
  GOTO ext
  END IF


pp4: RESUME pp4x


rsrto2:
END

errto1:
RESUME rsrto1x

rsrto1x:
ON ERROR GOTO errto2
RESET
OPEN pathbackup$ + cfgfilename FOR APPEND AS 1
RESET
GOTO rsrto1


errto2:
COLOR 7, 0

IF autobackup = 1 AND INSTR(COMMAND$, "/ANOTIMER") = 0 AND dotim = 0 THEN
  log$ = filelist6$ + ": " + cfgfilename + filelist2$
  RESUME ext 'NoFWarn2
  END IF

IF pathbackup$ = "" THEN
  PRINT filelist1$ + cfgfilename + filelist2$
  ELSE
  PRINT filelist3$ + cfgfilename + filelist4$ + pathbackup$ + cfgfilename + filelist5$
  END IF
RESUME rsrto2

errto3:
'IF pathbackup$ = "" THEN
  log$ = filelist6$
  log2$ = pathbackup$ + cfgfilename
'  END IF

GOSUB errsummon
RESUME ext

ErrLogCrt:
m = 1
IF nofilewarn AND NOT INSTR(COMMAND$, "/ANOTIMER") = 0 THEN
  RESUME NoFWarn
  ELSE
  IF spreturnerror = 1 THEN
    GOSUB errsummon
    spreturnerror = 0
    COLOR 2, 5
    LOCATE pm + 1, 11: PRINT mmenu1$
    COLOR 5, 6
    LOCATE pm + 4, 11: PRINT mmenu4$
    m = 4
    RESUME editr
    ELSE
    RESUME nofilbl
    END IF
  END IF

ErrLog:
RESUME ErrLogRs

ErrLogRs:
destroywindow w$(2)
'destroywindow w$(1)
GOSUB errsummon
GOTO lst

errsummon:
a$ = LEFT$(log$, l)
a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
a$ = LEFT$(a$ + SPACE$(l), l)
IF log2$ = "" THEN
  w$(6) = createwindow(pm + 2, 45, pm + 4, 48 + l)
  LOCATE pm + 3, 47: PRINT a$
  ELSE
  w$(6) = createwindow(pm + 1, 45, pm + 4, 48 + l)
  LOCATE pm + 2, 47: PRINT a$
  a$ = log2$
  IF LEN(a$) > l THEN a$ = "..." + RIGHT$(a$, l - 3)
  a$ = SPACE$(INT((l - LEN(a$)) / 2)) + a$
  a$ = LEFT$(a$ + SPACE$(l), l)
  LOCATE pm + 3, 47: PRINT a$
  END IF
a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
  a$ = INKEY$: IF mb THEN mouse x%, y%, t%
  LOOP: IF mb THEN mouseoff
destroywindow w$(6)
RETURN


Er10:
RESUME rs10

Er11:
RESUME rs11


NoFWarn2:
nofilewarn = 1
autobackup = 0
dotim = 5
LOCATE , 1: PRINT SPACE$(80);
LOCATE , 1: PRINT log$ + ": " + log2$
cs = CSRLIN
GOTO NoFWarn

FUNCTION createwindow$ (y1 AS SINGLE, x1 AS SINGLE, y2 AS SINGLE, x2 AS SINGLE)
DIM refdata AS STRING
refdata = CHR$(x1) + CHR$(y1) + CHR$(x2) + CHR$(y2)
va = 2 * (x2 - x1 + y2 - y1)
va1 = x2 - x1
va2 = x2 - x1 + y2 - y1
va3 = 2 * (x2 - x1) + y2 - y1
refdata = refdata + CHR$(va) + CHR$(va1) + CHR$(va2) + CHR$(va3) + CHR$(0)

FOR va = y1 TO y2
  a$ = ""
  FOR vc = x1 TO x2
    refdata = refdata + CHR$(SCREEN(va, vc))
    NEXT
  NEXT

createwindow$ = refdata
showwindow refdata, 1
END FUNCTION

SUB destroywindow (refdata AS STRING)
a$ = MID$(refdata, 10)
x1 = ASC(LEFT$(refdata, 1))
y1 = ASC(MID$(refdata, 2, 1))
x2 = ASC(MID$(refdata, 3, 1))
y2 = ASC(MID$(refdata, 4, 1))
x3 = x2 - x1 + 1
COLOR 7, 0
FOR va = y1 TO y2
  FOR vc = 1 TO x3
    LOCATE va, x1 - 1 + vc
    PRINT MID$(a$, vc, 1)
    NEXT
  a$ = MID$(a$, x3 + 1)
  NEXT
END SUB

FUNCTION dirx$ (f$, a%)
STATIC dta AS STRING * 43
STATIC r AS regtypex

IF NOT f$ = "" THEN
  r.ax = &H1A00
  r.ds = VARSEG(dta)
  r.dx = VARPTR(dta)
  interruptx &H21, r, r
  r.ax = &H4E00
  r.cx = a%
  f$ = f$ + CHR$(0)
  r.ds = VARSEG(f$)
  r.dx = SADD(f$)
  END IF

interruptx &H21, r, r
IF (r.flags AND 1) THEN EXIT FUNCTION
dirx$ = MID$(dta, 22, INSTR(32, dta, CHR$(0)) - 22)
r.ax = &H4F00
END FUNCTION

SUB docopy (sz&, what&, ges&, sp, ll, f$)
f& = INT((&H7FFF& - 6400) / 3)': ll = LEN(f$)
WHILE dn& < what&
  i& = what& - dn&: IF i& > f& THEN i& = f&
  h$ = "": h$ = STRING$(i&, CHR$(0)): GET #2, , h$: PUT #1, , h$
  dn& = dn& + i&: sz& = sz& + i&: b = CINT(sz& / ges& * ll)
  'LOCATE z, sp: COLOR 1, 3
  LOCATE , sp
  PRINT LEFT$(f$, b);
  ': COLOR 11, 1: PRINT MID$(f$, b + 1)
  WEND
h$ = ""
END SUB

SUB editor (m$(), ex, ey, wnx, wny, vsx, vsy, scr$, einf, md, t1, t0, c1, c0, c2, pathbackup$)
DIM n$(3): mb = mButtons%
COLOR 1, 3: LOCATE ex - 1, ey - 2: PRINT editor1$;

RESET: OPEN pathbackup$ + cfgfilename FOR INPUT AS 1
IF NOT EOF(1) THEN LINE INPUT #1, a$
a = 0
DO
  IF EOF(1) THEN EXIT DO
  a = a + 1: LINE INPUT #1, m$(a)
  DO WHILE INSTR(m$(a), CHR$(32)) > 0: m$(a) = LEFT$(m$(a), INSTR(m$(a), CHR$(32)) - 1) + CHR$(md) + MID$(m$(a), INSTR(m$(a), CHR$(32)) + 1): LOOP
  LOOP
FOR a = a + 1 TO vsx
  m$(a) = ""
  NEXT a
RESET

x = 1: y = 1
DO
  COLOR t1, t0
  FOR a = 1 TO x - 1
    LOCATE ex - 1 + a, ey: PRINT LEFT$(MID$(m$(blx + a), bly + 1) + SPACE$(vsy), wny);
    NEXT a
  FOR a = x + 1 TO wnx
    LOCATE ex - 1 + a, ey: PRINT LEFT$(MID$(m$(blx + a), bly + 1) + SPACE$(vsy), wny);
    NEXT a

  DO
    COLOR t1, t0: LOCATE ex - 1 + x, ey: PRINT LEFT$(MID$(m$(blx + x), bly + 1) + SPACE$(vsy), wny);
    COLOR einf * c1 + (1 - einf) * c2, c0: LOCATE ex - 1 + x, ey - 1 + y: PRINT LEFT$(MID$(m$(blx + x), bly + y) + " ", 1);
    a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
    DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
      a$ = INKEY$: IF mb THEN mouse x%, y%, t%
      LOOP: IF mb THEN mouseoff
    COLOR t1, t0: LOCATE ex - 1 + x, ey - 1 + y: PRINT LEFT$(MID$(m$(blx + x), bly + y) + " ", 1);
    IF t% THEN
      e = INT(x% / 8) + 1: f = INT(y% / 8) + 1: a$ = "": c$ = "": t% = 1
      IF f = 4 AND e > 40 AND e < 41 + LEN(editor1$) THEN a$ = CHR$(27)
      IF f = 4 AND e > 46 AND e < 77 THEN c$ = "I"
      IF e = 41 AND f > 4 AND f < 23 THEN c$ = "II"
      IF f > 4 AND f < 23 AND e > 42 AND e < 75 THEN x = f - 4: y = e - 42
      IF f = 23 AND e > 40 AND e < 77 THEN c$ = "Q"
      IF e = 76 AND f > 4 AND f < 23 THEN c$ = "QQ"
      END IF
    IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1)
    
    IF INSTR(scr$, a$) > 0 AND LEN(a$) = 1 THEN
      IF a$ = " " THEN a$ = CHR$(md)
      m$(blx + x) = LEFT$(m$(blx + x) + SPACE$(vsy), vsy)
      m$(blx + x) = LEFT$(m$(blx + x), bly + y - 1) + a$ + MID$(m$(blx + x), bly + y + 1 - einf)
      m$(blx + x) = RTRIM$(m$(blx + x)): c$ = "M"
      DO WHILE INSTR(m$(blx + x), CHR$(32)) > 0: m$(blx + x) = LEFT$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) - 1) + CHR$(md) + MID$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) + 1): LOOP
      END IF

    IF c$ = "S" AND y + bly >= ende(m$(blx + x), vsy) AND blx + x < vsx THEN
      DO
	IF bly + y + LEN(m$(blx + x + 1)) > vsy THEN EXIT DO
	m$(blx + x) = LEFT$(m$(blx + x) + SPACE$(vsy), bly + y - 1) + m$(blx + x + 1)
	m$(blx + x) = RTRIM$(m$(blx + x))
	DO WHILE INSTR(m$(blx + x), CHR$(32)) > 0: m$(blx + x) = LEFT$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) - 1) + CHR$(md) + MID$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) + 1): LOOP
	FOR a = blx + x + 1 TO vsx - 1: m$(a) = m$(a + 1): NEXT a
	m$(vsx) = ""
	EXIT DO
	LOOP
      EXIT DO
      END IF
    
   
    IF a$ = CHR$(13) AND blx + x < vsx AND RTRIM$(m$(vsx)) = "" THEN
      FOR a = vsx TO blx + x + 2 STEP -1
	m$(a) = m$(a - 1)
	NEXT a
      m$(blx + x + 1) = MID$(m$(blx + x), bly + y)
      m$(blx + x) = LEFT$(m$(blx + x), bly + y - 1)
      y = 1: IF x < wnx THEN x = x + 1 ELSE IF blx < vsx - wnx THEN blx = blx + 1
      EXIT DO
      END IF

    IF c$ = "K" OR a$ = CHR$(8) THEN IF y > 1 THEN y = y - 1 ELSE IF bly > 0 THEN bly = bly - 1: EXIT DO
    IF c$ = "M" THEN IF y < wny THEN y = y + 1 ELSE IF bly < vsy - wny THEN bly = bly + 1: EXIT DO
    IF c$ = "H" THEN IF x > 1 THEN x = x - 1 ELSE IF blx > 0 THEN blx = blx - 1: EXIT DO
    IF c$ = "P" THEN IF x < wnx THEN x = x + 1 ELSE IF blx < vsx - wnx THEN blx = blx + 1: EXIT DO

    IF c$ = "I" AND blx > 0 THEN blx = blx - wnx: IF blx < 0 THEN blx = 0: EXIT DO ELSE EXIT DO
    IF c$ = "II" AND bly > 0 THEN bly = bly - wny: IF bly < 0 THEN bly = 0: EXIT DO ELSE EXIT DO
    IF c$ = "Q" AND blx + wnx < vsx THEN blx = blx + wnx: IF blx + wnx > vsx THEN blx = vsx - wnx: EXIT DO ELSE EXIT DO
    IF c$ = "QQ" AND bly + wn < vsy THEN bly = bly + wny: IF bly + wny > vsy THEN bly = vsy - wny: EXIT DO ELSE EXIT DO

    IF a$ = CHR$(8) OR c$ = "S" THEN
      m$(blx + x) = LEFT$(m$(blx + x) + SPACE$(vsy), vsy)
      m$(blx + x) = LEFT$(m$(blx + x), bly + y - 1) + MID$(m$(blx + x), bly + y + 1)
      m$(blx + x) = RTRIM$(m$(blx + x))
      DO WHILE INSTR(m$(blx + x), CHR$(32)) > 0: m$(blx + x) = LEFT$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) - 1) + CHR$(md) + MID$(m$(blx + x), INSTR(m$(blx + x), CHR$(32)) + 1): LOOP
      END IF

    IF c$ = "R" THEN einf = 1 - einf

    IF c$ = "G" AND y + bly = 1 AND x = 1 AND blx > 0 THEN blx = 0: EXIT DO
    IF c$ = "G" AND y + bly = 1 AND x > 1 THEN x = 1
    IF c$ = "G" AND y = 1 AND bly > 0 THEN bly = 0: EXIT DO
    IF c$ = "G" AND y > 1 THEN y = 1
     
    IF c$ = "O" AND bly + y = ende(m$(x + blx), vsx) AND x = wnx AND blx + wnx < vsx THEN blx = vsx - wnx
    IF c$ = "O" AND bly + y = ende(m$(x + blx), vsx) AND x < wnx THEN x = wnx
	
    IF c$ = "O" AND bly + y <> ende(m$(x + blx), vsx) THEN
      y = ende(m$(blx + x), vsx)
      bly = y - INT(wny / 2)
      IF bly + wny > vsy THEN bly = vsy - wny
      IF bly < 0 THEN bly = 0
      y = y - bly
      END IF

    IF c$ = "O" THEN EXIT DO
    IF a$ = CHR$(27) THEN EXIT DO

    LOOP
  IF a$ = CHR$(27) THEN
    pm = 10: m = 2' l = 24
    a$ = createwindow(pm + 1, 45, pm + 5, 48 + l)
    n$(1) = editor2$
    n$(2) = editor3$
    n$(3) = editor4$
    FOR a = 1 TO 3
      LOCATE pm + 1 + a, 47: PRINT n$(a)
      NEXT a
    DO
      COLOR 1, 3: LOCATE pm + 1 + m, 47: PRINT n$(m)
      a$ = "": c$ = "": IF mb THEN DO WHILE t%: mouse x%, y%, t%: LOOP: t% = 0
      DO WHILE (NOT t% AND a$ = ""): IF mb THEN mouseon
	a$ = INKEY$: IF mb THEN mouse x%, y%, t%
	LOOP: IF mb THEN mouseoff
      COLOR 11, 1: LOCATE pm + 1 + m, 47: PRINT n$(m)
      IF t% THEN
	e = INT(x% / 8) + 1: f = INT(y% / 8) + 1: a$ = "": c$ = "": t% = 1
	IF e > 46 AND e < 71 AND f > 11 AND f < 15 THEN IF m = f - 11 THEN a$ = CHR$(13) ELSE m = f - 11
	END IF
      IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1)
      IF c$ = "G" OR c$ = "I" THEN m = 1
      IF c$ = "O" OR c$ = "Q" THEN m = 3
      IF c$ = "H" THEN m = m - 1: IF m = 0 THEN m = 3
      IF c$ = "P" THEN m = m + 1: IF m = 4 THEN m = 1
      IF a$ = CHR$(27) THEN m = 2: EXIT DO
      IF a$ = CHR$(13) THEN EXIT DO
      LOOP
    a$ = CHR$(27)
    END IF
  IF a$ = CHR$(27) AND m = 3 THEN EXIT SUB
  IF a$ = CHR$(27) AND m = 1 THEN
    FOR a = 1 TO vsx: DO WHILE INSTR(m$(a), CHR$(md)) > 0: m$(a) = LEFT$(m$(a), INSTR(m$(a), CHR$(md)) - 1) + CHR$(32) + MID$(m$(a), INSTR(m$(a), CHR$(md)) + 1): LOOP: NEXT a
    a = vsx: DO WHILE (a > 1 AND LTRIM$(a$) = ""): a = a - 1: LOOP
    IF a = 1 AND LTRIM$(m$(1)) = "" THEN a = 0
    RESET: OPEN pathbackup$ + cfgfilename FOR OUTPUT AS 1
    PRINT #1, "[FileList]"
    FOR b = 1 TO a
      IF LTRIM$(m$(b)) <> "" THEN PRINT #1, RTRIM$(m$(b))
      NEXT b
    RESET
    EXIT SUB
    END IF
  LOOP
END SUB

FUNCTION EINGABE$ (zeile, spalte, s$, l2, l, einf, x$)
b$ = LEFT$(s$ + SPACE$(l), l): y = 1: z = 1
IF LTRIM$(b$) = "" THEN z = 0

DO WHILE a$ <> CHR$(13)
  COLOR 7, 0: LOCATE zeile, spalte: PRINT MID$(b$, 1 + blatt, l2)
  COLOR (1 - einf) * 15, 7: LOCATE zeile, spalte - 1 + y: PRINT MID$(b$, y + blatt, 1)
  c$ = "": a$ = ""
  WHILE a$ = "": a$ = INKEY$: WEND
  COLOR 7, 0: LOCATE zeile, spalte - 1 + y: PRINT MID$(b$, y + blatt, 1)
  v = ASC(a$): IF LEN(a$) = 2 THEN c$ = MID$(a$, 2, 1): a$ = "": v = 0
  IF a$ = CHR$(27) THEN b$ = CHR$(27): a$ = CHR$(13)
  IF INSTR(x$, a$) > 0 AND NOT a$ = "" THEN
    IF z = 1 THEN
      b$ = LEFT$(a$ + SPACE$(l), l): c$ = "M"
      ELSE
      b$ = LEFT$(b$, y + blatt - 1) + a$ + MID$(b$, y + blatt + 1 - einf, l - (y + blatt)): c$ = "M"
      END IF
    END IF
  IF c$ = "R" THEN einf = 1 - einf
  IF c$ = "M" THEN IF y + blatt < l THEN IF y < l2 THEN y = y + 1 ELSE blatt = blatt + 1
  IF c$ = "K" OR a$ = CHR$(8) THEN IF y + blatt > 1 THEN IF y > 1 THEN y = y - 1 ELSE blatt = blatt - 1
  IF c$ = "G" THEN IF y = 1 THEN blatt = 0 ELSE y = 1
  IF c$ = "O" THEN y = ende(b$, l): IF y > INT(l2 / 2) THEN blatt = y - INT(l2 / 2): y = y - blatt: IF blatt > l - l2 THEN y = y + (blatt - (l - l2)): blatt = l - l2
  IF c$ = "S" OR a$ = CHR$(8) THEN
    IF z = 1 THEN
      b$ = SPACE$(l)
      ELSE
      b$ = LEFT$(b$, y + blatt - 1) + MID$(b$, y + blatt + 1, l - (y + blatt)) + " "
      END IF
    END IF
  z = 0
  LOOP

b$ = RTRIM$(LTRIM$(b$))
EINGABE$ = b$
END FUNCTION

FUNCTION ende (a$, lx)
a = LEN(RTRIM$(a$)) + 1
IF a > lx THEN a = lx
IF a = 0 THEN a = 1
ende = a
END FUNCTION

SUB EXCOPY (l&, z, sp, f$, nowrite)
f& = INT((&H7FFF& - 6400) / 3): ll = LEN(f$)
DO WHILE dn& < l&
  i& = l& - dn&
  IF i& > f& THEN i& = f&
  h$ = ""
  h$ = SPACE$(i&)
  GET #1, , h$
  IF NOT nowrite = 1 THEN
    PUT #2, , h$
    END IF
  dn& = dn& + i&
  b = CINT(LOC(1) / LOF(1) * ll)
  LOCATE z, sp
  COLOR 1, 3
  PRINT LEFT$(f$, b);
  COLOR 11, 1
  PRINT MID$(f$, b + 1)
  h$ = ""
  LOOP
END SUB

FUNCTION getfileattr% (filename$)
DIM regsx AS regtypex
regsx.ax = &H4300
testname$ = filename$ + CHR$(0)
regsx.ds = VARSEG(testname$)
regsx.dx = SADD(testname$)
interruptx &H21, regsx, regsx
IF regsx.flags AND 1 THEN
  getfileattr% = -1
  ELSE
  getfileattr% = regsx.cx
  END IF
END FUNCTION

FUNCTION mButtons%
DIM r AS RegType
INTERRUPT &H33, r, r
mButtons% = r.ax AND r.bx
END FUNCTION

SUB mouse (x%, y%, li%)
DIM r AS RegType
r.ax = 5
r.bx = t%
INTERRUPT &H33, r, r
x% = r.cx
y% = r.dx
li% = (r.ax AND 1) <> 0
END SUB

SUB mouseoff
DIM r AS RegType
r.ax = 2
INTERRUPT &H33, r, r
END SUB

SUB mouseon
DIM r AS RegType
r.ax = 1
INTERRUPT &H33, r, r
END SUB

SUB reboot
'(WarmBoot%)
DEF SEG = 0
'IF WarmBoot% = 1 THEN
  POKE &H473, &H12 ' Setup for Warm Boot
  POKE &H472, &H34
'  ELSE
'  POKE &H472, &H12 ' Setup for Cold Boot
'  POKE &H473, &H34 ' tho not really nessary
'  END IF
DEF SEG = &HFFFF
CALL Absolute(0)
END SUB

FUNCTION replace$ (xchar1 AS STRING, xchar2 AS STRING, xchar3 AS STRING)
DIM xlong1 AS LONG
DO
  xlong1 = INSTR(xchar1, "$" + xchar2)
  IF xlong1 = 0& THEN EXIT DO
  xchar1 = LEFT$(xchar1, xlong1 - 1&) + xchar3 + MID$(xchar1, xlong1 + LEN(xchar2) + 1&)
  LOOP
DO
  xlong1 = INSTR(xchar1, "%" + xchar2)
  IF xlong1 = 0& THEN EXIT DO
  xchar1 = LEFT$(xchar1, xlong1 - 1&) + xchar3 + MID$(xchar1, xlong1 + LEN(xchar2) + 1&)
  LOOP
replace$ = xchar1
END FUNCTION

FUNCTION setfileattr% (filename$, attribute%)
DIM regsx AS regtypex
testname$ = filename$ + CHR$(0)
regsx.ax = &H4301
regsx.cx = attribute%
regsx.ds = VARSEG(testname$)
regsx.dx = SADD(testname$)
interruptx &H21, regsx, regsx
IF regsx.flags AND 1 THEN
  setfileattr% = -1
  ELSE
  setfileattr% = 0
  END IF
END FUNCTION

SUB showwindow (refdata AS STRING, modus AS INTEGER)
x1 = ASC(LEFT$(refdata, 1))
y1 = ASC(MID$(refdata, 2, 1))
x2 = ASC(MID$(refdata, 3, 1))
y2 = ASC(MID$(refdata, 4, 1))
x3 = x2 - x1 - 1

IF modus = 1 THEN
  COLOR 11, 1
  ELSE
  COLOR 2, 5
  END IF
LOCATE y1, x1: PRINT CHR$(218) + STRING$(x3, CHR$(196)) + CHR$(191)
FOR va = y1 + 1 TO y2 - 1
  LOCATE va, x1: PRINT CHR$(179) + SPACE$(x3) + CHR$(179)
  NEXT
LOCATE y2, x1: PRINT CHR$(192) + STRING$(x3, CHR$(196)) + CHR$(217)
END SUB

