#!/usr/bin/perl
# Usage: ./mkcdlist < list > list.new

@list = <>; # get packages list from stdin

foreach (@list)
  {
    chomp();

    # we don't need comments, so they are removed
    $_ =~ s/\/\*.*\*\///gi;
    $_ =~ s/ //g;

    # skip if line is empty
    next if $_ eq "";

    # resolve all dependencies, recommands and suggests
    resolve_depends($_);
  }

print "/* YACS Task for Linuxtag 2000 Debian CD */\n\n\n";

foreach (sort keys %packages)
  {
    print "$_\n"
      unless $_ eq "";
  }

sub resolve_depends
  {
    my $package = shift;
    break if $packages{$package} >= 1;

    foreach (`apt-cache show $package`)
      {
	chomp();
	if (/^Depends:/ || /^Pre\-Depends:/ || /^Suggests:/ || /^Recommands:/)
	  {
	    my $dep = $_;
	    $dep =~ s/[a-z|A-Z|\-]*: *//;
	    $dep =~ s/,/~/g;
	    $dep =~ s/\|/~/g;
	    $dep =~ s/ //g;
	    my @depends = split /~/, $dep;
	    foreach (@depends)
	     {
	       $_ =~ s/\(.*//;
	       if ($packages{$_} < 1)
		 {
		   $packages{$_} = 1;
		   resolve_depends($_)
		 }
	     }
	 }
      } 

    $packages{$package} = 2;
  }


