#!/bin/sh

WINEPREFIX=${WINEPREFIX:-$HOME/.wine}
FONTDIR=$WINEPREFIX/drive_c/windows/fonts

TMPDIR=${TMP:-/tmp}/liberation$$
mkdir -p $TMPDIR

cat > $TMPDIR/ttf2sfd.ff << EOF
Open(\$1)
Save(\$2);
EOF

cat > $TMPDIR/sfd2ttf.ff << EOF
Open(\$1)
Generate(\$2);
EOF

convert_font()
{
    fontforge -script $TMPDIR/ttf2sfd.ff $FONTDIR/$1 $TMPDIR/$1.sfd 2>/dev/null

    cat $TMPDIR/$1.sfd                      | \
    sed "s/^FontName: .*/FontName: $3/"     | \
    sed "s/^FullName: .*/FullName: $4/"     | \
    sed "s/^FamilyName: .*/FamilyName: $5/"   \
    > $TMPDIR/$2.sfd

    fontforge -script $TMPDIR/sfd2ttf.ff $TMPDIR/$2.sfd $FONTDIR/$2 2>/dev/null
}

convert_font "LiberationSans-Regular.ttf"    "Arial.TTF"   "ArialMT"            "Arial"             "Arial"
convert_font "LiberationSans-Italic.ttf"     "Ariali.TTF"  "Arial-ItalicMT"     "Arial Italic"      "Arial"
convert_font "LiberationSans-Bold.ttf"       "Arialbd.TTF" "Arial-BoldMT"       "Arial Bold"        "Arial"
convert_font "LiberationSans-BoldItalic.ttf" "Arialbi.TTF" "Arial-BoldItalicMT" "Arial Bold Italic" "Arial"

convert_font "LiberationSerif-Regular.ttf"    "Times.TTF"   "TimesNewRomanPSMT"            "Times New Roman"             "Times New Roman"
convert_font "LiberationSerif-Italic.ttf"     "Timesi.TTF"  "TimesNewRomanPS-ItalicMT"     "Times New Roman Italic"      "Times New Roman"
convert_font "LiberationSerif-Bold.ttf"       "Timesbd.TTF" "TimesNewRomanPS-BoldMT"       "Times New Roman Bold"        "Times New Roman"
convert_font "LiberationSerif-BoldItalic.ttf" "Timesbi.TTF" "TimesNewRomanPS-BoldItalicMT" "Times New Roman Bold Italic" "Times New Roman"

convert_font "LiberationMono-Regular.ttf"    "Cour.TTF"   "CourierNewPSMT"            "Courier New"             "Courier New"
convert_font "LiberationMono-Italic.ttf"     "Couri.TTF"  "CourierNewPS-ItalicMT"     "Courier New Italic"      "Courier New"
convert_font "LiberationMono-Bold.ttf"       "CourBD.TTF" "CourierNewPS-BoldMT"       "Courier New Bold"        "Courier New"
convert_font "LiberationMono-BoldItalic.ttf" "CourBI.TTF" "CourierNewPS-BoldItalicMT" "Courier New Bold Italic" "Courier New"

rm -rf $TMPDIR
