/*

$VER: ThorSalv 1.0 by David De Groot 

Salvagescript for damaged THOR-databases


*/



options failat 21
signal on error
signal on break_c
NL = '0a'x


/* we need rexxreqtoolslibrary */

bool = exists('libs:rexxreqtools.library')
 if BOOL = 0 then say "You need RexxReqTools.library"
else call addlib('rexxreqtools.library',0,-30)


/* encourage user to backup his data */

call rtezrequest("You might want to backup your data" nl "before using this program." nl,
                 "Safe is safe!" nl "" nl "Click 'Exit' if you want to backup first.",,
                 "No need to backup|_EXIT","Info",,
                 'rt_reqpos=reqpos_centerscr rtez_flags=ezreqf_centertext')

 if rtresult == 0
   then exit

/* do a flush in case bbsreadlib is active (we don't want that!) 
   and do it twice (there seems to be a problem in flushing this lib) */

address command
'avail flush >nil:'
'wait 1'
'avail flush >nil:'



/* get the thorpath from env: */

if Open('x','env:thor/thorpath',READ)
 then thorpath=readln('x')

else do
 call message("You might want to consult the ThorSalv.guide" nl,
              "on exact documentation and usage of this program." nl "" nl,
              "It seems you don't have Thor installed???")
    exit
    end


/* ask the systemname */

again:
systemname = rtgetstring( "Systemname", "Enter the exact name of the damaged database" nl,
                          "and press <Return>.",,
                          "ThorSalv","<Press Return>",,
                          'rtgs_backfill=true rtgs_flags = gsreqf_centertext|gsreqf_highlighttext rt_reqpos=reqpos_centerscr')

 if rtresult == 0
   then do
       call message("You should press the <Return> button" nl, 
                    "on the keyboard for the full effect." nl,
                    "Please try again.")

       signal again
      end


/* check presence of programs */

if exists(thorpath || 'bin/basemanager')
  then nop
    else do
        call message("Can't find Basemanager!")
         exit;end

if exists(thorpath || 'rexx/bbsread/checkdatabase.br')
  then nop
    else do
        call message("Can't find Checkdatabase.br!")
         exit;end

if exists(thorpath || 'bin/packbbs')
  then nop
    else do
        call message("Can't find Packbbs!")
         exit;end


/* repair the database */

address command

pragma('D',thorpath || 'bin')          
'basemanager validate ' || systemname || ''


pragma('D',thorpath || 'rexx/bbsread') 
'rx checkdatabase.br ' || systemname || ' DELETE'


pragma('D',thorpath || 'bin')          
'packbbs ' || systemname || ''


call rtezrequest('      ' || systemname || '' "recovered.      ",,
                 "Thanks!", "ThorSalv by David De Groot", 'rt_reqpos=reqpos_centerscr')
exit


/* labels */

error:
call message("Apparently something went wrong!" nl,
             "Try the THOR help.guide.")
return

message:
call rtezrequest(arg(1),'Ok','ThorSalv','rt_reqpos=reqpos_centerscr rtez_flags=ezreqf_centertext',)
return


break_c:       

call message("You entered a break.")
return

