eval '(exit $?0)' && eval '[ -f /usr/local/bin/perl ] && exec /usr/local/bin/perl -S $0 ${1+"$@"}; exec perl -S $0 ${1+"$@"};'
& eval 'if ( -f /usr/local/bin/perl ) exec /usr/local/bin/perl -S $0 $argv:q ; exec perl -S $0 $argv:q'
     if 0;

# @(#)[Hyper-G] [HGS-C] hgrestore	1.00 [server restore] [Gerald Pani]

# 
#<copyright>
# 
# Copyright (c) 1996
# Institute for Information Processing and Computer Supported New Media (IICM),
# Graz University of Technology, Austria.
# 
#</copyright>
  
#<file>
#
# Name:       hgrestore.pl
#
# Purpose:    restore data of the Hyper-G Server 
#
# Created:    18 Jan 96    Gerald Pani
#
# Description:
# 
#</file>
# 
# $Id: hgrestore.pl,v 1.4 1996/01/26 10:44:20 gpani Exp $
#

$pname = &basename( $0);		# name of this program 

# homedir
$homedir = $ENV{'HOME'} || $ENV{'LOGDIR'} || (getpwuid($<))[7] 
    || &localDie( "$pname: Error: You're homeless!");

# restart if .hgrc is't included
&checkHgrc();

# variables and default values

# read resource file
$mail_to		= ''; 	# email to ...
$backup_log	= "$homedir/HG-backup.log";
$restore_log	= "$homedir/HG-restore.log";
$backup_dev	= '';
$backup_command	= '/bin/tar';
$backup_host	= '';
$rsh_command	= '';
$backup_user	= '';
$dd_command	= '/bin/dd';
$HG_backup_command_line	= '';
$HG_restore_command_line	= '';

# parse any switches
$testOnly = 0;
{
    local($arg);
    while ($arg = shift) {
	($arg =~ /^-h$/)    && (&localDie( &Usage())); # help
	($arg =~ /^-help$/) && (&localDie( &Usage())); # help
	($arg =~ /^-test/) && ($testOnly = 1, next); # help
	&localDie( &Usage());
    }
}

&readRC();
$mail_to = '';			# don't send emails

# testing variables

# backup_command executable?
&localDie( "Error: not executable $backup_command\n") unless (-f "$backup_command" && -x _);

if ($backup_host) {
    # rsh_command
    &localDie( "Error: not executable $rsh_command\n") unless (-f "$rsh_command" && -x _);
    local($testCommand) = $backup_user 
	? "$rsh_command $backup_host -l $backup_user /bin/echo test"
	    : "$rsh_command $backup_host /bin/echo test";
    print "testing remote shell command:\n\t$testCommand\n";
    local($testRSHcommand) = `$testCommand`;
    chop($testRSHcommand) if (defined($testRSHcommand));
    &localDie( "Error: can't execute $rsh_command on $backup_host\n") 
	unless (defined($testRSHcommand) && $testRSHcommand eq 'test');
    print "test ok\n\n";

    # dd_command
    $testCommand = $backup_user 
	? "$rsh_command $backup_host -l $backup_user /bin/ls $dd_command"
	    : "$rsh_command $backup_host /bin/ls $dd_command";
    print "testing dd command:\n\t$testCommand\n";
    $testRSHcommand = `$testCommand`;
    chop($testRSHcommand);
    &localDie( "Error: can't find $dd_command on $backup_host\n") unless ($testRSHcommand eq $dd_command);
    print "test ok\n\n";

    # backup_dev
    &localDie( "Error: in hg-backup.rc: backup_dev not specified\n") unless $backup_dev;
    $testCommand = $backup_user 
	? "$rsh_command $backup_host -l $backup_user /bin/ls $backup_dev"
	    : "$rsh_command $backup_host /bin/ls $backup_dev";
    print "testing backup device file:\n\t$testCommand\n";
    $testRSHcommand = `$testCommand`;
    chop($testRSHcommand);
    &localDie( "Error: can't find $backup_dev on $backup_host\n") unless ($testRSHcommand eq $backup_dev);
    print "test ok\n\n";
}
else {
    # backup_dev
    &localDie( "Error: in hg-backup.rc: backup_dev not specified\n") unless $backup_dev;
    &localDie( "Error: can't find $backup_dev on localhost\n") unless (-c "$backup_dev" && -w _);
}

