#! /bin/sh

usage()
{
    echo "Usage: $0 FILE"
    echo "Checks that the package FILE has a copyright file in the appropriate location."
}

compression()
{
    # Gives the compression format of the data tarball of a Debian package,
    # which can be '', gz, xz, bz2 or lzma
    file="$1"
    data="$(ar t "$file" | grep '^data.tar')"
    echo "${data#data.tar.}"
}

if [ $# -ne 1 ]
then
    usage
    exit 2
fi

file="$1"
basename="$(basename "$file" .deb)"
name="${basename%%_*}"
copyright="./usr/share/doc/$name/copyright"

case "$(compression "$file")" in
    gz) tar_opts="$tar_opts -z";;
    xz) tar_opts="$tar_opts -J";;
    bz2) tar_opts="$tar_opts -j";;
    lzma) tar_opts="$tar_opts --lzma";;
esac

if ar p $1 data.tar.gz | tar $tar_opts -t | grep -q "^$copyright\$"
then
    echo "Copyright successfully found at $copyright"
else
    echo "No copyright file found at $copyright"
    exit 1
fi
