#!/bin/bash

# default font setting script for Vine Linux 7/7CR
# used by tex, gs, xpdf..
#
# usage: update-defaultfont

TEXMF=/var/lib/texmf
PTEXMAPFILE=$TEXMF/fonts/map/dvipdfmx/vine/ptex-vine.map
UPTEXMAPFILE=$TEXMF/fonts/map/dvipdfmx/vine/uptex-vine.map
OTFMAPFILE=$TEXMF/fonts/map/dvipdfmx/vine/otf-vine.map
OTFUPMAPFILE=$TEXMF/fonts/map/dvipdfmx/vine/otf-up-vine.map
TEXALIASDIR=$TEXMF/fonts/truetype/vine
ALIASDIR=/usr/share/fonts/alias/TrueType

TMPPTEXMAPFILE=$(mktemp /tmp/ptex-vine.map.XXXXXX)
TMPUPTEXMAPFILE=$(mktemp /tmp/uptex-vine.map.XXXXXX)
TMPOTFMAPFILE=$(mktemp /tmp/otf-vine.map.XXXXXX)
TMPOTFUPMAPFILE=$(mktemp /tmp/otf-up-vine.map.XXXXXX)

# set default font symlink
lnttf() {
    F=$(fc-match -v "$2" | grep "file:" | cut -f2 -d\")
    ln -sf $F $TEXALIASDIR/$3.ttf
    ln -sf $F $ALIASDIR/$3.ttf
    cat <<EOF >> $TMPPTEXMAPFILE
% $1 %
$1	H	$3.ttf
t$1	V	$3.ttf
$1i	H	$3.ttf -s .3
t$1i	V	$3.ttf -s .3

EOF
}

# make symlink for CR fonts
lnttc() {
    F=$(find /usr/share/fonts/ -name "$2.ttc" -type f | head -1)
    if [ -f "$F" ]; then
      ln -sf $F $TEXALIASDIR/$3.ttc
      cat <<EOF >> $TMPPTEXMAPFILE
% $3 %
$1   	H	$3.ttc
t$1   	V	$3.ttc
$1i   	H	$3.ttc -s .3
t$1i   	V	$3.ttc -s .3

EOF
    fi
}

########################################################################################

cat <<EOF > $TMPPTEXMAPFILE
% Vine additional map %
rml	2004-H	Mincho-Medium.ttf
rmlv	2004-V	Mincho-Medium.ttf
gbm	2004-H	Gothic-Medium.ttf
gbmv	2004-V	Gothic-Medium.ttf

% italic %
rmli	2004-H	Mincho-Medium.ttf,Italic
rmlvi	2004-V	Mincho-Medium.ttf,Italic
gbmi	2004-H	Gothic-Medium.ttf,Italic
gbmvi	2004-V	Gothic-Medium.ttf,Italic

% new JIS font metric %
rmln	2004-H	Mincho-Medium.ttf
rmlnv	2004-V	Mincho-Medium.ttf
gbmn	2004-H	Gothic-Medium.ttf
gbmnv	2004-V	Gothic-Medium.ttf

EOF

cat <<EOF > $TMPUPTEXMAPFILE
urml		UniJIS2004-UTF16-H	Mincho-Medium.ttf
urmlv		UniJIS2004-UTF16-V	Mincho-Medium.ttf
ugbm		UniJIS2004-UTF16-H	Gothic-Medium.ttf
ugbmv		UniJIS2004-UTF16-V	Gothic-Medium.ttf

uprml-h		UniJIS2004-UTF16-H	Mincho-Medium.ttf
uprml-v		UniJIS2004-UTF16-V	Mincho-Medium.ttf
upgbm-h		UniJIS2004-UTF16-H	Gothic-Medium.ttf
upgbm-v		UniJIS2004-UTF16-V	Gothic-Medium.ttf
uprml-hq	UniJIS-UCS2-H	Mincho-Medium.ttf
upgbm-hq	UniJIS-UCS2-H	Gothic-Medium.ttf

EOF

cat<<EOF > $TMPOTFMAPFILE
% TEXT, 90JIS
hminl-h	H	Mincho-Medium.ttf
hminl-v	V	Mincho-Medium.ttf
hminr-h	H	Mincho-Medium.ttf
hminr-v	V	Mincho-Medium.ttf
hminb-h	H	FutoMin-Bold.ttf
hminb-v	V	FutoMin-Bold.ttf
hgothr-h	H	Gothic-Medium.ttf
hgothr-v	V	Gothic-Medium.ttf
hgothb-h	H	FutoGo-Bold.ttf
hgothb-v	V	FutoGo-Bold.ttf
hgotheb-h	H	FutoGo-Bold.ttf
hgotheb-v	V	FutoGo-Bold.ttf
hmgothr-h	H	MaruGo-Medium.ttf
hmgothr-v	V	MaruGo-Medium.ttf

% TEXT, JIS04
hminln-h	2004-H	Mincho-Medium.ttf
hminln-v	2004-V	Mincho-Medium.ttf
hminrn-h	2004-H	Mincho-Medium.ttf
hminrn-v	2004-V	Mincho-Medium.ttf
hminbn-h	2004-H	FutoMin-Bold.ttf
hminbn-v	2004-V	FutoMin-Bold.ttf
hgothrn-h	2004-H	Gothic-Medium.ttf
hgothrn-v	2004-V	Gothic-Medium.ttf
hgothbn-h	2004-H	FutoGo-Bold.ttf
hgothbn-v	2004-V	FutoGo-Bold.ttf
hgothebn-h	2004-H	FutoGo-Bold.ttf
hgothebn-v	2004-V	FutoGo-Bold.ttf
hmgothrn-h	2004-H	MaruGo-Medium.ttf
hmgothrn-v	2004-V	MaruGo-Medium.ttf

% CID
otf-cjml-h	Identity-H	Mincho-Medium.ttf
otf-cjml-v	Identity-V	Mincho-Medium.ttf
otf-cjmr-h	Identity-H	Mincho-Medium.ttf
otf-cjmr-v	Identity-V	Mincho-Medium.ttf
otf-cjmb-h	Identity-H	FutoMin-Bold.ttf
otf-cjmb-v	Identity-V	FutoMin-Bold.ttf
otf-cjgr-h	Identity-H	Gothic-Medium.ttf
otf-cjgr-v	Identity-V	Gothic-Medium.ttf
otf-cjgb-h	Identity-H	FutoGo-Bold.ttf
otf-cjgb-v	Identity-V	FutoGo-Bold.ttf
otf-cjge-h	Identity-H	FutoGo-Bold.ttf
otf-cjge-v	Identity-V	FutoGo-Bold.ttf
otf-cjmgr-h	Identity-H	MaruGo-Medium.ttf
otf-cjmgr-v	Identity-V	MaruGo-Medium.ttf

% Unicode 90JIS
otf-ujml-h	UniJIS-UTF16-H	Mincho-Medium.ttf
otf-ujml-v	UniJIS-UTF16-V	Mincho-Medium.ttf
otf-ujmr-h	UniJIS-UTF16-H	Mincho-Medium.ttf
otf-ujmr-v	UniJIS-UTF16-V	Mincho-Medium.ttf
otf-ujmb-h	UniJIS-UTF16-H	FutoMin-Bold.ttf
otf-ujmb-v	UniJIS-UTF16-V	FutoMin-Bold.ttf
otf-ujgr-h	UniJIS-UTF16-H	Gothic-Medium.ttf
otf-ujgr-v	UniJIS-UTF16-V	Gothic-Medium.ttf
otf-ujgb-h	UniJIS-UTF16-H	FutoGo-Bold.ttf
otf-ujgb-v	UniJIS-UTF16-V	FutoGo-Bold.ttf
otf-ujge-h	UniJIS-UTF16-H	FutoGo-Bold.ttf
otf-ujge-v	UniJIS-UTF16-V	FutoGo-Bold.ttf
otf-ujmgr-h	UniJIS-UTF16-H	MaruGo-Medium.ttf
otf-ujmgr-v	UniJIS-UTF16-V	MaruGo-Medium.ttf

% Unicode JIS04
otf-ujmln-h	UniJIS2004-UTF16-H	Mincho-Medium.ttf
otf-ujmln-v	UniJIS2004-UTF16-V	Mincho-Medium.ttf
otf-ujmrn-h	UniJIS2004-UTF16-H	Mincho-Medium.ttf
otf-ujmrn-v	UniJIS2004-UTF16-V	Mincho-Medium.ttf
otf-ujmbn-h	UniJIS2004-UTF16-H	FutoMin-Bold.ttf
otf-ujmbn-v	UniJIS2004-UTF16-V	FutoMin-Bold.ttf
otf-ujgrn-h	UniJIS2004-UTF16-H	Gothic-Medium.ttf
otf-ujgrn-v	UniJIS2004-UTF16-V	Gothic-Medium.ttf
otf-ujgbn-h	UniJIS2004-UTF16-H	FutoGo-Bold.ttf
otf-ujgbn-v	UniJIS2004-UTF16-V	FutoGo-Bold.ttf
otf-ujgen-h	UniJIS2004-UTF16-H	FutoGo-Bold.ttf
otf-ujgen-v	UniJIS2004-UTF16-V	FutoGo-Bold.ttf
otf-ujmgrn-h	UniJIS2004-UTF16-H	MaruGo-Medium.ttf
otf-ujmgrn-v	UniJIS2004-UTF16-V	MaruGo-Medium.ttf

EOF

cat<<EOF > $TMPOTFUPMAPFILE

% TEXT, 90JIS
uphminl-h	UniJIS-UTF16-H	Mincho-Medium.ttf
uphminl-v	UniJIS-UTF16-V	Mincho-Medium.ttf
uphminr-h	UniJIS-UTF16-H	Mincho-Medium.ttf
uphminr-v	UniJIS-UTF16-V	Mincho-Medium.ttf
uphminb-h	UniJIS-UTF16-H	FutoMin-Bold.ttf
uphminb-v	UniJIS-UTF16-V	FutoMin-Bold.ttf
uphgothr-h	UniJIS-UTF16-H	Gothic-Medium.ttf
uphgothr-v	UniJIS-UTF16-V	Gothic-Medium.ttf
uphgothb-h	UniJIS-UTF16-H	FutoGo-Bold.ttf
uphgothb-v	UniJIS-UTF16-V	FutoGo-Bold.ttf
uphgotheb-h	UniJIS-UTF16-H	FutoGo-Bold.ttf
uphgotheb-v	UniJIS-UTF16-V	FutoGo-Bold.ttf
uphmgothr-h	UniJIS-UTF16-H	MaruGo-Medium.ttf
uphmgothr-v	UniJIS-UTF16-V	MaruGo-Medium.ttf

% TEXT, JIS04
uphminln-h	UniJIS2004-UTF16-H	Mincho-Medium.ttf
uphminln-v	UniJIS2004-UTF16-V	Mincho-Medium.ttf
uphminrn-h	UniJIS2004-UTF16-H	Mincho-Medium.ttf
uphminrn-v	UniJIS2004-UTF16-V	Mincho-Medium.ttf
uphminbn-h	UniJIS2004-UTF16-H	FutoMin-Bold.ttf
uphminbn-v	UniJIS2004-UTF16-V	FutoMin-Bold.ttf
uphgothrn-h	UniJIS2004-UTF16-H	Gothic-Medium.ttf
uphgothrn-v	UniJIS2004-UTF16-V	Gothic-Medium.ttf
uphgothbn-h	UniJIS2004-UTF16-H	FutoGo-Bold.ttf
uphgothbn-v	UniJIS2004-UTF16-V	FutoGo-Bold.ttf
uphgothebn-h	UniJIS2004-UTF16-H	FutoGo-Bold.ttf
uphgothebn-v	UniJIS2004-UTF16-V	FutoGo-Bold.ttf
uphmgothrn-h	UniJIS2004-UTF16-H	MaruGo-Medium.ttf
uphmgothrn-v	UniJIS2004-UTF16-V	MaruGo-Medium.ttf

EOF

## make these directories if these do not exists
mkdir -p $TEXALIASDIR $ALIASDIR

## remove broken symbolic links
rm -f $TEXALIASDIR/*
rm -f $ALIASDIR/*
#find -L $TEXALIASDIR -type l -exec rm -f \{\} \;
#find -L $ALIASDIR -type l -exec rm -f \{\} \;

## alias font
#     tfm name	fontconfig alias		PostScript name
lnttf min	Serif				Mincho-Medium
lnttf goth	Sans				Gothic-Medium
lnttf futomin	FutoMin				FutoMin-Bold
lnttf futogo	FutoGo				FutoGo-Bold
lnttf marugo	Jun				MaruGo-Medium

# make symlink for CR fonts
for i in /usr/share/vine/fonts.d/*
do
  if echo $file | grep -q "lnttc-.*\.sh$" ; then
    . $i
  fi
done

#
cp -f $TMPPTEXMAPFILE $PTEXMAPFILE
cp -f $TMPUPTEXMAPFILE $UPTEXMAPFILE
cp -f $TMPOTFMAPFILE $OTFMAPFILE
cp -f $TMPOTFUPMAPFILE $OTFUPMAPFILE
rm -f $TMPPTEXMAPFILE $TMPUPTEXMAPFILE $TMPOTFMAPFILE $TMPOTFUPMAPFILE
chmod 644 $PTEXMAPFILE $UPTEXMAPFILE $OTFMAPFILE $OTFUPMAPFILE
