#!/bin/sh
# © Ričardas Čepas <rch@WriteMe.Com>. Copying policy: BSD style
ttmkfdir -o fonts.scale -m 20 -c; perl -i.orig -we '
	require 5.0;
	use strict;
	undef $/; $_=<>."\n";
	print STDERR "Wait a minute..\n";
	if ( (defined $ARGV[0] && $ARGV[0] eq "-lt") || $ENV{LANG} =~ "^lt" ) {
		s/^(.+?\s+-[^-]+?-[^-]+?L[TV]-.*?)-iso8859-4\s*?$/$&\n$1-windows-1257/mg;
	}
	my %subst_families = (
		helvetica => [ "Helvetica New", "Arial", "Verdana", "Gill Sans" ],
		#, "HelveticaLT"
		"times" => [ "Times New", "Times New Roman", "CG Times", "Bodoni", "Utopia" ],
		courier => [ "Courier New", "CourierLT" ],
	);
	my %subst_slants = (
		r => ["r"],
		i => ["i"],
		o => ["o","i"],
	);
	foreach my $family (keys %subst_families) {
	foreach my $weight ("medium", "bold") {
	foreach my $slant (keys %subst_slants) {
	   print STDERR "$family-$weight-$slant ";
SUBST:	foreach my $encoding (1..10,15) {
	   if ( ! m/^.+?\s+-[^-]+?-$family-$weight-$slant-.*?-iso8859-$encoding\s*$/mi ) {
		foreach my $subst_family (@{$subst_families{$family}}) {
		foreach my $subst_slant (@{$subst_slants{$slant}}) {
#			printf STDERR "trying $subst_family-$weight-$subst_slant\n";
			s/^(.+?\s+-[^-]+?)-$subst_family-$weight-$subst_slant-(.*?-iso8859-$encoding\s*?)$/$&\n$1-$family-$weight-$slant-$2/mi and next SUBST;
		}
		}
		( $encoding == 4 ) and print STDERR "\nSubstitution for -$family-$weight-$slant-*-iso8859-$encoding not found.\n";
	   }
	}
	}
	}
	}
	print STDERR "\n";
	s/^(.+?\s+-.*)-iso8859-1\s*?$/$&\n$1-iso10646-1/mg;
	my $num_of_fonts = s/^.+?\s+-/$&/mg;
	s/^.*?$/$num_of_fonts/m;
	print;
	' fonts.scale
	#s/^\s*\d+\s*$/$&! Generated by ttmkfdir.\n! xfsft uses default font encoding for unknown charset in X font name structure,\n! so adding *windows-1257 for *LT fonts should work.\n/;' fonts.scale
mkfontdir
killall -USR1 xfs
DISPLAY=:0.0; export DISPLAY; xset fp rehash