# HG_restore_command_line
&localDie( "Error: in hg-backup.rc: HG_restore_command_line not specified\n") 
    unless $HG_restore_command_line;
&localDie( "Error: in hg-backup.rc: backup_command not specified\n") 
    if (!$backup_command && $HG_restore_command_line =~ /\$backup_command/);
$HG_restore_command_line =~ s/\$backup_command/$backup_command/g;
&localDie( "Error: in hg-backup.rc: rsh_command not specified\n") 
    if (!$rsh_command && $HG_restore_command_line =~ /\$rsh_command/);
$HG_restore_command_line =~ s/\$rsh_command/$rsh_command/g;
&localDie( "Error: in hg-backup.rc: backup_host not specified\n") 
    if (!$backup_host && $HG_restore_command_line =~ /\$backup_host/);
$HG_restore_command_line =~ s/\$backup_host/$backup_host/g;
if ($backup_user) {
    $HG_restore_command_line =~ s/\$backup_user/$backup_user/g;
}
else {
    $HG_restore_command_line =~ s/-l\s+\$backup_user//g;
}
&localDie( "Error: in hg-backup.rc: dd_command not specified\n") 
    if (!$dd_command && $HG_restore_command_line =~ /\$dd_command/);
$HG_restore_command_line =~ s/\$dd_command/$dd_command/g;
&localDie( "Error: in hg-backup.rc: backup_dev not specified\n") 
    if (!$backup_dev && $HG_restore_command_line =~ /\$backup_dev/);
$HG_restore_command_line =~ s/\$backup_dev/$backup_dev/g;

# check sendmail
if (-f '/usr/lib/sendmail' && -x _) {
    $sendmail = '/usr/lib/sendmail';
}
elsif (-f '/etc/sendmail' && -x _) {
    $sendmail = '/etc/sendmail';
}

# check directories
$DIRdcs = $ENV{'DIRdcs'} || &localDie( "$pname: Error: Env.Var. DIRdcs not set !\n");
$DIRfts = $ENV{'DIRfts'} || &localDie( "$pname: Error: Env.Var. DIRfts not set !\n");
if (!($DIRdbs = $ENV{'DIRdbs'})) {
    if (!($DIRdbs = $ENV{'DIRserver'})) {
	&localDie( "$pname: Error: Env.Var. DIRdbs not set\n");
    }
    else {
	print( STDERR "$pname: Warning: Env.Var. DIRdbs not set. Using DIRserver\n");
    }
}

&checkSetDir($DIRdcs) if (-e $DIRdcs);
&checkSetDir($DIRfts) if (-e $DIRfts);
&checkSetDir($DIRdbs) if (-e $DIRdbs);
chdir() || &localDie( "$pname: Error: chdir HOME: $!\n");

# set up HG_restore_error_command_line and HG_restore_command_line
$HG_restore_command_line =~ s/[ \t]+/ /g;

# check host
$DBHost = $ENV{'DBHost'};	# get from environ;
defined($DBHost) 		|| &localDie( "$pname: Error: DBHost not set!\n");
chop($hostname = `hostname`);
$hostname eq $DBHost 		|| &localDie( "$pname: Error: DBHost ($DBHost) not equal to $hostname!\n");

select(STDOUT); $| = 1;		# unbuffered STDOUT

if ($testOnly) {
    print "\n";
    print "              SETTINGS:\n";
    print "              =========\n\n";
#    print " mail_to = $mail_to\n";
    print " restore_log = $restore_log\n";
    print " backup_dev = $backup_dev\n";
    print " backup_command = $backup_command\n";
    print " backup_host = $backup_host\n";
    print " rsh_command = $rsh_command\n";
    print " backup_user = $backup_user\n";
    print " dd_command = $dd_command\n";
    print " HG_restore_command_line = $HG_restore_command_line\n\n";
    exit(0);
} 

print "\n========================================";
while (1) {
    print "\n    hgrestore is going to delete files of the following directories\n";
    print "\n      $DIRdbs\n";
    print "\n      $DIRdcs\n";
    print "\n      $DIRfts\n";
    print "\n    Proceed only if your are sure, that the server is STOPPED and \n";
    print "\n    the backup you want to restore is VALID.\n\n";
    print "\n    PROCEED? (y/n) ";
    local($resp);
    $resp = <STDIN>;
    if ($resp =~ /^[Yy]/) {
	last;
    }
    elsif ($resp =~ /^[Nn]/) {
	exit(1);
    }
}



