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

BEGIN { $ENV{OOO_VENDOR} ||= "unknown"; $helpsans = 0; $helpfixed = 0; }

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

    # move Bitstream Vera fonts after better fonts
    s/\"Bitstream Vera Sans\"\,(.*Albany\,)/$1\"Bitstream Vera Sans\"\,/;
    s/\"Bitstream Vera Sans\"\,(.*\"Albany AMT\"\,)/$1\"Bitstream Vera Sans\"\,/;
    s/\"Bitstream Vera Sans\"\,(.*Arial\,)/$1\"Bitstream Vera Sans\"\,/;
    s/\"Bitstream Vera Sans\"\,(.*\"Nimbus Sans L\"\,)/$1\"Bitstream Vera Sans\"\,/;

    s/\"Bitstream Vera Sans Mono\"\,(.*Cumberland\,)/$1\"Bitstream Vera Sans Mono\"\,/;
    s/\"Bitstream Vera Sans Mono\"\,(.*\"Courier New\"\,)/$1\"Bitstream Vera Sans Mono\"\,/;
    s/\"Bitstream Vera Sans Mono\"\,(.*\"Cumberland AMT\"\,)/$1\"Bitstream Vera Sans Mono\"\,/;
    s/\"Bitstream Vera Sans Mono\"\,(.*\"Nimbus Mono L\"\,)/$1\"Bitstream Vera Sans Mono\"\,/;

    s/\"Bitstream Vera Serif\"\,(.*Thorndale\,)/$1\"Bitstream Vera Serif\"\,/;
    s/\"Bitstream Vera Serif\"\,(.*\"Thorndale AMT\"\,)/$1\"Bitstream Vera Serif\"\,/;
    s/\"Bitstream Vera Serif\"\,(.*\"Times New Roman\"\,)/$1\"Bitstream Vera Serif\"\,/;
    s/\"Bitstream Vera Serif\"\,(.*\"Nimbus Roman No9 L\"\,)/$1\"Bitstream Vera Serif\"\,/;

    # make sure Albany, Cumberland, and Thorndale are there
    (m/Albany,/)     || s/Arial,/Albany,Arial,/g;
    (m/Cumberland,/) || s/\"Courier New\",/Cumberland,\"Courier New\",/g;
    (m/Thorndale,/)  || s/\"Times New Roman\",/Thorndale,\"Times New Roman\",/g;

    # add AMT fonts
    (m/\"Albany AMT\",/)     || s/Albany,/\"Albany AMT\",Albany,/g;
    (m/\"Cumberland AMT\",/) || s/Cumberland,/\"Cumberland AMT\",Cumberland,/g;
    (m/\"Thorndale AMT\",/)  || s/Thorndale,/\"Thorndale AMT\",Thorndale,/g;

    # add URW fonts
    (m/\"Nimbus Sans L\",/)      || s/Arial,/Arial,\"Nimbus Sans L\",/g;
    (m/\"Nimbus Mono L\",/)      || s/\"Courier New\",/\"Courier New\",\"Nimbus Mono L\",/g;
    (m/\"Nimbus Roman No9 L\",/) || s/\"Times New Roman\",/\"Times New Roman\",\"Nimbus Roman No9 L\",/g;

    # prune duplicates
    s/Albany,(.*)Albany,/Albany,$1/;
    s/Cumberland,(.*)Cumberland,/Cumberland,$1/;
    s/Thorndale,(.*)Thorndale,/Thorndale,$1/;

    s/\"Albany AMT\",(.*)\"Albany AMT\",/\"Albany AMT\",$1/;
    s/\"Cumberland AMT\",(.*)\"Cumberland AMT\",/\"Cumberland AMT\",$1/;
    s/\"Thorndale AMT\",(.*)\"Thorndale AMT\",/\"Thorndale AMT\",$1/;

    s/\"Nimbus Sans L\",(.*)\"Nimbus Sans L\",/\"Nimbus Sans L\",$1/;
    s/\"Nimbus Mono L\",(.*)\"Nimbus Mono L\",/\"Nimbus Mono L\",$1/;
    s/\"Nimbus Roman No9 L\",(.*)\"Nimbus Roman No9 L\",/\"Nimbus Roman No9 L\",$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/(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/(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/;

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

    if ($ENV{OOO_VENDOR} =~ /buntu/) {
      # prefer DejaVu for the Help
      if ($helpsans) { s/font-family: */font-family: DejaVuSans,/; };
      if (/^body/) { $helpsans = 1; } else { $helpsans = 0; };
      if ($helpfixed) { s/font-family: */font-family: DejaVuMonoSans,/; };
      if (/^\.code/) { $helpfixed = 1; } else { $helpfixed = 0; };
    };
