#!/usr/bin/perl -w

#  make-lsm
#
#  Version: 0.1
#
#  Utility to create LSM (Linux Software Map) description files, version 4.
#    http://www.execpc.com/lsm/
#
#  Usage:
#
#    *  Fill in fields below, according to the format of LSM
#       files.  Do not worry about formatting continuation lines
#       or the 80 character limit -- that's what this program is
#       for.
#
#    *  Put the resulting program in the program's development directory.
#
#    *  Create a file 'VERSION' containing the version, e.g. 1.21
#
#    *  Run ./make-lsm
#
#    -  The following fields will be filled in by this program:
#       Version, Entered-date, and the <size>-<filename> part of Primary-site

use POSIX;

%fields = (

    'Title' => 'wmtheme',

#    'Description' => 'wmtheme is a Perl program for managing AfterStep, Blackbox, Enlightenment, Golem, GTK+, IceWM, Oroborus, Sawfish, Window Maker, and xmms themes. It can download, install, activate, and remove themes. It handles badly packaged themes and minimizes version mismatches.',

    'Description' => 'wmtheme is a Perl program for managing AfterStep, Blackbox, Enlightenment, Golem, GTK+, IceWM, Oroborus, Sawfish, Window Maker, and xmms themes. It can install, activate, and remove themes. It handles badly packaged themes and minimizes version mismatches.',

    'Keywords' => 'X11, themes, AfterStep, Window Maker, Blackbox, Enlightenment, Sawfish, IceWM, wmaker, Golem, GTK+, Oroborus, xmms',

    'Author' => ['jswink@pacbell.net (Joshua Swink)'],

    'Maintained_by' => ['jswink@pacbell.net (Joshua Swink)'],

    'Primary_site' => 'http://download.sourceforge.net/wmtheme/',

    'Alternate_site' => 'http://www.ibiblio.org/pub/linux/X11/xutils/',

    'Original_site' => '',

    'Platforms' => 'X11',

    'Copying_policy' => 'GPL',

  );

#----------------------------------
# Set up some lsm-describing data
#----------------------------------

$margin = 17;
$maxline = 78;

@order = qw(
  Title          
  Version        
  Entered-date   
  Description    
  Keywords       
  Author         
  Maintained-by  
  Primary-site   
  Alternate-site 
  Original-site  
  Platforms      
  Copying-policy 
  );

%required = (
  'Title' => 1,
  'Version' => 1,
  'Entered_date' => 1,
  'Description' => 1,
  'Primary_site' => 1
  );

#----------------------------------------
# Fill in the fields that a program can
#----------------------------------------

chomp($version = `cat VERSION`);
$version or die "make-lsm: can't get version from file 'VERSION'\n";

$tarball = "$fields{Title}-$version.tar.gz";

  # Allow lsm to be created even if the tarball isn't present, putting
  # '1kb' as the size.  This means you can make the tarball, including
  # the wrongish lsm file, and then make another lsm file with the
  # correct size.  The final tarball, including an lsm with the correct
  # size, can be made as the second tarball instead of the third.
  # If you want the accuracy.

if (-f $tarball) {
  $bytes = (stat $tarball)[7];
  if ($bytes < 1048576) {
    $size = sprintf('%0.1fkb', $bytes / 1024);
  } else {
    $size = sprintf('%0.1fMB', $bytes / 1048576);
  }
  $size =~ s/\.0//;
} else {
  $size = '1kb';
}

$lsmfile = "$fields{Title}-$version.lsm";


$fields{Version} = $version;
$fields{Entered_date} = strftime("%Y-%m-%d", localtime);

#----------------------------------------
# Make sure required fields are present
#----------------------------------------

foreach $field (@order) {
  if (!$fields{$field} and $required{$field}) {
    die "Missing required field $field\n";
  }
}

#----------------------------------------
#  Write the file
#----------------------------------------

open F, ">$lsmfile" or die "Can't write $lsmfile: $!\n";

select F;

print "Begin4\n";

print itemhead('Title'), "$fields{Title}\n";
print itemhead('Version'), "$fields{Version}\n";
print itemhead('Entered-date'), "$fields{Entered_date}\n";
lsmfmt('Description', $fields{Description});
lsmfmt('Keywords', $fields{Keywords}) if $fields{Keywords};

if (@{$fields{Author}}) {
  print itemhead('Author'), "$fields{Author}->[0]\n";
  for ($i = 1; $i < @{$fields{Author}}; $i++) {
    print ' ' x $margin, "$fields{Author}->[$i]\n";
  }
}

if (@{$fields{Maintained_by}}) {
  print itemhead('Maintained-by'), "$fields{Maintained_by}->[0]\n";
  for ($i = 1; $i < @{$fields{Maintained_by}}; $i++) {
    print ' ' x $margin, "$fields{Maintained_by}->[$i]\n";
  }
}

print itemhead('Primary-site'), "$fields{Primary_site}\n";
print ' ' x $margin, "$size $tarball\n";

print itemhead('Alternate-site'), "$fields{Alternate_site}\n"
  if $fields{Alternate_site};

print itemhead('Original-site'), "$fields{Original_site}\n"
  if $fields{Original_site};

lsmfmt('Platforms', $fields{Platforms}) if $fields{Platforms};
lsmfmt('Copying-policy', $fields{Copying_policy}) if $fields{Copying_policy};

print "End\n";

select STDOUT;

close F;

print "make-lsm: wrote $lsmfile\n";

sub itemhead {
  "$_[0]:" . ' ' x ($margin - (length($_[0]) + 1));
}

sub lsmfmt {
  my ($field, $text) = @_;
  my (@words, $i);

  my $line = itemhead($field);

  @words = split(/ +/, $text);

  for ($i = 0; $i < @words; $i++) {
    if (length($line) eq $margin) {
      $line .= $words[$i];
    } elsif (length($line) + 1 + length($words[$i]) > $maxline) {
      print "$line\n";
      $line = ' ' x $margin . $words[$i];
    } else {
      $line .= " $words[$i]";
    }
  }

  print "$line\n" if length($line) > $margin;
}


