#!/usr/bin/env perl
#
# vntex-3.2.1
#
# vntex-update-maps
#
# Copyright 2007 Reinhard Kotucha <reinhard.kotucha@web.de>
#
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# 
# The current maintainer is Reinhard Kotucha.

$^W=1;

use Getopt::Long;
$Getopt::Long::autoabbrev=0;

($basename=$0)=~s/.*\///;

sub usage {
    print <<"EOF";
Usage:

    $basename [-h] [-n] -home|-sys
    
	$basename adds font map files to updmap.cfg and 
	executes updmap in order to create the font databases.

	You have to use the -home option if you extracted VnTeX 
	to TEXMFHOME and you have to use the -sys option if you 
	extracted VnTeX to TEXMFLOCAL

 	$basename -help will tell you the content of the 
	variables TEXMFHOME and TEXMFLOCAL.
	
    Options:

	-h|--help  Print this message.

	-sys       Run updmap-sys.

	-home      Run updmap.

        -n         Print the commands that would be executed, but 
	           do not execute them.

EOF
;
}


unless (@ARGV) { usage and exit 1 }

GetOptions 
    "n",
    "sys",
    "home",
    "help|h";


sub expand_var {
    my $var=shift;

    if ($^O=~/^MSWin(32|64)$/i) {
        open KPSEWHICH, 'kpsewhich --expand-var=$'  . "$var |";
    } else {
        open KPSEWHICH, 'kpsewhich --expand-var=\$' . "$var |";
    }
    while (<KPSEWHICH>) {
        return "$_";
        chop;
    }
    close KPSEWHICH;
}


if ($opt_help) {
    usage;
    print "    TEXMFHOME=", expand_var 'TEXMFHOME';
    print "\n";
    print "    TEXMFLOCAL=",expand_var 'TEXMFLOCAL';
    print "\n";
    exit 0;
}

$chicken_mode=($opt_n)? 1:0;

if ($opt_home) {
    $sys=0;
} elsif ($opt_sys) {
    $sys=1;
} else {
    die "No target specified.  Valid arguments: -home or -sys.\n\n"
	. "Run: perl $basename -help\n\n";
}

$updmap_cmd=($sys)? "updmap-sys":"updmap";

while (<DATA>) {
    last if /^__/;
    next if /^\s*$/;
    s/\@UPDMAP\@/$updmap_cmd/;
    @command=split " ", $_;
    print "@command\n";
    system @command unless $chicken_mode;
}

print "$updmap_cmd\n";
system "$updmap_cmd" unless $chicken_mode;

__DATA__
@UPDMAP@ --nomkmap --nohash --disable vnr.map
@UPDMAP@ --nomkmap --nohash --enable MixedMap vnrtext.map
@UPDMAP@ --nomkmap --nohash --enable MixedMap vnrother.map
@UPDMAP@ --nomkmap --nohash --enable Map urwvn.map
@UPDMAP@ --nomkmap --nohash --enable Map chartervn.map
@UPDMAP@ --nomkmap --nohash --enable Map mscorevn.map
@UPDMAP@ --nomkmap --nohash --enable Map arevvn.map
@UPDMAP@ --nomkmap --nohash --enable Map classicovn.map
@UPDMAP@ --nomkmap --nohash --enable Map cmbrightvn.map
@UPDMAP@ --nomkmap --nohash --enable Map comicvn.map
@UPDMAP@ --nomkmap --nohash --enable Map concretevn.map
@UPDMAP@ --nomkmap --nohash --enable Map garamondvn.map
@UPDMAP@ --nomkmap --nohash --enable Map grotesqvn.map
@UPDMAP@ --nomkmap --nohash --enable Map vntopia.map
@UPDMAP@ --nomkmap --nohash --enable Map txttvn.map
