#!/usr/bin/perl
#Tag 0x00000f00
#ident "$Revision $"
#
# Copyright Silicon Graphics, 1997-1998
#
# Generate an inst selections file from an installed system.
# correct results only from a 6.3 or later system, will try to
# do something reasonable for 6.2 and older systems
#
# The "-n" option will generate selections files with version
# numbers, which is good for exact clones: "inst fails if the
# versions mismatch". The default, without version numbers implies:
# "take the latest version of this product".

require "getopts.pl";		# works with perl4 and perl5

&Getopts("n");			# put version numbers in selections file

open(SP, "/usr/sbin/showprods -D 3 -nF 2>&1 |") ||
  die "can't run showprods? $!\n";

$rec = <SP>;
if ($rec =~ /^Illegal option -- F/) {
    # pre-6.3 showprods format
    print STDERR "$0: Pre-IRIX6.3 version found.";
    print STDERR " You will need to customize the output\n";
    close(SP);
    open(SP, "/usr/sbin/showprods -D 3 -s |") ||
      die "can't run showprods? $!\n";
    print "from user\@host:/path\nk *\n";
    while (<SP>) {
	print "i $_" if /[^.]+\.[^.]+\.[^.]+/;
    }
    close(SP);
} else {
    # post-6.2 version, with distribution location
    while(<SP>) {
	($arg, $prod, $vers, $dist) = split;
	last if /^I/;
    }
    $seen{$dist} = 1;
    print "from $dist\n";
    if ($prod =~ /^[^.]+\.[^.]+\.[^.]+$/) {
	if ($opt_n) {
	    push(@sel, "i $prod $vers\n");
	} else {
	    push(@sel, "i $prod\n");
	}
    }

    while(<SP>) {
	next unless /^I/;
	($arg, $prod, $vers, $dist) = split;
	if ($prod =~ /^[^.]+\.[^.]+\.[^.]+$/) {
	    if (! $dist) {
		$miss{(split(/\./, $prod))[0]}++;
	    } elsif (! defined($seen{$dist})) {
		$seen{$dist} = 1;
		print "open $dist\n";
	    }
	    if ($opt_n) {
		push(@sel, "i $prod $vers\n");
	    } else {
		push(@sel, "i $prod\n");
	    }
	}
    }
    close(SP);
}
print "k *\n";
print @sel;
if (%miss && $opt_n) {
    print STDERR "$0: the following products have no associated distribution\n";
    print STDERR "\tlocation. You will have to add to the distribution list in the output:\n";
    $col=0; $margin=70;
    for $f (keys % miss) {
	$l = length($f);
	if (($col + $l) > $margin) {
	    print STDERR "\n";
	    $col = 0;
	}
	print STDERR "$f ";
	$col += $l + 1;
    }
    print STDERR "\n";
}