($ok, $running) = &serverStatus();

if ($ok && $running) {
    &localDie( "Error: server running\n");
}

if (!&cleanDIRdbs()) {
    &localDie( "Error: cleaning $DIRdbs\n");
}

if (!&cleanDIRdcs()) {
    &localDie( "Error: cleaning $DIRdcs\n");
}

if (!&cleanDIRfts()) {
    &localDie( "Error: cleaning $DIRfts\n");
}

if (!&doRestore()) {
     &localDie( "Error: restore failed: error in restore command\n");
}

if (!open(EOB, "< $homedir/.end_of_backup")) {
    &localDie( "Error: can't read $homedir/.end_of_backup\n");
}

$bState = '';
$bDIRdbs = '';
$bDIRdcs = '';
$bDIRfts = '';
$bFTSmark = '';

while (<EOB>) {
    /^State=(.+)$/ && ($bState = $1, next);
    /^DIRdbs=(.+)$/ && ($bDIRdbs = $1, next);
    /^DIRdcs=(.+)$/ && ($bDIRdcs = $1, next);
    /^DIRfts=(.+)$/ && ($bDIRfts = $1, next);
    /^FTSmark=(.+)$/ && ($bFTSmark = $1, next);
}
close(EOB);

if ($bState ne 'running' && $bState ne 'stopped') {
    &localDie( "Error: undefined server backup state $bState\n");
}

if (!$bDIRdbs) {
    &localDie( "Error: undefined DIRdbs at backup time\n");
}
if (!$bDIRdcs) {
    &localDie( "Error: undefined DIRdcs at backup time\n");
}
if (!$bDIRfts) {
    &localDie( "Error: undefined DIRfts at backup time\n");
}
if ($bDIRdbs ne $DIRdbs) {
    print "\nATTENTION: environment variable DIRdbs changed\n";
    print "\n  please move $bDIRdbs to $DIRdbs before starting the server\n";
}
if ($bDIRdcs ne $DIRdcs) {
    print "\nATTENTION: environment variable DIRdcs changed\n";
    print "\n  please move $bDIRdcs to $DIRdcs before starting the server\n";
}
if ($bDIRfts ne $DIRfts) {
    print "\nATTENTION: environment variable DIRfts changed\n";
    print "\n  please move $bDIRfts to $DIRfts before starting the server\n";
}

$ret = system( "rm -rf $bDIRdcs/local/local_.backup");
if ($ret) {
    local($CmdStatus) = $ret >> 8;
    $CmdStatus = unpack('c',pack('c',$CmdStatus));
    &localDie( "Error: cleaning $bDIRdcs/local/local_.backup $CmdStatus\n");
}
if ($bState eq 'running') {
    open (RESTORE, "> $bDIRdbs/RESTORE_MODE") 
	|| &localDie( "Error: can't create $bDIRdbs/RESTORE_MODE: $!\n");
    print RESTORE "RESTORE_MODE\n";
    close(RESTORE);
    open (RESTORE, "> $bDIRfts/RESTORE_MODE") 
	|| &localDie( "Error: can't create $bDIRfts/RESTORE_MODE: $!\n");
    print RESTORE "0x$bFTSmark\n";
    close(RESTORE);
}

print "\nFINISHED\n";
exit(0);


sub dcsFreeze {
    # freeze dcserver
    # Parameters: empty
    # Return values:
    #   on success:
    #     (0, timeOfBackup)
    if (open(CONTR, "| dbcontrol > hgbackup$$.tmp")) {
	print CONTR "U\nzDCSfreeze\nq\n";
	close( CONTR);
	if ($?) {
	    local($CmdStatus) = $? >> 8;
	    $CmdStatus = unpack('c',pack('c',$CmdStatus));
	    print STDERR "dcsFreeze: no connection to server $CmdStatus\n";
	    unlink( "hgbackup$$.tmp");
	    return (1, $CmdStatus);
	}
	local($system) = 0;
	local($ok) = '';
	if (open(CONTR, "< hgbackup$$.tmp")) {
	    while (<CONTR>) {
		/^User: \S+\(system\)$/ && ($system = 1);
		/^dcserver frozen 0x([0-9a-f]+)$/ && ($ok = $1);
	    }
	    close(CONTR);
	    unlink( "hgbackup$$.tmp");
	    if ($system) {
		if ($ok) {
		    return (0, $ok);
		}
		print STDERR "dcsFreeze: no dcserver backup time stamp\n";
		return (5, '');
	    }
	    else {
		print STDERR "dcsFreeze: not system user\n";
		return (2,'');
	    }
	}
	else {
	    print STDERR "dcsFreeze: can't open tmp file\n";
	    return (3,'');
	}
    }
    else {
	print STDERR "dcsFreeze: can't open pipe: $!\n";
	return (4,'');
    }
}


