#!/usr/bin/perl -w
# Copyright (C) Steve Haslam 1999. This is free software.
# Distributable under the terms of the GNU General Public License, version 2
# See the file 'COPYING' for license text.

require 5;
use strict;
use POSIX;
use Debian::ThemeConverters qw[printv printi printw printe wrapsystem wchdir direntries debwalk];

my $delondie;

$SIG{'__WARN__'} = \&printw;
$SIG{'__DIE__'} = sub { printe @_; if ($delondie) { wchdir('/'); wrapsystem("/bin/rm", "-rf", $delondie); } exit 255; };

Debian::ThemeConverters::main \&sawmillthemetodeb;

exit 0;

sub sawmillthemetodeb {
  my $themepkgfilename = shift;
  my $destdir = shift;

  my $themename = $themepkgfilename;
  $themename =~ s@.*/@@;
  $themename =~ s@.tar.gz$@@;
  $themename =~ s@.tar$@@;
  $themename =~ s@.tgz$@@;

  # Make Debian package name
  # Name convetion entirely invented by me and probably crap
  # "sawmill-xyyzy-theme"
  my $dpkgname = $themename;
  my $dpkgversion = "0.0";
  my $dpkgmaintainer = "Unknown";
  my $dpkgdescription = "Sawmill theme";

  $dpkgname =~ tr/A-Z/a-z/;
  $dpkgname =~ s/[^a-z+.-]//g;
  $dpkgname = "sawmill-$dpkgname-theme";
  printv "Debian package: $dpkgname";

  # Create working area
  my $tmpdir = tmpnam;
  mkdir($tmpdir, 0755) || die "Can't create $tmpdir: $!\n";
  $delondie = $tmpdir;
  printv "Entering directory $tmpdir";
  wchdir($tmpdir);

  # Prepare to get files
  my $installpath = "usr/share/sawmill/themes";
  if (wrapsystem("/usr/bin/install", "-d", "$tmpdir/$installpath") != 0) {
    die "$dpkgname: Unable to create directory $tmpdir/$installpath\n";
  }

  # Simply copy in .tar.gz file. Nice 'N' easy...
  if (wrapsystem("/bin/cp", $themepkgfilename, "$tmpdir/$installpath") != 0) {
    die "$themepkgfilename: Unable to copy theme\n";
  }

  # Make .deb file
  my $dpkgfile = "${dpkgname}_${dpkgversion}_all.deb";
  wchdir $destdir;
  Debian::ThemeConverters::makedeb($dpkgfile, $tmpdir,
				   { Name => $dpkgname,
				     Version => $dpkgversion,
				     Maintainer => $dpkgmaintainer,
				     Description => $dpkgdescription,
				     LongDescription => "This pacakge was automagically created from a Sawmill theme called $themename"
				   });

  # Clean up
  wchdir('/');
  if (wrapsystem("/bin/rm", "-rf", $tmpdir) != 0) {
    warn "Failed to remove work directory $tmpdir";
  }
  undef $delondie;

  return $dpkgfile;
}

=head1 NAME

sawmillthemetodeb - Convert Sawmill theme to Debian package

=head1 SYNOPSIS

B<sawmillthemetodeb>
[B<-p>]
[B<-i>]
[B<-r> I<command>]
[B<-R> I<command>]
I<tarball>...

=head1 DESCRIPTION

B<sawmillthemetodeb> converts Sawmill themes in the ".tar.gz" files
named in its command line to ".deb" Debian packages.

With no options, the tarballs will be converted to .deb files only. In
order to do this, the .deb builder (dpkg-deb(1)) need to be run at
least through fakeroot(1). The command used to get "root privileges"
is specified with the B<-r> option.

The B<-i> option will automatically install the converted packages and
then delete the .deb files. In order to install a package, B<real>
root privileges are needed. The command needed to get these is
specified with the B<-R> option, if different from the command given
by B<-r>.

The B<-p> option will cause B<sawmillthemetodeb> to print out which
.deb files were made in a program-parseable fashion. This is used
internally when the B<-r> option is used.

The output will look like this:

    sawmillthemetodeb:I: qw[foo.deb bar.deb quux.deb]

=head1 EXAMPLES

   sawmillthemetodeb -rfakeroot -Rsudo -i Babylon5.tar.gz
   sawmillthemetodeb -rsudo -i AboveEndor.tar.gz

=head1 SEE ALSO

dpkg(1), Debian::ThemeConverters(3pm)

=head1 AUTHOR

Steve Haslam <araqnid@debian.org>

=cut
