#!/usr/bin/perl -pi.bak -w

BEGIN { $ENV{OOO_VENDOR} ||= "unknown"; $uisans = 0; $uifixed = 0; }

# This does a partial substitution of the really
# mechanical bits of our fonts.

    # make sure that the MS compatible fonts like Albany, Arial are preferred
    # over the Bitstream Vera fonts

    s/(Bitstream Vera Sans;.*)Albany;/Albany;$1/;
    s/(Bitstream Vera Sans;.*)Arial;/Arial;$1/;
    s/(Bitstream Vera Sans Mono;.*)Cumberland;/Cumberland;$1/;
    s/(Bitstream Vera Sans Mono;.*)Courier New;/Courier New;$1/;
    s/(Bitstream Vera Serif;.*)Thorndale;/Thorndale;$1/;
    s/(Bitstream Vera Serif;.*)Times New Roman;/Times New Roman;$1/;

    # add Albany, Cumberland, and Thorndale
    (m/Albany;/)     || s/Arial;/Albany;Arial;/g;
    (m/albany;/)     || s/arial;/albany;arial;/g;
    (m/Thorndale;/)  || s/Times New Roman;/Thorndale;Times New Roman;/g;
    (m/thorndale;/)  || s/timesnewroman;/thorndale;timesnewroman;/g;
    (m/Cumberland;/) || s/Courier New;/Cumberland;Courier New;/g;
    (m/cumberland;/) || s/couriernew;/cumberland;couriernew;/g;

    # add AMT fonts
    (m/Albany AMT;/)     || s/Albany;/Albany AMT;Albany;/g;
    (m/albanyamt;/)      || s/albany;/albanyamt;albany;/g;
    (m/Thorndale AMT;/)  || s/Thorndale;/Thorndale AMT;Thorndale;/g;
    (m/thorndaleamt;/)   || s/thorndale;/thorndaleamt;thorndale;/g;
    (m/Cumberland AMT;/) || s/Cumberland;/Cumberland AMT;Cumberland;/g;
    (m/cumberlandamt;/)  || s/cumberland;/cumberlandamt;cumberland;/g;
    (m/andymt;/)         || s/comicsansms;/andymt;comicsansms;/g;
    (m/andy;/)           || s/kidprint;/andy;kidprint;/g;

# New bits from SUSE:

    # add Nimbus fonts
    (m/Nimbus Sans L;/)      || s/Arial;/Arial;Nimbus Sans L;/g;
    (m/nimbussansl;/)        || s/arial;/arial;nimbussansl;/g;
    (m/Nimbus Roman No9 L;/) || s/Times New Roman;/Times New Roman;Nimbus Roman No9 L;/g;
    (m/nimbusromanno9l;/)    || s/timesnewroman;/timesnewroman;nimbusromanno9l;/g;
    (m/Nimbus Mono L;/)      || s/Courier New;/Courier New;Nimbus Mono L;/g;
    (m/nimbusmonol;/)        || s/couriernew;/couriernew;nimbusmonol;/g;


