#!/usr/bin/perl -w

#  wminstallfonts - install X11 fonts

#  This program is released under the GNU General Public License.
#  See the file COPYING. If that file is missing, see
#  http://www.gnu.org/copyleft/gpl.html

use File::Copy;
use Getopt::Long;
Getopt::Long::Configure('bundling');
$programname = 'wminstallfonts';

$ttfdir_prog = 'ttmkfdir';
`which $ttfdir_prog 2>/dev/null`;
if ($?) {
  $ttfdir_prog = 'mkttfdir';
  `which $ttfdir_prog 2>/dev/null`;
  $ttfdir_prog = '' if $?;
}

if (!GetOptions(
  'h|help',        \$options{help},
  't|ttfdir=s',    \$options{ttfdir},
  'f|fontdir=s',   \$options{fontdir},
  'd|decompress',  \$options{decompress},
  'D|debug',       \$options{debug},
  'c|compress',    \$options{compress},
  'C|clear',       \$options{clear}
  )) { exit 1 };

if ($options{help}) {
  printusage();
  exit;
}

if ($options{compress} and $options{decompress}) {
  die "Options -c and -d conflict\n";
}

$debug = $options{debug};

unless ($fontdir = $options{fontdir}) {
  if (@ARGV) {
    die "Option -f (font directory) is required\n";
  } else {
    printusage();
    exit;
  }
}

$ttfdir = $options{ttfdir} || '';

#===========================================================================#

$inputdir = shift or die "Please supply a directory with new fonts\n";
$anyfonts = 0;
$anyttf = 0;

opendir DH, $inputdir or die "Can't opendir $inputdir: $!\n";

while(defined($entry = readdir DH)) {
  next if -d "$inputdir/$entry";

  $name = $entry;
  if ($name =~ s/\.gz$//) {
    $compressed = 1;
  } else {
    $compressed = 0;
  }

  if ($name =~ /.\.(?:pcf|pfa|pfb|spd)$/) {

      # Check whether it's already installed
    if (-e "$fontdir/$name" or -e "$fontdir/$name.gz") {
      unlink "$inputdir/$entry" if $options{clear};
      next;
    }

    if ($compressed) {
      if ($options{decompress}) {
        if (system('gzip', '-d', "$inputdir/$entry")) {
          print "Warning: failed to compress $inputdir/$entry\n";
          next;
        }
        $entry =~ s/\.gz$//;
        unless (-e "$inputdir/$entry") {
          die "Error: after decompression, $inputdir/$entry was expected but not found!\n";
        }

      }
    } elsif ($options{compress}) {
      if (system('gzip', "$inputdir/$entry")) {
        print "Warning: failed to uncompress $inputdir/$entry\n";
        next;
      }
      $entry .= '.gz';
      unless (-e "$inputdir/$entry") {
        die "Error: after compression, $inputdir/$entry was expected but not found!\n";
      }
    }

    move("$inputdir/$entry", $fontdir) or
      die "Error when moving $inputdir/$entry to $fontdir\n";
    print "$programname: $entry -> $fontdir\n" if $debug;
    $anyfonts = 1;

  } elsif ($ttfdir and $ttfdir_prog  and $name =~ /\.ttf$/i) {

      #  Don't allow any ttf fonts to be compressed: they probably won't work

    if ($compressed) {
      if (system('gzip', '-d', "$inputdir/$entry")) {
        print "Warning: failed to uncompress $inputdir/$entry\n";
        next;
      }
      $entry =~ s/\.gz$//;
      unless (-e "$inputdir/$entry") {
        die "Error: after decompression, $inputdir/$entry was expected but not found!\n";
      }
    }

    $name =~ s/\.ttf$/.ttf/i;    # undo capitalization

      # Check whether it's already installed
    if (-e "$ttfdir/$name") {
      unlink "$inputdir/$entry" if $options{clear};
      next;
    }

    move ("$inputdir/$entry", "$ttfdir/$name") or
      die "Error when moving $inputdir/$entry to $ttfdir/$name\n";
    print "$programname: $entry -> $ttfdir\n" if $debug;
    $anyttf = 1;
  }

}

closedir DH;

if ($anyfonts) {
  system('mkfontdir', $fontdir) and
    die "Couldn't mkfontdir $fontdir\n";
}

if ($anyttf) {
  if ($ttfdir_prog eq 'ttmkfdir') {
    system($ttfdir_prog, '-d', $ttfdir, '-o', "$ttfdir/fonts.scale") and
      die "Couldn't $ttfdir_prog $ttfdir\n";
  } elsif ($ttfdir_prog eq 'mkttfdir') {
    system($ttfdir_prog, $ttfdir) and
      die "Couldn't $ttfdir_prog $ttfdir\n";
  } else {
    die "$programname: don't know how to use $ttfdir_prog\n";
  }

  system('mkfontdir', $ttfdir) and
    die "Couldn't mkfontdir $ttfdir\n";
}

if ($anyfonts or $anyttf) {
  system('xset', 'fp', 'rehash') and
    die "Couldn't run xset fp rehash\n";
  print "Fonts installed successfully\n";
}

sub printusage {
  print "Usage: $programname [options] -f <fontdir> <temp-dir>
Install fonts from <temp-dir> to directories specified with -t and -f.
Any fonts that are installed will be removed from <temp-dir>.

  -h, --help        print this help text
  -c, --compress    compress fonts (but not truetype fonts) with gzip
  -C, --clear       clear out fonts that are already installed
  -d, --decompress  uncompress any gzipped fonts
  -f, --fontdir     where to install fonts (except truetype)
  -t, --ttfdir      where to install truetype fonts
  -D, --debug       enable debugging output

";
}

