#!/usr/bin/perl 


#***************************************************************************
#                          Virtrun  -  A Perl/Dialog Script
#                             -------------------
#    Started              : January 10 2000
#    copyright            : (C) 1999-2000 by Afra Ahmad
#    email                : afra@prongs.org
#    homepage             : http://prongs.org/virtfs
#    documentation        : http://www.prongs.org/virtfs/docs
# ***************************************************************************

#***************************************************************************
#    
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
# 
# ***************************************************************************


use strict;
my ($program,$args);
my $version="v.0.70.0";
&root_check();
$ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/lib";

if (!$ARGV[0]){
    print "Virtex $version\n";
    print "Usage: virtrun command [arguments]\n";
    exit(0);
}


my $config_file="/etc/virt.conf";
my $virtual_dir=&lookfor("Leading Virtual Directory",1);
my $temp="virt";
my $server_file=&lookfor("Servers File",1);
$server_file="$virtual_dir/$server_file";

my ($server,$ip)=&list_domains;
my $domain = "$virtual_dir/$server";

my ($run,$i);
$run = $ARGV[0];
for ($i=1;$i<@ARGV;$i++){
    $run .= " $ARGV[$i]";
}
print "\nExecuting: $run  within $server:\n\n";
system ("chroot $domain $run");
print "\n\n";
exit(0);

sub list_domains{
    my ($virtual)=@_;

# List all domains. We first check the virtfs configuration files, and
# then the leading virtual directory for (unlisted) domains.   
    
    my (@dirs,$ip,$name,$i,@domains,$list);
    open (FH,"+< $server_file") || die "No such $server_file!";
    
  
    while (<FH>){
	chomp;
	s/#.*//;
	s/^\s+//;
	s/\s+$//;
	next unless length;
	($name,$ip)=split(/\:/, $_,2);
	    $i++;
	$list .= "$name $ip\n" if $name && $ip && (-d "$virtual_dir/$name/etc");
      
	}

    close (FH);

# add directories!
    die "No Virtual Domains Found!\nMake sure configuration is okay...\n" if (!$list);
    
    print $list;
    
    print "\nPlease Choose the domain (ie. domain.com): ";
    my $choice=<STDIN>;chomp($choice);
    open (FH,"+< $server_file") || die "Cannot open: $!\n";
    while (<FH>){
	next unless length;
	($name,$ip)=split(/\s*\:\s*/,$_,2);
	if ($name eq $choice){
	    close (FH);
	    return ($choice,$ip);
	}
    }
    close (FH);
    return ($choice);
}

sub root_check{
my ($test);
die "You are not root!\n" if $< && !$test;
return;
}

sub lookfor{
    my ($variable,$level)=@_;
  open (FH,$config_file) || die "Cannot open $config_file: $!\n";
    
    my ($name,$value);
  
    while (<FH>){
	chomp;
	s/#.*//;
	s/^s+//;
	s/\s+$//;
	next unless length;
	($name,$value)=split(/\s*=\s*/, $_,2);
	
	if ($name eq $variable){
	    close (FH);
	    return $value;

	}
	
    }

    close (FH);
    if ($level == 1){
	die "Couldn't read the configuration file\n$config_file for $variable!\n";
    }
   
}




