#!/bin/sh
# refresh-fonts-data -- Refresh data about font packages in Debian

# Copyright (C) 2008, 2009 Raphael Geissert <atomo64@gmail.com>
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.

set -e

if [ -z "$1" ]; then
    printf "Usage: %s <path-to-data> [<contents>]\n" "$(basename "$0")"
    cat <<INFO

If <contents> is specified, it should be the path to the Contents file
from the current unstable distribution.  It will be used to find all
font files already packaged for Debian and update the list of known
font files and the packages that contain them.  <path-to-data> should
be the path to the root of the Lintian data directory to update.

If the Contents file is not specified, the script will download the
following files from a mirror.  The mirror can be specified with the
DEB_MIRROR environment variable.  If it is not set, the default is
http://i386-geomirror.debian.net/debian.

* Contents-i386.gz

Any necessary special parameters for wget can be set via the
environment variable WGET_ARGS.  The default arguments are -nv.

INFO
    exit
fi

readonly lintian_data="$(readlink -f "$1")"
if [ -n "$2" ] ; then
    contents="$(readlink -f "$2")"
fi

[ -d "$lintian_data" ] || {
    printf "%s is not a directory, aborting" "$lintian_data" >&2
    exit 1
}

readonly workdir="$(mktemp -d)"

cleanup () {
    [ ! -d "$workdir" ] || rm -rf "$workdir"
}; trap cleanup EXIT

mirror="${DEB_MIRROR:=http://i386-geomirror.debian.net/debian}"
WGET_ARGS="${WGET_ARGS:=-nv}"
wget() {
    echo wget "$mirror"/"$1"
    /usr/bin/wget $WGET_ARGS -O "$workdir/$(basename "$1")" "$mirror"/"$1"
}
mkdir -p "$lintian_data/files"

cat > "$workdir/fonts" <<EOF
# The list of known font filenames already packaged for Debian and the
# package that contains the font.  Only packages starting with ttf-,
# otf-, t1-, xfonts- and fonts- are searched for fonts.
#
# Last updated: $(date -u +'%Y-%m-%d')

EOF

if [ -z "$contents" ] ; then
    wget dists/sid/Contents-i386.gz
    contents="$workdir/Contents-i386.gz"
fi
zcat "$contents" \
    | perl -n -w -E 'print lc $_ if (s%^.+/([\w-]+\.(?:[to]tf|pfb))\s+\w+/((?:[to]tf|t1|x?fonts)-[^,]+)(,.+)?$%$1 $2%i);' \
    | LC_ALL=C sort >> "$workdir/fonts"
# Fonts in xfonts-tipa are really shipped by tipa.
sed -i 's/ xfonts-tipa$/ tipa/g' "$workdir/fonts"
mv "$workdir/fonts" "$lintian_data/files/fonts"

# Local Variables:
# indent-tabs-mode: nil
# End:
# vim: syntax=sh sw=4 sts=4 sr et
