#!/usr/bin/perl # # PKGSRC database implementation. # $pkgsrc_base="/usr/pkgsrc"; $dbdir="/var/db/pkg"; $dbfile="pkgsrc.db"; if ($#ARGV == -1) { print "PKGSRC database implementation. -pancake\@phreaker.net-\n"; print "Usage: \"pkg_db\" [options] [args]\n"; print " [options]\n"; print " gendb - Generates a pkgsrc database on stdout.\n"; print " getdb - Download the pkgsrc database.\n"; print " show - Show information of certain packet.\n"; print " search - Search a package.\n"; print " install - Download deps and install.\n"; print " deinstall - Deinstall target packages. (TODO)\n"; print " update - List all not updated packages.\n"; print "**autoclean*- Allways remove not old packages.\n"; } else { if ("gendb" eq $ARGV[0]) { genDB(); } elsif ("getdb" eq $ARGV[0]) { getDB(); } elsif ("install" eq $ARGV[0]) { $flags=0; foreach $pkg (@ARGV) { if (!$flags) { # Capture the Flag! $flags=1; next; } installPKG($pkg); } } elsif ("show" eq $ARGV[0]) { $flags=0; foreach $pkg (@ARGV) { if (!$flags) { # Capture the Flag! $flags=1; next; } showPKG($pkg); } } elsif ("search" eq $ARGV[0]) { @_=@ARGV; searchPKG(@ARGV); } elsif ("deinstall" eq $ARGV[0]) { $flags=0; $opt=""; foreach $pkg (@ARGV) { if (!$flags) { # Capture the Flag! if ($pkg=~/^\-/) { $opt.="$pkg "; } else { $flags=1; } next; } else { deinstallPKG($opt,$pkg); } } } elsif ("update" eq $ARGV[0]) { $mustup=""; if (! -f "$dbdir/$dbfile" ) { die "Cannot open database '$dbdir/$dbfile'."; } chdir("$dbdir"); $pkg=`ls`; @pkgs=split(/\n/,$pkg); foreach $pkg (@pkgs) { if ( -f $pkg ) { next; } $pkg=~/(.*)\-(.*)/; $pkgname=$1; $version=$2; $ver=getXXX("VER",$pkgname); if ($ver == -1) { #print "Package $pkgname not found on database\!\n"; } else { if ( $version < $ver ) { $mustup.="$pkgname "; print "$version -> $ver\t- $pkgname\n"; } } } } } exit 0; # Control Versions sub getXXX { ($what,$pkg)=@_; if (! -f "$dbdir/$dbfile" ) { die "Cannot open database file: '$dbdir/$dbfile'."; } open F,"$dbdir/$dbfile"; while() { if (/^PKG: $pkg/) { while() { if ($_ eq "--\n") { return ""; } if (/^$what: (.*)/) { return $1; } } } } close F; return -1; } # Main pkg_db functions sub genDB { chdir("$pkgsrc_base"); @cat=split(/\n/,`ls|grep -v CVS`); foreach $caty ( @cat ) { $dir="$pkgsrc_base/$caty"; if ( ! -d $dir ) { next; } $_=chdir($dir); if ($_ == 0) { die "Cannot chdir to $dir."; } foreach $pkg (`ls|grep -v CVS`) { $pkg=~s/\n//; if (! -d "$dir/$pkg" ) { next; } if (! -f "$dir/$pkg/Makefile" ) { next; die "Makefile for $dir/$pkg does not exist\n"; } open F,"$dir/$pkg/Makefile"; $v=0; $dep=""; # Deps without version while() { $str=$_; if ($v == 0) # If version not found yet. { $str=~/.*\=\s*(.*)/; $str=$1; $1=~/(.*)-(\d.\d.*)/; if ( ! $2 eq "" ) { print "PKG: $pkg\n"; print "VER: $2\n"; print "CAT: $caty\n"; $v=1; } } if ($str=~/COMMENT=\s*(.*)$/) { print "COM: $1\n"; } if ($str=~/^.include \"\.\.\/\.\.\/.*\/(.*)\/buildlink2\.mk\"/) { $dep.="$1 "; } if ($str=~/^DEPENDS.?\=\s*(.*)/) { $1=~/.*\=(.*)\:\.\.\/\.\.\/.*\/(.*)/; if ($2 eq "") { $1=~/.*\:\.\.\/\.\.\/.*\/(.*)/; $dep.="$1 "; } else { $dep.="$2\($1\) "; } } } close F; $dep=~s/\n//g; if ($v != 0) { if ( ! $dep eq "" ) { print "DEP: $dep\n"; } print "--\n"; } } } } sub getDB { print "Download the pkgsrc.db file and cp'it to: $dbdir/$dbfile.\n"; } sub installPKG { ($pkg)=@_; $deps=getXXX("DEP",$pkg); if ($deps == -1) { print "Package $pkg not found in database.\n"; exit(1); } @deps=split(/ /,$deps); foreach $dep (@deps) { $_deps=getXXX("DEP",$dep)." "; if ($_deps == -1) { print "Broken dep in database: '$dep'.\n"; exit(1); } $deps.=" $_deps "; @deps=split(/ /,$deps); print "dep+=$dep\n"; } chdir ("/usr/"); # Needs a valid value } sub showPKG { ($pkg)=@_; if (! -f "$dbdir/$dbfile" ) { die "Cannot open database file: '$dbdir/$dbfile'."; } open F,"$dbdir/$dbfile"; while() { if (/^PKG: (.*)$/) { if ($1 eq $pkg) { print "\n$_"; while() { if ($_ eq "--\n") { last; } print; } goto show_found; } } } print "Package $pkg not found.\n"; show_found: close F; } sub searchPKG { # in @_ I have the regexps :) shift; @moo=@_; $ok=0; if (! -f "$dbdir/$dbfile" ) { die "Cannot open database file: '$dbdir/$dbfile'."; } open F,"$dbdir/$dbfile"; while() { if (/^PKG: (.*)$/) { $pkgname=$1; } elsif (/^COM: (.*)$/) { $comment=$1; } elsif (/^CAT: (.*)$/) { $category=$1; } elsif ($_ eq "--\n") { $is=0; foreach $key (@moo) { if ($pkgname=~/$key/i || $comment=~/$key/i) { $is=1; } else { $is=0; goto search_next; } } if ($is == 1 ) { print "$pkgname \t- $comment.\n"; $ok=1; } } search_next: } if (! $ok ) { print "No packages matching.\n"; } close F; } sub deinstallPKG { ($flags,$pkg)=@_; print "==> Removing $pkg...\n"; `pkg_delete $flags $pkg`; }