Code:
#!/usr/bin/perl
#
# $*BSD port_install,v 1.20 2004/1/9 16:30:26 steve Exp $
#
# Copyright (c) 2004 Steve Milner.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
# PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use Switch;
use DirHandle;
$PORT_DIR = "/usr/ports";
# USAGE ----------------------------
if(scalar(@ARGV) < 1 or $ARGV[0] eq "--help" or $ARGV[0] eq "-h") {
usage();
exit;
}
# End USAGE ------------------------
$count = 0;
foreach (@ARGV) {
switch ($_) {
case "--ports" { $PORT_DIR = $ARGV[$count+1]; }
case "-p" { $PORT_DIR = $ARGV[$count+1]; }
case "--getinfo" { getinfo($ARGV[scalar(@ARGV)-1]) }
case "-g" { getinfo($ARGV[scalar(@ARGV)-1]) }
case "--search" {
my $inst = (in_array("--install",@ARGV) or in_array("-i",@ARGV));
search($ARGV[scalar(@ARGV)-1],$inst,1);
}
case "-s" {
my $inst = (in_array("--install",@ARGV) or in_array("-i",@ARGV));
search($ARGV[scalar(@ARGV)-1],$inst,1);
}
case "--install" {
if(in_array("--search",@ARGV) or in_array("-s",@ARGV)) {
search($ARGV[scalar(@ARGV)-1],1,1);
} else {
install($ARGV[scalar(@ARGV)-1],"");
}
}
case "-i" {
if(in_array("--search",@ARGV) or in_array("-s",@ARGV)) {
search($ARGV[scalar(@ARGV)-1],1,1);
} else {
install($ARGV[scalar(@ARGV)-1],"");
}
}
}
$count++;
}
# SUBROUTINES ----------------------
sub usage() {
print "port_install (C) 2004 Steve Milner\n";
print "USAGE: port_install [switches] port\n";
print "\t--help | -h\tShows this message\n";
print "\t--search | -s\tSearches and returns ports that match\n";
print "\t--install | -i\tInstalls a port\n";
print "\t--quiet | -q\tSuppresses output\n";
print "\t--bgrun | -b\tRuns the processes in the background\n";
print "\t--getinfo | -g\tGet basic information on a package\n";
print "\t--ports | -p\tFull path to the ports directory (use with caution)\n";
print "\nExamples:\n";
print "\tSearch and install package: \tport_install --install --search package\n";
print "\tUse nonstandard ports and install:\tport_install --ports /home/steve/ports --install package\n";
print "\tGet a list of packages like package:\tport_install --search package\n";
print "\tInstall package in the background:\tport_install --quiet --bgrun --install package\n";
}
# Search takes the item to be searched for and
# a binary value where 1 means to install
# the result IF it is the only result
#
# The search looks through INDEX to find
# matching values
sub search($$) {
my $var = $_[0];
my $install = $_[1];
my %matched = find($var);
my $count;
for($count = 0; $matched{"$count-pkg"}; $count++) {
print $matched{"$count-pkg"} . "\n";
}
if($install > 0) {
if($count != 1) {
print "Only one package can be installed at a time using search.\n";
} else {
print "Installing\n";
install($matched{"0-pkg"},$matched{"0-path"});
}
}
exit;
}
sub install($$) {
my $pkg = $_[0];
my $pathto = $_[1];
if(in_array("--quiet",@ARGV) or in_array("-q",@ARGV)) {
if(in_array("--bgrun",@ARGV) or in_array("-b",@ARGV)) {
$extra = " 2&> /dev/null &";
} else {
$extra = " 2&> /dev/null";
}
} elsif(in_array("--bgrun",@ARGV) or in_array("-b",@ARGV)) {
$extra = " &";
}
# We still need to search to get the package information ...
if(!$pathto) {
my %matched = find($pkg);
if(scalar(keys(%matched))/8 > 1) {
print "Multiple packages returned. Pelase use the search function to get the correct name.\n";
exit;
} elsif(scalar(keys(%matched))/8 < 1) {
print "Could not find package: $pkg\n";
exit;
} else {
$pathto = $matched{"0-path"};
}
}
chdir $pathto;
$ok = `make install clean $extra`;
if($ok) {
print "ALL DONE\n";
} else {
print "ERROR! Couldn't install the package!\n";
}
}
sub find($) {
my $var = $_[0];
open(LIST, "$PORT_DIR/INDEX");
my %matched = ();
my $count = 0;
$var =~ s/\-/\\\-/;
$var =~ s/\./\\\./;
$var =~ s/\+/\\\+/;
while(<LIST>) {
my @tmp = split(/\|/, $_);
$pkg = $tmp[0]; # For nice output
$t = $tmp[0];
if($t =~ /$var/i) {
$matched{"$count-pkg"} = $pkg;
$matched{"$count-path"} = $tmp[1];
$matched{"$count-dest"} = $tmp[2];
$matched{"$count-desc"} = $tmp[3];
$matched{"$count-ldesc"} = $tmp[4];
$matched{"$count-maintain"} = $tmp[5];
$matched{"$count-cat"} = $tmp[6];
$matched{"$count-requires"} = $tmp[7];
$count++;
}
}
close (LIST);
return %matched;
}
sub getinfo($) {
my $pkg = $_[0];
my %matched = find($pkg);
if(scalar(keys(%matched))/8 > 1) {
print "Multiple packages returned. Pelase use the search function to get the correct name.\n";
exit;
} elsif(scalar(keys(%matched))/8 < 1) {
print "Could not find package: $pkg\n";
exit;
} else {
print "Package:\t" . $matched{"0-pkg"} . "\n";
print "Description:\t" . $matched{"0-desc"} . "\n";
print "Maintainer:\t" . $matched{"0-maintain"} . "\n";
print "Destination:\t" . $matched{"0-dest"} . "\n";
print "Requires:\t" . $matched{"0-requires"} . "\n" ;
print "Long Description:";
if(open(FH, $matched{"0-ldesc"})) {
print "\n";
while(<FH>) {
print $_;
}
close(FH);
} else {
print " No long description\n";
}
}
}
# Checks array (mainly @ARGV) for specific
# values. Support subroutine
sub in_array($$) {
my $check = shift;
foreach(@_) {
if($check eq $_) {
return 1;
}
}
return 0;
}
# End SUBROUTINES ------------------
Bookmarks