# sub backupFailed {
#     # $HG_backup_command_line
#     local($message) = @_;
#     unlink( "$invalid_backup");
#     if (!open(EOB, "> $invalid_backup")) {
# 	print STDERR "Error: can't create $invalid_backup\n";
# 	return;
#     }
#     print EOB "BACKUP FAILED ", &localTime( time), "\n";
#     print EOB "$message\n" if $message;
#     close(EOB);
#     local($ret) = system( $HG_backup_error_command_line);
#     unlink( "$invalid_backup");
#     return;
# }

# creates a string representing the actual time
sub localTime {
  local( $time) = @_;
  local( $sec, $min, $hour, $mday, $mon, $year) = localtime( $time);
  sprintf("%02d/%02d/%02d %02d:%02d:%02d", $year, $mon + 1, $mday, $hour, $min, $sec);
}



sub doRestore {
    # $HG_restore_command_line
    chdir('/') || &localDie( "$pname: Error: can't change into /!\n");
    print "\nexecuting command:\n   $HG_restore_command_line\n\n";

    local($command) = "($HG_restore_command_line) 2>&1";
    if (!open(LOG, "> $restore_log")) {
	print( STDERR "Error: $!: opening $restore_log\n");
	return 0;
    }
    select(LOG); $| = 1;		# unbuffered 
    select(STDOUT);
    if (!open(BCKP, "$command |")) {
	print( STDERR "Error: $!: executing $command\n");
	print LOG "Error: $!: executing $command\n";
	close(LOG);
	return 0;
    }
    while (<BCKP>) {
	print $_;
	print LOG $_;
    }
    close(BCKP);
    close(LOG);
    return 1;
}

sub staticStatus {
    local($time, $size) = &filetime( "$DIRdbs/db.change.log");
    if (!$time) {
	return (0, '');
    }
    local($sDBS) = "$time $size";
    ($time, $size) = &filetime( "$DIRdbs/.");
    if (!$time) {
	return (0, '');
    }
    $sDBS .= " $time $size";
    return (1, $sDBS);
}

sub filetime {
  local($file) = @_;
  local(@arr) = stat("$file");	# ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev,
			      	# $size, $atime, $mtime, $ctime, $blksize, $blocks)
  local(@result) = ($arr[9], $arr[7]);	# ($mtime, $size, $mode)
  @result;
}

sub basename {
  local($path) = @_;
  
  return '' unless $path =~ m,(^|/)([^/]+)$,;
  return $2;
}

# environment variables from ~/.hgrc should be set. One of them should
 # be 'HGRC'. If 'HGRC' isn't set, then include ~/.hgrc and restart
 # this program.
sub checkHgrc {
  return if defined( $ENV{'HGRC'});

  $ENV{'HGRC'} = '';
  &localDie( "$pname: Error: Couldn't read $homedir/.hgrc") if (! -r "$homedir/.hgrc");
  exec( "/bin/csh -c \'(source $homedir/.hgrc ; $0 @ARGV)\'") ||
      &localDie( "$pname: Error: exec: $!");
}

