#!/usr/bin/perl 
#
# Copyright (C) 1997-1998 Federico Di Gregorio.
#
# This program is part of the Definitive Type Manager package.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

use strict;

# Some program infos and other global stuff
my $prog_name = 'dtm';
my $prog_version = '0.4';
my $main_catalog_path = '/usr/lib/dtm/catalogs';
my $add_catalog_path = '/etc/dtm/catalogs';
my $user_catalog_path = "$ENV{HOME}/fonts/catalogs";
my $super_catalog_path = '/etc/dtm/catalogs';
my $super_methods_path = '/etc/dtm/methods';
my $user_methods_path = "$ENV{HOME}/fonts/methods" ;
my @font_types = ('type1');
my %font_catalogs;
my @snippets;


# Packages used
use Getopt::Long;
use DTM::Utils;
use DTM::Catalog;
use DTM::Snippet;


# Display the licence
sub Licence {
    print <<EOT;

  $prog_name (dtm) $prog_version
  Copyright (C) 1997-1998 Federico Di Gregorio. 

  This program is part of the Definitive Type Manager.

  This is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2, or (at your option) any later
  version.

  This is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  for more details.

  You should have received a copy of the GNU General Public License with
  your Debian GNU/Linux system, in /usr/doc/copyright/GPL, or with the
  dfont source package as the file COPYING.  If not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 

EOT
    exit;
}


# Display a banner
sub Banner {
    print <<EOT;
$prog_name $prog_version
This program is part of the Debian Type Manager.
Type $prog_name --help for help about managing fonts.
Type $prog_name --licence for copyright licence and lack of warranty.

EOT
    exit;
}


# Display usage help
sub Usage {
    print <<EOT;
Usage: 
  $prog_name actions options <catalog snippet>

Where the actions are:
  -a, --add              adds the fonts in catalog snippet
  -p, --purge            remove the fonts in the catalog snippet
  -u, --update           updates everything

And the options are:
  -l, --local, --user    acts on the user home directory with std methods
  -L, --local-methods    uses custom methods in user home directory
 
And: 
  -h, --help             display this help message
      --licence          display the licence
      --version          display a banner

EOT
    exit;
}


# Sets some global variables to hold parameters
my ($opt_update, $opt_add, $opt_purge);
my ($opt_local, $opt_local_methods, $opt_licence, $opt_help, $opt_version);

# Process parameters
sub GetParams {
    my $ret = GetOptions("update|u", \$opt_update,
			 "add|a", \$opt_add,
			 "purge|p", \$opt_purge,
			 "local|user|l", \$opt_local,
			 "user-methods|L", \$opt_local_methods,
			 "licence", \$opt_licence,
			 "help|h", \$opt_help,
			 "version", \$opt_version);

    Banner() if $opt_version;
    Usage() if ($opt_help);
    Licence() if ($opt_licence);
    
    # if more than one action is requested, there is an error,
    # but at least an action is required
    my $opt = ($opt_update ? 1 : 0) +
	      ($opt_add ? 1 : 0) +
	      ($opt_purge ? 1 : 0);
    $opt_local = 1 if $opt_local_methods;

    if ($opt == 0) {
	dtm_error("an action (e.g., update, add, purge) is required");
    }

    if ($opt > 1) {
	dtm_error("only one action (e.g., update, purge, add) is permitted");
    }

    # gets snippet list
    if ($opt_update) {
	foreach my $font_type (@font_types) {
	    @snippets = $opt_local ?
		(glob("$user_catalog_path/$font_type/*")) :
		    (glob("$add_catalog_path/$font_type/*"),
		     glob("$main_catalog_path/$font_type/*"));
	}
    } 
    else {
	@snippets = (@ARGV);
	dtm_error("at least one snippet for add or purge actions")
	    unless @snippets;
    }
}


# strips a name from the path
sub StripDir {
    my $ffile = shift;
    my ($path, $file) = abs_path_file($ffile);
    return $file;
}


# makes some directories if absent from the user home dir
sub MakeUserDirs {
    safe_system("install -d $ENV{HOME}/fonts/catalogs") 
	if !(-d "$ENV{HOME}/fonts/catalogs");
    safe_system("install -d $ENV{HOME}/fonts/methods") 
	if !(-d "$ENV{HOME}/fonts/methods");
    safe_system("indtall -d $ENV{HOME}/fonts/type1/outlines")
	if !(-d "$ENV{HOME}/fonts/type1/outlines");
}


# Process command line arguments and creates a new font catalog
Getopt::Long::config("bundling");
GetParams();

# if local creates the user directories
MakeUserDirs if $opt_local;

# loads the catalogs and put them in the catalog hash
foreach my $font_type (@font_types) {
    my $catalog;
    if ($opt_local) {
	$catalog = DTM::Catalog::user_catalog($font_type)
	    unless $opt_local_methods;
	$catalog = DTM::Catalog::user_catalog($font_type,
					      $user_methods_path)
	    if $opt_local_methods;
    }
    else {
	$catalog = DTM::Catalog::sys_catalog($font_type);
	}
			
    $font_catalogs{$catalog->{'CATALOG'}} = $catalog;		     
}


# then executes the required action
while (my ($catalog_name, $catalog) = each %font_catalogs) {

    if ($opt_add) {
	# makes a backup copy of the catalog
	safe_system("cp -f $catalog_name $catalog_name.old") 
	    if -f $catalog_name;
	
	# add the snippets
	print "Adding snippets to $catalog->{'TYPE'} catalog:\n";
	foreach my $snippet (@snippets) {
	    print "-- " . StripDir($snippet) . " --\n";
	    my $snipcat = new DTM::Catalog($snippet, 'FAIL_ON_CREATE');
	    $catalog->merge($snipcat);
	    my $log = $catalog->error();
	    if ($log) {
		print "\n";
		dtm_warning("dtm: some fonts were not added to the catalog");
		dtm_warning("dtm: this can be or not an error; a log follows...");
		print $log;
	    }
	}
	print "\n";
	# saves the catalog and goes to the next
	$catalog->save();
    }

    if ($opt_purge) {
	# makes a backup copy of the catalog
	safe_system("cp -f $catalog_name $catalog_name.old") 
	    if -f $catalog_name;
	
	# purge from the snippets
	print "Purging snippets from $catalog->{TYPE} catalog:\n";
	foreach my $snippet (@snippets) {
	    print "-- " . StripDir($snippet) . " --\n";
	    my $snipcat = new DTM::Catalog($snippet, 'FAIL_ON_CREATE');
	    foreach my $oink ($snipcat->filter()) {
		$catalog->remove($oink) or 
                    dtm_warning("dtm: " . $catalog->error());
		my $warn = $catalog->error();
		dtm_warning("dtm: $warn") if $warn; 
	    }
	}
	print "\n";
	# saves the catalog and goes to the next
	$catalog->save();
    }
}



