#!/usr/bin/perl -w -I/usr/sausalito/perl
# Author: Joshua Uziel
# Configure UPS settings via the LCD... and set them through CCE
# $Id: 10configure_ups,v 1.5 2001/11/13 07:45:08 uzi Exp $

use CCE;
use POSIX;
use I18n;
use Locale::gettext;

if ($ARGV[0] eq '-s') {
	$option = '-s';
} else {
	$option = '';
}

# LCD helper programs
$LCD_YESNO = "/sbin/lcd-yesno";
$LCD_GETIP = "/sbin/lcd-getip";
$LCD_WRITE = "/sbin/lcd-write";

# Connect to CCE
$cce = new CCE;
$cce->connectuds();

$i18n = new I18n;
$i18n->setLocale(I18n::i18n_getSystemLocale($cce));

# Get the current master's IP
@oids = $cce->find('System');
my ($ok, $upsoid) = $cce->get($oids[0], 'UPS');
if (not $ok) {
	$cce->bye('FAIL');
	exit(1);
}

# Get our master's IP address, or 0.0.0.0 if not set.
$masterip = $upsoid->{masterip} ? $upsoid->{masterip} : '0.0.0.0';

START:
if (getYN('lcdenable1', 'lcdenable2')) {	# enable

	# Get if it's master or slave
	my $result = getYN('lcdstate1', 'lcdstate2');

	if ($result) {	# master
		$state = 'master';
	} else {		# slave
		$state = 'slave';
		$masterip = getIP('lcdmasterip', $masterip);
	}
} else {			# disable
	$state = '';
}

# See if they're sure about it... if not, let them start over.
if (!getYN('lcdsavecancel1', 'lcdsavecancel2')) {
	# Reset LCD back to prompt
	system('/sbin/lcdstop');
	system('/etc/rc.d/init.d/lcd-showip');

	$cce->bye('SUCCESS');
	exit(0);
} else {
        my $top = $i18n->get('lcdsaving1', {}, 'base-ups');
        my $bottom = $i18n->get('lcdsaving2', {}, 'base-ups');
        `$LCD_WRITE $option \"$top\" \"$bottom\"`;
	sleep 1;
}

# Then we set everything.  The nice thing is that
# setting it in CCE will call the handler for us.
if ($state eq 'slave') {	# Only set "masterip" if we're a slave
	$ret = $cce->set( $oids[0], 'UPS',
			{ state => $state, masterip => $masterip }
			) if (@oids > 0);
} else {
	$ret = $cce->set( $oids[0], 'UPS',
			{ state => $state }
			) if (@oids > 0);
}

if ($ret) {
        my $top = $i18n->get('lcdbadset1', {}, 'base-ups');
        my $bottom = $i18n->get('lcdbadset2', {}, 'base-ups');
        `$LCD_WRITE $option \"$top\" \"$bottom\"`;
	sleep 3;

	if (getYN('lcdtryagain')) {
		goto START;
	}
}

# Reset LCD back to prompt
system('/sbin/lcdstop');
system('/etc/rc.d/init.d/lcd-showip');

$cce->bye('SUCCESS');
exit(0);


#returns 1 if yes, 0 if no.
# if the second argument is '' or nonexistant, default to Yes/No
sub getYN{
        my($tag1, $tag2) = @_;

        my $title = $i18n->get($tag1, {}, 'base-ups');

	$tag2 = ($tag2 eq '') ? 'lcdyesno' : $tag2;
	my $yesno = $i18n->get($tag2, {}, 'base-ups');

        system("$LCD_YESNO $option -1 \"$title\" -2 \"$yesno\"");

        return !( ($? >> 8) - 1 ); 
        # $? >> 8 = real exit code of program
        # yes = 1, no = 2
        # w/ -1, yes=0, no=1
        # w/ !, yes = 1, no = 0

}

#returns IP address
sub getIP{
        my($tag, $ip)=@_;

	my $title = $i18n->get($tag, {}, 'base-ups');
	$ip = `$LCD_GETIP $option -1 \"$title\" -i \"$ip\"`;

        return $ip;
}

