#!/usr/local/bin/perl -w # Fake yinst for POSIX systems with apt and dpkg use strict; use vars qw( %ABBREV $APT_CACHE $APT_FILE $APT_GET @COMMANDS $DPKG $PROG $SUDO $USAGE $VERSION $arg1 $cmd $globargs ); %ABBREV = ('add' => 'install', 'ck' => 'check', 'del' => 'remove', 'ls' => 'list', 'rm' => 'remove'); $APT_CACHE = "apt-cache"; $APT_GET = "apt-get"; @COMMANDS = ('activate', 'changed', 'check', 'check-config', 'clean', 'clone', 'create', 'crontab', 'cvsunzip', 'collect', 'db', 'deactivate', 'diff', 'env', 'fetch', 'help', 'history', 'install', 'list', 'lock', 'man', 'mvroot', 'packages', 'reload', 'remove', 'repair', 'restart', 'restore', 'save', 'scp', 'self-install', 'self-update', 'set', 'ssh', 'start', 'stop', 'tag', 'unlock', 'unset', 'version'); $DPKG = "dpkg"; $PROG = "yinst"; $SUDO = -x "/usr/local/bin/sudo" ? "/usr/local/bin/sudo" : "/usr/bin/sudo"; $VERSION = "0.0.0.1"; sub usage { #inserting yinst.usage $USAGE =< Usage: $PROG ] Commands: help install { [-] } ... ls [-files] [[-]] ... packages [] remove [-] ... version EOF ; print STDERR $USAGE; } sub msg { # emsg(); # foreach (map { split(/\n/) } @_) { # print $MSGPREFIX.$_."\n"; # } print(@_); print("\n"); } sub autoabbrev { # Adapted from Getopt::Long my($cmd) = @_; my $trycmd = lc($cmd); my $pat = quotemeta($trycmd); my @names = @COMMANDS; my @hits = grep(/^$pat/,@names); if (grep($_ eq $trycmd,@hits) == 1) { return $trycmd; } my %hit; foreach (@hits) { my $hit = $_; $hit{$hit} = 1; } if (keys(%hit) > 1) { cmd_error_exit("$cmd is ambiguous (".join(", ",@hits).")"); } @hits = keys(%hit); if (@hits == 0) { return ''; } $hits[0]; } sub cmd_help { usage(); } sub cmd_install { my @args = @ARGV; my $args_str = join(" ", @args); open(apt_get_pipe, "$SUDO $APT_GET install $args_str |") || die("Could not execute $APT_GET!"); while () { chop(); print("yinst ($APT_GET): $_\n"); } close(apt_get_pipe); } sub cmd_list { my $args_str = join(" ", @ARGV); my $cmd; if ($ARGV[0] && $ARGV[0] eq "--files") { shift(@ARGV); return cmd_list_files(); } else { $cmd = "$DPKG -l $args_str"; } open(cmd_pipe, "$cmd |") || die("Could not execute \"$cmd\"!"); while () { chop(); my ($status, $package, $version) = split(); if ($status eq "ii") { print("yinst: $package-$version\n"); } } close(cmd_pipe); } sub get_pkg_version { my ($pkg_name) = @_; # print("get_pkg_version: pkg_name = \"$pkg_name\"\n"); my $cmd = "$DPKG -l $pkg_name"; open(cmd_pipe, "$cmd |") || die("Could not execute \"$cmd\"!"); while () { chop(); my ($status, $package, $version) = split(); if ($status eq "ii" && $pkg_name eq $package) { return "$pkg_name-$version"; } } close(cmd_pipe); } sub get_pkg_which_owns_file { my $args_str = join(" ", @ARGV); my $cmd = "$DPKG -S $args_str"; # print("get_pkg_which_owns_file: $cmd\n"); open(cmd_pipe, "$cmd |") || die("Could not execute \"$cmd\"!"); while () { chop(); my ($pkg_name) = split(":"); if ($pkg_name) { return $pkg_name } } close(cmd_pipe); } sub cmd_list_files { my $args_str = join(" ", @ARGV); my $cmd; my ($pkg_name, $pkg_version); if ($ARGV[0] && -f $ARGV[0]) { $pkg_name = get_pkg_which_owns_file(); $pkg_version = get_pkg_version($pkg_name); printf("yinst: %-60s $pkg_version\n", $ARGV[0]); return; } else { $cmd = "$DPKG -L $args_str"; $pkg_version = get_pkg_version($ARGV[0]); } open(cmd_pipe, "$cmd |") || die("Could not execute \"$cmd\"!"); while () { chop(); if ($pkg_version) { printf("yinst: %-60s $pkg_version\n", $_); } else { print("yinst: $_\n"); } } close(cmd_pipe); } sub cmd_packages { my @args = @ARGV; my $args_str = join(" ", @args); my $cmd = "$APT_CACHE showpkg $args_str"; system($cmd); } sub cmd_remove { my @args = @ARGV; my $args_str = join(" ", @args); my $cmd = "$SUDO $APT_GET remove $args_str"; system($cmd); } sub cmd_version { print("yinst: yinst $VERSION (Marc's hack for OS X)\n"); exit(0); } #### Main #### $arg1 = shift(@ARGV); if (!$arg1 || $arg1 eq "-h" || $arg1 eq "-help" || $arg1 eq "--help") { $arg1 = "help"; } elsif ($arg1 eq "-v" || $arg1 eq "-ver" || $arg1 eq "--version") { $arg1 = "version"; } elsif ($arg1 =~ /^-/) { cmd_error_exit("Need a subcommand before the options" ." (e.g. yinst list -desc)"); } $cmd = $arg1; if (defined($ABBREV{$cmd})) { $cmd = $ABBREV{$cmd}; } else { $cmd = autoabbrev($cmd); } if (!$cmd) { unshift(@ARGV,$arg1); $arg1 = $cmd = "install"; msg("Assuming you meant: $PROG install ".join(" ",@ARGV)); } $globargs = grep(/[[*?]/, @ARGV); # grep { $SIG{$_} = \&uerror_exit } qw/INT HUP QUIT TERM/; { no strict 'refs'; my $xcmd = "cmd_".$cmd; $xcmd =~ tr/-/_/; &$xcmd(); }