#!/usr/local/bin/perl -w
# start - starts Vend in UNIX server mode
$VendRoot = '/ext/minivend';
$PERL = '/usr/local/bin/perl';
$VEND = "$VendRoot/minivend.pl";
$PerlFlags = '-w';
## END CONFIGURABLE VARIABLES

$ENV{'IFS'} = " ";
$ENV{'PATH'} = "/bin:/usr/bin:$VendRoot/bin";

if($0 =~ /_inet$/) {
	$Inet_mode = 1;
}

# Untaint alphanuerics, periods, slashes, and minus signs
# for options
my $tmp;
for(@ARGV) {
    m{([-./\w]+)};
    $tmp = $1;
    $_ = $tmp;
}

sub prompt {
    my($pr) = shift || '? ';
    my($def) = shift;
    my $ans = '';

    print $pr;
    print "[$def] " if $def;
	chop($ans = <STDIN>);
    $ans ? $ans : $def;
}

sub get_id {
	my $file = -f "$VendRoot/error.log"
				? "$VendRoot/error.log" : '';
	return '' unless $file;
	my ($name, $group);

	my($uid) = (stat($file))[4];
	$name = (getpwuid($uid))[0];
	return $name;
}


if ($< == 0) {
	$name = get_id() || 'YOUR_MINIVEND_USER_NAME';
	my $num = $n > 1 ? $n : '';
    print <<EOM;

The MiniVend server should not be run as root. It should run
as a the user name you configured in when you set up the catalogs.
	
If you are starting the server from /etc/rc.local or another
startup file, you can use this:

su $name <<EOF
$VendRoot/bin/start $num
EOF

EOM
	if(-t) {
		exit 2 if $name eq 'YOUR_MINIVEND_USER_NAME';
		my $ask = prompt("Do you want me to start it as '$name' now? ", 'y');
		exit 2 unless $ask =~ /^\s*y/i;
		$Trysu = 1;
	}
	else {
		exit 2 if $name eq 'YOUR_MINIVEND_USER_NAME';
		$Trysu = 1;
	}

}

if (defined $Trysu) {
	exec <<EndOfExec;
su $name <<EOF
$0 $n
EOF
EndOfExec

}

# Check if we are running from CGI, and undef the key value
# 

if (defined $ENV{GATEWAY_INTERFACE}) {
	$ENV{GATEWAY_INTERFACE} = '';
	my $user = $ENV{REMOTE_USER} || 'none';
	my $userdata = qq|'$user:$ENV{REMOTE_ADDR}:$ENV{SCRIPT_NAME}'|;
	#push(@Flags, '-u', $userdata);
}

if (defined $Inet_mode) {
	system $PERL, $PerlFlags, $VEND, '-serve', '-i', @ARGV;
}
else {
	system $PERL, $PerlFlags, $VEND, '-serve', @ARGV;
}


sleep 1;
