#!/usr/bin/perl
#
# /usr/share/diskserver/public/PUB_set_led
#
# This program will use the Perl's default pragmas library to parse the 
# @ARGV with our declared option. See book: "Programming Perl" for details.
#
# Support Options:
#    diskfull [percentage%], (no)getipfail, (no)printporterror, (no)upgrade
#    shutdown, ready, loadok, pushbutton, startup
#    thermal, (no)fixip, copybutton, (no)copyerror, initbeep
#
# The option which head with (no) means that it could release the current
# state.
# For instance, original state: led 1 on
#               diskfull      : led 1 blink
#               nodiskfull    : led 1 on (restore)
# The current state must check the lower priority flag. Not only restore to 
# default, but also the actually state.
#

use Getopt::Long;

###### ------ Definition: ------ ######
$false          = 0;
$true           = 1;
$action         = $true;	# default: add action(true), else del action.
				# ---- Input options declaration ---- #
$LoadOK         = 1;		# prevent now_priority(0) == false(0)
$Ready          = 2;		# so start from 1
$Shutdown       = 3;
$GetIPFail      = 4;
$DiskFull       = 5;
$PrintPortError = 6;
$Upgrade        = 7;
$PushButton     = 8;
$StartUp        = 9;
$Thermal        = 10;
$FixIP          = 11;
$CopyButton     = 12;		# for VCD Server: Copy Button
$CopyError      = 13;		# for VCD Server: Copy Action Fail
$InitBeep       = 14;		# LoadOK need to check this flag to beep!

$DiskPercentage = 0;		# The default percentage of DiskFull option

$LED_STATE_LOGFILE="/var/log/myledstate";   # our current led state log file
$BEEP_STATE_LOGFILE="/var/log/mybeepstate"; # our current buzzer state log file
$SYSLOG_EXE="/usr/share/diskserver/Qset/C_File/PPC_setlog"; # our log program

$_debug_  = $false;		# $true/$false
$_hw_vcd_ = $false;		# $true/$false

###### ------ Parse Input Option ------ ######
$ret = &GetOptions ('diskfull=i', 'getipfail!', 'printporterror!', 'upgrade!', 'pushbutton!', 'shutdown', 'ready', 'loadok', 'startup', 'thermal', 'fixip!', 'copybutton', 'copyerror!', 'initbeep');

###### ------ Debug Implementation: print all data out ------ ######
myprint("RETURN = $ret\n");
myprint("diskfull= $opt_diskfull, getipfail= $opt_getipfail, ");
myprint("printproterrorr= $opt_printporterror, upgrade= $opt_upgrade, ");
myprint("pushbutton= $opt_pushbutton, shutdown= $opt_shutdown, ");
myprint("ready= $opt_ready, loadok= $opt_loadok, startup= $opt_startup, ");
myprint("thermal= $opt_thermal, fixip= $opt_fixip, ");
myprint("copybutton= $opt_copybutton, copyerror= $opt_copyerror\n");
myprint("initbeep= $opt_initbeep\n");

###### ------ SubRoutine ------ ######
# ********** General Subroutine (tools) ********** #
sub myprint {
    if ($_debug_ == $true) {
	my @data = @_;
	print "@data";
    }				# end if: ifdef _DEBUG_, then printf(....)
}

sub read_status {
    my $line = shift @_;
    my @current;
    @current = split /:/, $line;
    myprint("READ=[@current]\n");
    return @current;
}	# Read current status