sub readRC { 
    if (open(RC, "< $homedir/.hg-backup.rc")) {
	local($linenum) = 0;
	local($line) = '';
	local($append) = 0;
	while (<RC>) {
	    $linenum++;
	    if ($append) {
		if (/^(.*)\\$/) {
		    $line .=  " " . $1;
		    next;
		}
		elsif (/^\s*$/) {
		    # empty line
		    $append = 0;
		}
		else {
		    (/^(.*)$/) && ($line .= " " . $1);
		    $append = 0;
		}
	    }
	    else {
		if (/^\s*#/) {
		    # comment line
		    next;
		}
		if (/^\s*$/) {
		    # empty line
		    next;
		}
		if (/^(.*)\\$/) {
		    $line = $1;
		    $append = 1;
		    next;
		}
		else {
		    (/^(.*)$/) && ($line = $1);
		}
	    }

	    ($line =~ /^\s*mail_to\s*=\s*(\S+)\s*$/) && ($mail_to = $1) && next;
	    ($line =~ /^\s*backup_log\s*=\s*(\S+)\s*$/) && ($backup_log = $1) && next;
	    ($line =~ /^\s*restore_log\s*=\s*(\S+)\s*$/) && ($restore_log = $1) && next;
	    ($line =~ /^\s*backup_dev\s*=\s*(\S+)\s*$/) && ($backup_dev = $1) && next;
	    ($line =~ /^\s*backup_command\s*=\s*(\S+)\s*$/) && ($backup_command = $1) && next;
	    ($line =~ /^\s*backup_host\s*=\s*(\S+)\s*$/) && ($backup_host = $1) && next;
	    ($line =~ /^\s*rsh_command\s*=\s*(\S+)\s*$/) && ($rsh_command = $1) && next;
	    ($line =~ /^\s*backup_user\s*=\s*(\S+)\s*$/) && ($backup_user = $1) && next;
	    ($line =~ /^\s*dd_command\s*=\s*(\S+)\s*$/) && ($dd_command = $1) && next;
	    ($line =~ /^\s*HG_backup_command_line\s*=\s*(.+)\s*$/) && ($HG_backup_command_line = $1) && next;
	    ($line =~ /^\s*HG_restore_command_line\s*=\s*(.+)\s*$/) && ($HG_restore_command_line = $1) && next;
	    if ($line =~ /^\s*(\S+)\s*=\s*(.+)\s*$/) {
		&localDie( "$pname: Error: hg-backup.rc ($linenum): unknown variable $1\n");
	    }
	    &localDie( "$pname: Error: hg-backup.rc ($linenum): unexpected line\n\t$line\n");
	}
	close (RC);
    }
    else {
	&localDie( "$pname: Error: can't open $homedir/.hg-backup.rc: $!\n");
    }
    return 0;
}

sub checkSetDir {
  local($dir) = @_;
  -d $dir 		|| &localDie( "$pname: Error: $dir is not a directory!\n");
  chdir($dir) 		|| &localDie( "$pname: Error: can't change into $dir!\n");
  (-o ".") 		|| &localDie( "$pname: Error: current user not owner of $dir!\n");
  return 1;
}
  
sub serverRunning {
    # connect server and retrieve status
    # Parameters: empty
    # Return values:
    #   on success:
    #      (ok = 0, dbUpSince, dcUpSince)
    #   on failure:
    #      (error = 1, CmdStatus, '')
    #      (error = {2,3,4}, '', '')
    if (open(CONTR, "| dbcontrol > hgbackup$$.tmp")) {
	print CONTR "U\nzstat\nzdcstat\nq\n";
	close( CONTR);
	if ($?) {
	    # can't connect server or timeout
	    local($CmdStatus) = $? >> 8;
	    $CmdStatus = unpack('c',pack('c',$CmdStatus));
	    unlink( "hgbackup$$.tmp");
	    return (1, $CmdStatus, '');
	}
	local($system) = 0;
	local($dbUpSince) = '';
	local($dcUpSince) = '';
	if (open(CONTR, "< hgbackup$$.tmp")) {
	    while (<CONTR>) {
		/^User: \S+\(system\)$/ && ($system = 1);
		if (/^UpSince=([0-9\/ :]+) 0x[0-9a-f]+$/) {
		    if ($dbUpSince) {
			$dcUpSince = $1;
		    }
		    else {
			$dbUpSince = $1;
		    }
		}
	    }
	    close(CONTR);
	    unlink( "hgbackup$$.tmp");
	    if ($system) {
		# identified as system, status retrieved
		return (0, $dbUpSince, $dcUpSince);
	    }
	    else {
		# not identified as system
		return (2,'','');
	    }
	}
	else {
	    # can't open tmp-file
	    return (3,'','');
	}
    }
    else {
	# can't open control connection
	return (4,'','');
    }
}

