; mIRC Backup System
;================================

alias bkdir { return $scriptdir $+ backups\ }
alias bknf {
  var %i = 1
  while (%i) {
    if (!$findfile($bkdir,mirc.bak*,%i,0)) {
      return mirc.bak $+ %i
    }
    inc %i
  }
}
alias -l tini { return $bkdir $+ _temp.ini }
alias -l trem { return $bkdir $+ _rtemp.ini }
alias -l srem { return $bkdir $+ _srem.ini }

alias mdelete {
  if (!$1) { echo -s *** You must specify a backup file number. | echo -s Type /mfiles for the list of them.  }
  else {
    if ($isfile($bkdir $+ mirc.bak $+ $1) == $false) { echo -s *** Could not delte backup number. | echo -s Deletion Failed. | halt }
    else {
      .remove $+(",$bkdir $+ mirc.bak $+ $1,")
      echo -s Backup Removed...
    }
  }
}

alias mfiles {
  echo -s 
  echo -s Listing Backup Files:
  var %i = 1, %c = 0
  while (%i <= $findfile($bkdir,mirc.bak*,0,0)) {
    var %f = $findfile($bkdir,mirc.bak*,%i,0)
    echo -s File $+($chr(35),%i) -> Created: $asctime($file(%f).ctime) ( $+ $duration($calc($ctime - $file(%f).ctime)) $+ )
    inc %i
    inc %c
  }
  if (!%c) { echo -s *** No Backups Were Made } 
  echo -s 
  echo -s DONE
}
alias mbackup {
  echo -s Making backup
  if (!$isdir($bkdir)) { echo -s *** Error: No backup directory can be found. | echo -s Backup Failed. | halt }
  var %n = $bknf
  echo -s *** Saving mIRC Related Ini Files
  .saveini 
  echo -s *** Backing Up...
  .copy $+(",$iif($isfile($1-) == $true,$1-,$mircini),") $+(",$bkdir,\,%n,")
  echo -s $iif($1-,$1-,$nopath($mircini)) file has been backed up ( $+ %n $+ )
}

alias mrestore {
  if (!$1) { echo -s *** You must specify a backup file number. | echo -s Type /mfiles for the list of them.  }
  else {
    if ($isfile($bkdir $+ mirc.bak $+ $1) == $false) { echo -s *** Could not find backup number. | echo -s Restore Failed. | halt }
    else {
      echo -s Exiting mIRC, starting a temp one, and then reloading new mIRC
      set %do_backup $bkdir $+ mirc.bak $+ $1
      set %do_oldini $mircini
      exit
    }
  }
}

on *:LOAD {
  if ($version < 6.03) { echo -s Unable to load script. Mirc Version too low. Needs to be 6.03 or higher | .unload -rs $+(",$script,") } 

  echo -s mIRC Backup System Loaded
  echo -s Checking for valuable files...
  if (!$isdir($bkdir)) { echo -s No Files Found... Script Corrupted, please redownload and follow the readme.txt | .unload -rs $+(",$script,") } 
  else { echo -s mIRC BACKUP SYSTEM LOADED FULLY. READ THE README FILE FOR MORE INFORMATON
  }
}

on *:START {
  unset %do_backup
  unset %do_oldini
}

alias dobackup___ {
  if (%do_backup) {

    echo -s ******************
    echo -s Beginning Restore of mIRC
    echo -s ******************
    echo -s 
    echo -s Checking Temp Ini File...
    if (!$isfile($tini)) {
      echo -s --- Temp INI File NOT FOUND. Creating new one.

      var %p = $+(",$tini,")
      write -c %p
      write %p [rfiles]
      write %p n0= $+ $trem
      write %p n1= $+ $trem
      write %p n2= $+ $srem

      echo -s --- Temp INI File Created.
    }
    else { echo -s --- Temp INI File FOUND! }

    echo -s 
    echo -s Now Checking for Script File.

    if (!$isfile($srem)) {
      echo -s SCRIPT FILE NOT FOUND.
      echo -s BACKUP FAILED
      halt
    }
    else { echo -s --- Script File Found. }
    echo -s 
    echo -s Creating Remote File with Variables

    var %p = $+(",$trem,")
    write -c %p
    write %p [variables]
    write %p n0= $+ $eval(%do_backup,0) %do_backup
    write %p n1= $+ $eval(%do_oldini,0) %do_oldini

    echo -s Remote File Created.
    echo -s 
    echo -s 

    echo -s Running New mIRC with Temp INI and restoring old version.
    unset %do_backup
    unset %do_oldini
    .run $+(",$mircexe,") $+(",$tini,")
  }
}


on *:EXIT {
  dobackup___
}