sub set_current_status {
  SWITCH: {
      if ($opt_diskfull ge 0) {	   # '0' will treat as NULL (!= "" will false)
	  $now_status = $DiskFull;
	  $DiskPercentage = int $opt_diskfull;
	  $action = $true;
	  myprint("DISK=[$DiskPercentage%]\n");
	  last SWITCH;
      } # case "DiskFull"
      if ($opt_getipfail eq 0) {
	  $now_status = $GetIPFail;
	  $action = $false;
	  last SWITCH;
      } # case "NoGetIPFail"
      if ($opt_getipfail eq 1) {
	  $now_status = $GetIPFail;
	  last SWITCH;
      } # case "GetIPFail"
      if ($opt_printporterror eq 0) {
	  $now_status = $PrintPortError;
	  $action = $false;
	  last SWITCH;
      } # case "NoPrintPortError"
      if ($opt_printporterror eq 1) {
	  $now_status = $PrintPortError;
	  last SWITCH;
      } # case "PrintPortError"
      if ($opt_upgrade eq 0) {
	  $now_status = $Upgrade;
	  $action = $false;
	  `beep`;		# beep 1: after upgrade finish (NoUpgrade)
	  last SWITCH;
      } # case "NoUpgrade"
      if ($opt_upgrade eq 1) {
	  $now_status = $Upgrade;
	  last SWITCH;
      } # case "Upgrade"
      if ($opt_pushbutton eq 0) {
	  $now_status = $PushButton;
	  $action = $false;
	  last SWITCH;
      } # case "NoPushButton"
      if ($opt_pushbutton eq 1) {
	  $now_status = $PushButton;
	  last SWITCH;
      } # case "PushButton"
      if ($opt_shutdown eq 1) {
	  $now_status = $Shutdown;
	  last SWITCH;
      } # case "Shutdown"
      if ($opt_ready eq 1) {
	  $now_status = $Ready;
	  last SWITCH;
      } # case "Ready"
      if ($opt_loadok eq 1) {
	  $now_status = $LoadOK;
	  last SWITCH;
      } # case "LoadOK"
      if ($opt_startup eq 1) {
	  $now_status = $StartUp;
	  last SWITCH;
      } # case "StartUp"
      if ($opt_thermal eq 1) {
	  $now_status = $Thermal;
	  last SWITCH;
      } # case "Thermal"
      if ($opt_fixip eq 0) {
	  $now_status = $FixIP;
	  $action = $false;
	  last SWITCH;
      } # case "NoFixIP"
      if ($opt_fixip eq 1) {
	  $now_status = $FixIP;
	  last SWITCH;
      } # case "FixIP"
      if ($opt_copybutton eq 1) {
	  $now_status = $CopyButton;
	  last SWITCH;
      } # case "CopyButton"
      if ($opt_copyerror eq 0) {
	  $now_status = $CopyError;
	  $action = $false;
	  last SWITCH;
      } # case "NoCopyError"
      if ($opt_copyerror eq 1) {
	  $now_status = $CopyError;
	  last SWITCH;
      } # case "CopyError"
      if ($opt_initbeep eq 1) {
	  $now_status = $InitBeep;
	  last SWITCH;
      } # case "InitBeep"
  } ###### End SWITCH ######
}				# set flag base on current status