sub serverStatus {
    # retrieve status of server
    # Parameters: empty
    # Return values:
    #   on success:
    #      (ok = 1, 1, dbUpSince, dcUpSince)  server running
    #      (ok = 1, 0, dbStatus, '')  server stopped
    local($ret, $dbUpSince, $dcUpSince) = &serverRunning();
    if ($ret == 0) {
	# server running
	return (1, 1, $dbUpSince, $dcUpSince);
    }
    elsif ($ret == 1) {
	# server stopped
	if ($dbUpSince == 1) {
	    # unclear state 
	    print STDERR "Error: maybe dbcontrol not found\n";
	    return (0, 0, '', '');
	}
	else {
	    print STDERR "serverStatus: no connection to server $dbUpSince\n";
	    # retrieve status of stopped server
	    local( $ok, $sDBS) = &staticStatus();
	    if ($ok) {
		return (1, 0, $sDBS, '');
	    }
	    else {
		return (0, 0, '', '');
	    }
	}
    }
    elsif($ret == 2) {
	print STDERR "Error: not system user\n";
	return (0, 0, '', '');
    }
    elsif($ret == 3) {
	print STDERR "Error: can't open temporary file\n";
	return (0, 0, '', '');
    }
    elsif($ret == 4) {
	print STDERR "Error: open failed\n";
    }
    return (0, 0, '', '');
}

sub mailTo {
    local($subject, $message) = @_;
    return unless $mail_to;
    return unless $sendmail;
    local($cid);
    return if ($cid = fork());	# parent
    return unless defined($cid); # parent return if no fork
    open(MAIL, "| $sendmail -t -oi");
    print MAIL "To: $mail_to\nSubject: $subject\n\n$message";
    close(MAIL);
    exit(0);
}
  
sub localDie {
    local($message) = @_;
    if ($message) {
	&mailTo( "hgrestore error", $message);
	die "$message";
    }
    die;
}

sub Usage {
  return "
Usage: $0 [-h|-help] [-test]

        -h -help        this message
        -test           reads .hg-backup.rc
                        and prints variable settings

   For customizations copy 
     $homedir/samples/hg-backup.rc
   to
     $homedir/.hg-backup.rc
   and change the variables there.
";
}

sub cleanDIRdbs {
    print "removing directory $DIRdbs\n";
    local($ret) = system( "rm -rf $DIRdbs");
    if ($ret) {
	local($CmdStatus) = $ret >> 8;
	$CmdStatus = unpack('c',pack('c',$CmdStatus));
	print STDERR "Error: cleaning $DIRdbs $CmdStatus\n";
	return 0;
    }
    return 1;
}

sub cleanDIRdcs {
    print "removing directory $DIRdcs/local\n";
    local($ret) = system( "rm -rf $DIRdcs/local");
    if ($ret) {
	local($CmdStatus) = $ret >> 8;
	$CmdStatus = unpack('c',pack('c',$CmdStatus));
	print STDERR "Error: cleaning $DIRdcs/local $CmdStatus\n";
	return 0;
    }
    print "removing directory $DIRdcs/cgi\n";
    $ret = system( "rm -rf $DIRdcs/cgi");
    if ($ret) {
	local($CmdStatus) = $ret >> 8;
	$CmdStatus = unpack('c',pack('c',$CmdStatus));
	print STDERR "Error: cleaning $DIRdcs/cgi $CmdStatus\n";
	return 0;
    }
    return 1;
}

sub cleanDIRfts {
    print "removing mirrors in $DIRfts\n";
    local($ret) = system( "rm -f $DIRfts/mirror $DIRfts/mirror.nc $DIRfts/mirror.dirty");
    if ($ret) {
	local($CmdStatus) = $ret >> 8;
	$CmdStatus = unpack('c',pack('c',$CmdStatus));
	print STDERR "Error: cleaning mirrors in $DIRfts $CmdStatus\n";
	return 0;
    }
    print "removing index files in $DIRfts/inv\n";
    $ret = system( "rm -f $DIRfts/inv/*");
    if ($ret) {
	local($CmdStatus) = $ret >> 8;
	$CmdStatus = unpack('c',pack('c',$CmdStatus));
	print STDERR "Error: cleaning $DIRfts/inv $CmdStatus\n";
	return 0;
    }
    return 1;
}
   
