#!/usr/bin/perl
use Getopt::Long;
Getopt::Long::Configure ("posix_default", "no_ignore_case");

#ANALIZING INPUT ARGUMENTS
GetOptions("help!" => \$Help,
	"path|p=s" => \$lsbsi_chroot,
);
HELP_MESSAGE() if(defined $Help);

# check user's privileges
$user=`whoami`;
chomp($user);
$group=`groups | grep lsbsi`;
chomp($group);

if (($user ne "root") and (!$group)) {
    print("Only 'lsbsi' group users and root can run this program\n");
    exit;
}

# finding chroot path

if (!($lsbsi_chroot)) {
	if (-d "/opt/lsb/si/chroot") {
		$lsbsi_chroot = "/opt/lsb/si/chroot";
	}
}

if (!(-d "$lsbsi_chroot")) {
    print("Invalid path to chroot environment: '$lsbsi_chroot'\n");
    exit;
}

# determing of the terminal kind
$chroot_path=`whereis chroot | cut -d " " -f2`;
chomp($chroot_path);

#run pre-chroot config
system("$lsbsi_chroot/etc/lsb-si.conf");
#run post-chroot config
system("sudo $chroot_path $lsbsi_chroot /etc/lsb-si-chroot.conf");
# running terminal in chroot
print("Welcome to the LSB Sample Implementation 3.2.\n");
system("sudo $chroot_path $lsbsi_chroot");

sub HELP_MESSAGE()
{
	print STDERR <<"EOM"

$0: Script of launching LSB SI chroot environment on the server side.

Usage: $0 [options]

Options:
--help                         Display this help message.
-p|--path                      Path to LSB SI chroot.

EOM
	;
      exit(1);
}