sub compare_status {
    my $status = shift @_;
    my @list = @_;
    myprint("COMPARE:[$status]&[@list]\n");
    for ($i=0; $i<($#list+1); $i++) {
	if ($status == $list[$i]) {
	    return $true;	# end if: found match, status already exist
	}
    }
    return $false;		# end if: current status is new
}				# search current status

sub add_status {
    my $new;
    my $status = shift @_;	# one input: @_ = $_[0]
    my @current = @_;
    my $addresult;
    myprint("ADD=[$status][@current]\n");
    foreach $new (@current) {
	if ($new != $status) {
	    if ($addresult eq "") {
		$addresult = $new;
		next;
	    }
	    $addresult = join ':', $addresult, $new;
	}			# end if: not match, join back
    }			# end foreach: join the current status
    $addresult = join ':', $addresult, $status;	# add new input
    return $addresult;
}				# add a new status

sub del_status {
    my $new;
    my $status = shift @_;	# one input: @_ = $_[0]
    my @current = @_;
    my $delresult;
    myprint("DEL=[$status][@current]\n");
    foreach $new (@current) {
	if ($new != $status) {
	    if ($delresult eq "") {
		$delresult = $new;
		next;
	    }		# end if: 1st argument
	    $delresult = join ':', $delresult, $new;
	}			# end if: not match, join back
    }			# end foreach: join the current status
    return $delresult;
}				# del a exist status

sub write_status {
    my $status = shift @_;	# one input: @_ = $_[0]
    my $ret;
    $ret = `echo '$status' > $LED_STATE_LOGFILE`;
}

sub read_beep_status {
    my $status = `cat $BEEP_STATE_LOGFILE`;
    chomp($status);
    myprint("BEEP=[$status]\n");
    return $status;
}	# Read current beep status

sub write_beep_status {
    my $status = shift @_;	# one input: @_ = $_[0]
    my $ret;
    $ret = `echo '$status' > $BEEP_STATE_LOGFILE`;
}

sub set_harddisk_led {
    my $input = shift @_;	# one input: @_ = $_[0]
    my $usage = int ($input/10);
  SWITCH: {
      if ($usage == 0) {
	  `hd_led 0 0`;		# less than 10%, all hdleds: OFF
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "0% - 9%"
      if ($usage == 1) {
	  `hd_leds 10 10 20 30 40 50 60 70 80 90 A1`;	# hdled 1: ON
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "10% - 19%"
      if ($usage == 2) {
	  `hd_leds 10 10 20 30 40 50 60 70 80 91 A1`;	# hdled 1, 2: ON
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "20% - 29%"
      if ($usage == 3) {
	  `hd_leds 10 10 20 30 40 50 60 70 81 91 A1`;	# hdled 1, 2, 3: ON
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "30% - 39%"
      if ($usage == 4) {
	  `hd_leds 10 10 20 30 40 50 60 71 81 91 A1`;	# hdled 1, 2, 3, 4: ON
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "40% - 49%"
      if ($usage == 5) {
	  `hd_leds 10 10 20 30 40 50 61 71 81 91 A1`;
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "50% - 59%"
      if ($usage == 6) {
	  `hd_leds 10 10 20 30 40 51 61 71 81 91 A1`;
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "60% - 69%"
      if ($usage == 7) {
	  `hd_leds 10 10 20 30 41 51 61 71 81 91 A1`;
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "70% - 79%"
      if ($usage == 8) {
	  `hd_leds 10 10 20 31 41 51 61 71 81 91 A1`;
	  `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  last SWITCH;
      }	# case "80% - 89%"
      if ($usage == 9) {
	  if ($input >= 98) {
	      `hd_leds 10 120 21 31 41 51 61 71 81 91 A1`; # hdled 10: Blink
	      `do_led 4 2 0`;		# H/W Reserved LED (SD0):    Blink
	  } else {
	      `hd_leds 10 10 21 31 41 51 61 71 81 91 A1`;
	      `do_led 4 0`;		# H/W Reserved LED (SD0):    OFF
	  }			# over 98%
	  last SWITCH;
      }	# case "90% - 99%"
      if ($usage >= 10) {
	      `hd_led 0 1`;	# all hdled: ON
	      `do_led 4 1`;	# H/W Reserved LED (SD0):    ON
	  last SWITCH;
      }	# case "100%"
  } ###### End SWITCH ######
}				# Hard Disk Usage LED Indicator
    

# ********** Main Subroutine (do the action!) ********** #
# the following actions are base on the North Start project
sub do_action {
    my $status = shift @_;	# one input: @_ = $_[0]
    my @current = @_;		# current status array
    my $retcode;
    my @new_status;
    myprint("DO - STATUS[$status], CURRENT[@current]\n");
  SWITCH: {
				# LED 1: READY (SYSTEM)
      if ($status == $StartUp) {
	  myprint("DO - StartUp\n");
	  `do_led 0 0`;		# all leds OFF: initialize
	  `hd_led 0 0`;		# all hdleds OFF: initialize
	  `do_led 1 2 0`;	# led 1: Blink
	  write_status($StartUp);	# save current status as the only one
	  last SWITCH;
      }	# case "StartUp"

      if ($status == InitBeep) {
	  myprint("DO - InitBeep\n");   # FIXME: clear the beep flat while the
	  write_beep_status($InitBeep); # fs is 'rw'. (LoadOK will use this!!)
      }	# case "InitBeep"

      if ($status == $Thermal) {
	  myprint("DO - Thermal\n");
	  write_beep_status($Thermal);
	  `beep 5 12 15`;       # beep 5/every 15 sec/last 3 min
	  `$SYSLOG_EXE "SYSTEM" "Thermal temperature is too high"`;
	  sleep 180;		# delay for beep finish (3 minute=3*60)
	  `killall shutdown`;   # kill all shutdown process first
	  `shutdown -h now`;	# shutdown the system
	  `do_led 1 2 0`;	# led 1: Blink
	  last SWITCH;
      }	# case "StartUp"

      if ($status == $Shutdown) {
	  myprint("DO - Shutdown\n");
	  `do_led 1 2 0`;	# led 1: Blink
	  write_status($Shutdown);	# clear all status
	  `$SYSLOG_EXE "SYSTEM" "Shutdown"`;
	  last SWITCH;
      }	# case "Shutdown"

      if ($status == $Ready) {
	  myprint("DO - Ready\n");
	  $retcode = del_status($LoadOK, @current);
	  @new_status = read_status($retcode);
	  $retcode = add_status($Ready, @new_status);
	  write_status($retcode);
				# check Upgrade
	  $retcode = compare_status($Upgrade, @current);
	  if ($retcode == $true) {
	      myprint("Upgrade active, Ready do nothing!\n");
	  } else {
	      `do_led 1 1`;		# led 1: ON
	  }
	  last SWITCH;
      }	# case "Ready"

      if ($status == $LoadOK) {
	  myprint("DO - LoadOK\n");
				# del StartUp status
	  $retcode = del_status($StartUp, @current);
	  @new_status = read_status($retcode);
				# del LoadOk, add Ready status
	  $retcode = add_status($Ready, @new_status);
	  write_status($retcode);
	  `do_led 1 1`;		# led 1 ON
	  $retcode = read_beep_status();
	  if ($retcode == $InitBeep) {
	      `beep 1`;		# beep 1 sound
	  }			# end if: if system is beeping by other task,
	  last SWITCH;		#         don't stop it here. (FIXBUG!!)
      } # case "LoadOK"

				# LED 2: ERROR (STATUS)
      if ($status == $PrintPortError) {
	  if ($action == $true) {
	      myprint("DO - H/W Error\n");
				# PrintPortError
	      $retcode = add_status($PrintPortError, @current);
	      write_status($retcode);
	      @new_status = read_status($retcode);
	      $retcode = compare_status($Upgrade, @new_status);
	      if ($retcode == $true) {
		  myprint("Upgrade active, H/W Error do nothing!\n");
	      } else {
		  `do_led 2 1`;	# led 2: ON
	      }			# end if-else: check Upgrade
	  } else {
	      myprint("DO - No H/W Error\n");
				# NoPrintPortError
	      $retcode = del_status($PrintPortError, @current);
	      write_status($retcode);
	      @new_status = read_status($retcode);
	      $retcode = compare_status($Upgrade, @new_status);
	      if ($retcode == $true) {
		  myprint("Upgrade active, No H/W Error do nothing!\n");
	      } else {
		  `do_led 2 0`; # led 2: OFF
	      }			# end if-else: check Upgrade
	  }			# end if-else: (No)PrintPortError
	  last SWITCH;
      } # case "PrintPortError"

      if ($status == $Upgrade) {
				# check Shutdown
	  $retcode = compare_status($Shutdown, @current);
	  if ($retcode == $true) {
	      myprint("DO - (No)Upgrade\n");
	      myprint("Shutdown active, (No)Upgrade do nothing!\n");
	  } else {
	      if ($action == $true) {
		  myprint("DO - Upgrade\n");
				# Upgrade
		  $retcode = add_status($Upgrade, @current);
		  write_status($retcode);
		  `do_leds 2 11 20`;   # init: LED 1 - ON/LED 2 - OFF
		  `do_leds 2 120 220`; # blink LED 1, 2
	      } else {
		  myprint("DO - NoUpgrade\n");
				# NoUpgrade
		  $retcode = del_status($Upgrade, @current);
		  write_status($retcode);
				# led 1: back to Ready
				# led 2: check H/W Error
		  @new_status = read_status($retcode);
		  $retcode = compare_status($PrintPortError, @new_status);
		  if ($retcode == $true) {
		      myprint("H/W Error active, NoUpgrade switched!\n");
		      `do_leds 2 11 21`;    # led 1: ON, led 2: ON
		  } else {
		      `do_leds 2 11 20`;    # led 1: ON, led 2: OFF
		  }		# end if-else: check H/W Error
	      }			# end if-else: check No/Upgrade
	  }			# end if-else: check Shutdown
	  last SWITCH;
      }	# case "Uprgade"

				# LED 3: IP
      if ($status == $FixIP) {
	  if ($action == $true) {
	      myprint("DO - FixIP\n");
				# FixIP
	      $retcode = read_beep_status();
	      if ($retcode == $GetIPFail) {
		  `beep 0`;
	      }			# end if: silent GetIPFail
	      $retcode = add_status($FixIP, @current);
	      write_status($retcode);
	      if ($_hw_vcd_ != $true) {
		  `do_led 3 0`;	# led 3: OFF - static ip
	      }			# end if: VCD set the IP LED in other purpose
	      @new_status = read_status($retcode);
	      $retcode = compare_status($GetIPFail, @new_status);
	      if ($retcode == $true) {
 		  myprint("GetIPFail active: FixIP remove it\n");
		  $retcode = del_status($GetIPFail, @new_status);
		  write_status($retcode);
	      }		# end if-else: check GetIPFail
	  } else {
	      myprint("DO - NoFixIP\n");
				# NoFixIP
	      $retcode = read_beep_status();
	      if ($retcode == $GetIPFail) {
		  `beep 0`;
	      }			# end if: silent GetIPFail
	      $retcode = del_status($FixIP, @current);
	      write_status($retcode);
	      if ($_hw_vcd_ != $true) {
		  `do_led 3 1`;	# led 3: ON - dhcp client
	      }			# end if: VCD set the IP LED in other purpose
	      @new_status = read_status($retcode);
	      $retcode = compare_status($GetIPFail, @new_status);
	      if ($retcode == $true) {
 		  myprint("GetIPFail active: NoFixIP remove it\n");
		  $retcode = del_status($GetIPFail, @new_status);
		  write_status($retcode);
	      }			# end if-else: check GetIPFail
	  }			# end if-else: (No)FixIP
	  last SWITCH;
      } # case "(No)FixIP"

      if ($status == $GetIPFail) {
	  if ($action == $true) {
	      myprint("DO - GetIPFail\n");
				# GetIPFail
	      $retcode = del_status($FixIP, @current); # remove FixIP
	      write_status($retcode);
	      @new_status = read_status($retcode);
	      $retcode = add_status($GetIPFail, @new_status);
	      write_status($retcode);
	      if ($_hw_vcd_ != $true) {
		  `do_led 3 2 0`; # led 3: Blink
	      }			# end if: VCD set the IP LED in other purpose
	      write_beep_status($GetIPFail);
	      `beep 2 12 5`;  # beep 2/every 5 sec/last 1 min
	  } else {
	      myprint("DO - NoGetIPFail\n");
				# NoGetIPFail
	      $retcode = read_beep_status();
	      if ($retcode == $GetIPFail) {
		  `beep 0`;
	      }			# end if: silent GetIPFail
	      $retcode = compare_status($FixIP, @current); # check FixIP
	      if ($retcode == $true) {
		  # shouldn't come here! GetIPFail remove the FixIP flag
 		  myprint("FixIP    active: NoGetIPFail - LED 3 OFF\n");
		  if ($_hw_vcd_ != $true) {
		      `do_led 3 0`;	# led 3: OFF
		  }		# end if: VCD set the IP LED in other purpose
	      } else {
		  myprint("FixIP de-active: NoGetIPFail - LED 3 ON\n");
		  if ($_hw_vcd_ != $true) {
		      `do_led 3 1`;	# led 3: ON
		  }		# end if: VCD set the IP LED in other purpose
	      }			# end if-else: check FixIP
	      $retcode = del_status($GetIPFail, @current);
	      write_status($retcode);
	  }			# end if-else: (No)GetIPFail
	  last SWITCH;
      } # case "(No)GetIPFail"

      if ($status == $CopyError) {
	  if ($action == $true) {
	      myprint("DO - CopyError\n");
				# CopyError
	      $retcode = add_status($CopyError, @current);
	      write_status($retcode);
	      if ($_hw_vcd_ == $true) {
		  `do_led 3 1`;	# led 3: ON - copy action fail
	      }			# end if: VCD set the IP LED here!
	  } else {
	      myprint("DO - NoCopyError\n");
				# NoCopyError
	      $retcode = del_status($CopyError, @current);
	      write_status($retcode);
	      if ($_hw_vcd_ == $true) {
		  `do_led 3 0`;	# led 3: OFF - abort copy action
	      }			# end if: VCD set the IP LED here!
	  }			# end if-else: (No)CopyError
	  last SWITCH;
      } # case "(No)CopyError"

				# HD LED: DISK
      if ($status == $DiskFull) {
	  myprint("DO - DiskFull [$DiskPercentage%]\n");
	  if ($DiskPercentage > 0) {
	      if ($DiskPercentage < 98) {
		  $retcode = read_beep_status();
		  if ($retcode == $DiskFull) {
		      `beep 0`;
		  }		# end if: silent DiskFull
		  $retcode = del_status($DiskFull, @current);
		  write_status($retcode);
		  myprint("Disk Percentage less than 98%\n");
		  set_harddisk_led($DiskPercentage);
	      } else {
				# check/set beep indicator flag
		  $retcode = compare_status($DiskFull, @current);
		  if ($retcode == $false) {
		      write_beep_status($DiskFull);
		      `beep 3 12 15`; # beep 3/every 15 sec/last 3 min
		  }		# end if: first time DiskFull. BEEP !!
		  $retcode = add_status($DiskFull, @current);
		  write_status($retcode);
		  if ($DiskPercentage == 100) {
		      myprint("Disk Percentage equal to 100%\n");
		  } else {
		      myprint("Disk Percentage greater than 98%\n");
		  }		# end if-else: 98% - 100%
		  set_harddisk_led($DiskPercentage);
	      }			# end if-else:  0% -  98%
	  }			# end if: Percentage Valid
	  last SWITCH;
      } # case "DiskFull"

				# OTHERS: PushButton
      if ($status == $PushButton) {
	  myprint("DO - PushButton\n");
	  `$SYSLOG_EXE "SYSTEM" "Clear password and reset to default IP address"`;
	  $retcode = compare_status($GetIPFail, @current);
	  if ($retcode == $true) {
	      $retcode = del_status($GetIPFail, @current);
	      write_status($retcode);
	  }			# end if: clear GetIPFail flag
	  `do_led 3 0`;		# led 3: OFF (always set as FixIP)
	  last SWITCH;
      } # case "PushButton"

				# OTHERS: CopyButton
      if ($status == $CopyButton) {
	  myprint("DO - CopyButton\n");
				# do nothing!
	  last SWITCH;
      } # case "CopyButton"
  } ###### End SWITCH ######
}

###### ------ Main Prorgam ------ ######
if ($ret != 0) {
				# LED state
    if (!(-e "$LED_STATE_LOGFILE")) {
	$ret = `echo '' > $LED_STATE_LOGFILE`;
    }				# end if: set default if file doesn't exist
    $nowline = `cat $LED_STATE_LOGFILE`;
    chomp($nowline);
    @current_status = read_status($nowline);		# set array OK
				# BUZZER state
    if (!(-e "$BEEP_STATE_LOGFILE")) {
	$ret = `echo '' > $BEEP_STATE_LOGFILE`;
    }				# end if: set default if file doesn't exist

    set_current_status();	# set flag of $current_status/$action
    myprint("ACT=$action\n");
				# check if current status already exist!
    $result = compare_status($now_status, @current_status);
    if (($result == $true)&&($action == $true)) {
				# except: StartUp/Shutdown/Ready/DiskFull/FixIP
	if ($now_status != $StartUp) {
	    if ($now_status != $Shutdown) {
		if ($now_status != $Ready) {
		    if ($now_status != $DiskFull) {
			if ($now_status != $FixIP) {
			    myprint("Input [$now_status] already exist. Exit Now!!\n");
			    exit;
			}	# end if: FixIP
		    }		# end if: DiskFull
		}		# end if: Ready
	    }			# end if: Shutdown
	}			# end if: StartUp
    }				# end if: current status
				# use global variable $current_status/$action
    myprint("IN=[$now_status], CURRENT=[$current_status]\n");
    do_action($now_status, @current_status);
    exit;
}				# end if: we only deal with 1 option at a time