# prune duplicates

    s/Albany AMT;(.*)Albany AMT;/Albany AMT;$1/;
    s/Albany;(.*)Albany;/Albany;$1/;
    s/Arial;(.*)Arial;/Arial;$1/;
    s/Nimbus Sans L;(.*)Nimbus Sans L;/Nimbus Sans L;$1/;

    s/albanyamt;(.*)albanyamt;/albanyamt;$1/;
    s/albany;(.*)albany;/albany;$1/;
    s/arial;(.*)arial;/arial;$1/;
    s/nimbussansl;(.*)nimbussansl;/nimbussansl;$1/;

    s/Thorndale AMT;(.*)Thorndale AMT;/Thorndale AMT;$1/;
    s/Thorndale;(.*)Thorndale;/Thorndale;$1/;
    s/Times New Roman;(.*)Times New Roman;/Times New Roman;$1/;
    s/Nimbus Roman No9 L;(.*)Nimbus Roman No9 L;/Nimbus Roman No9 L;$1/;

    s/thorndaleamt;(.*)thorndaleamt;/thorndaleamt;$1/;
    s/thorndale;(.*)thorndale;/thorndale;$1/;
    s/timesnewroman;(.*)timesnewroman;/timesnewroman;$1/;
    s/nimbusromanno9l;(.*)nimbusromanno9l;/nimbusromanno9l;$1/;

    s/Cumberland AMT;(.*)Cumberland AMT;/Cumberland AMT;$1/;
    s/Cumberland;(.*)Cumberland;/Cumberland;$1/;
    s/Courier New;(.*)Courier New;/Courier New;$1/;
    s/Nimbus Mono L;(.*)Nimbus Mono L;/Nimbus Mono L;$1/;

    s/cumberlandamt;(.*)cumberlandamt;/cumberlandamt;$1/;
    s/cumberland;(.*)cumberland;/cumberland;$1/;
    s/couriernew;(.*)couriernew;/couriernew;$1/;
    s/nimbusmonol;(.*)nimbusmonol;/nimbusmonol;$1/;


    # sort fonts AMT over Albany... over MS over Nimbus over bitmap

    s/(Helvetica;.*)Nimbus Sans L;/Nimbus Sans L;$1/;
    s/(Nimbus Sans L;.*)Arial;/Arial;$1/;
    s/(Arial;.*)Albany;/Albany;$1/;
    s/(Albany;.*)Albany AMT;/Albany AMT;$1/;

    s/(helvetica;.*)nimbussansl;/nimbussansl;$1/;
    s/(nimbussansl;.*)arial;/arial;$1/;
    s/(arial;.*)albany;/albany;$1/;
    s/(albany;.*)albanyamt;/albanyamt;$1/;

    s/(Times;.*)Nimbus Roman No9 L;/Nimbus Roman No9 L;$1/;
    s/(Nimbus Roman No9 L;.*)Times New Roman;/Times New Roman;$1/;
    s/(Times New Roman;.*)Thorndale;/Thorndale;$1/;
    s/(Thorndale;.*)Thorndale AMT;/Thorndale AMT;$1/;

    s/(times;.*)nimbusromanno9l;/nimbusromanno9l;$1/;
    s/(nimbusromanno9l;.*)timesnewroman;/timesnewroman;$1/;
    s/(timesnewroman;.*)thorndale;/thorndale;$1/;
    s/(thorndale;.*)thorndaleamt;/thorndaleamt;$1/;

    s/(Courier;.*)Nimbus Mono L;/Nimbus Mono L;$1/;
    s/(Nimbus Mono L;.*)Courier New;/Courier New;$1/;
    s/(Courier New;.*)Cumberland;/Cumberland;$1/;
    s/(Cumberland;.*)Cumberland AMT;/Cumberland AMT;$1/;

    s/(courier;.*)nimbusmonol;/nimbusmonol;$1/;
    s/(nimbusmonol;.*)couriernew;/couriernew;$1/;
    s/(couriernew;.*)cumberland;/cumberland;$1/;
    s/(cumberland;.*)cumberlandamt;/cumberlandamt;$1/;

# Some RedHat CJK bits

    # Backups for Sung Ti
    s/方正宋体;/方正宋体;ZYSong18030;AR PL SungtiL GB;/g;
    # Backups for Ming Ti
    s/方正明體;/方正明體;AR PL Mingti2L Big5;/g;

    # Korean fixes
    s/SunGulim;/SunGulim;Baekmuk Gulim;/g;
    s/SunBatang;/SunBatang;Baekmuk Batang;/g;
    s/SunDotum;/SunDotum;Baekmuk Dotum;/g;

    # The bitstream fonts never make sense _at all_ they are so metrically odd.
    s/Bitstream Vera Sans;//g;
    s/Bitstream Vera Sans Mono;//g;
    s/Bitstream Vera Serif;//g;

# Some Ubuntu bits

    # push SansSerif to the end of the list, some Tamil fonts don't
    # have all ISO-8859-1 characters defined
    s/SansSerif;(.*)</$1;SansSerif</;

    if ($ENV{OOO_VENDOR} =~ /buntu/) {
      # prefer DejaVu as UI font
      if ($uisans) { s/<value>/<value>DejaVuSans;/; };
      if (/UI_SANS/) { $uisans = 1; } else { $uisans = 0; };
      if ($uifixed) { s/<value>/<value>DejaVuMonoSans;/; };
      if (/UI_FIXED/) { $uifixed = 1; } else { $uifixed = 0; };
    };
