#!/bin/sh

# lintian_pdeb  version 0.4
# a hook script to run lintian (from a pdebuild environment)
#
# Copyright (c) 2009-2012 Francesco Poli
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Changed by Michael Franzl:
# 0.3->0.4 lintian user changed to nobody

LC_ALL=C
export LC_ALL

TMP_LOG=$(mktemp --tmpdir aptitude-log.XXXXXX)
APTITUDE_TLOG="aptitude -o Aptitude::Log=$TMP_LOG"
AOPTS="-q -y"
$APTITUDE_TLOG $AOPTS install awk dpkg-dev lintian
TO_BE_PURGED=$(awk -F] '/INSTALL/ { print $2 ; }' ${TMP_LOG})
cat $TMP_LOG >> /var/log/aptitude
rm -f $TMP_LOG

PKG_DIR=$(readlink -f ${HOME}/*/debian/..)
if test $? -ne 0
then
    echo " => ERROR! found the following debianized source directories:"
    for DSD in ${HOME}/*/debian/..
    do
        readlink -f $DSD
    done
    echo "(there should be exactly one, instead)"
    exit 22
fi

cd $PKG_DIR
PKG=$(dpkg-parsechangelog |
      awk '/^Source: /  { src  = $2 ; }
           /^Version: / { vers = $2 ; print src "_" vers ; }')
ARCH=$(dpkg --print-architecture)
CHANGES_FILE=${PKG}_${ARCH}.changes

echo " => performing ftp-master-rejects lintian checks on $CHANGES_FILE"
su -p nobody -c "lintian -viF ${HOME}/$CHANGES_FILE"

echo " => performing general and experimental lintian checks on $CHANGES_FILE"
su -p nobody -c "lintian -EviIL +pedantic ${HOME}/$CHANGES_FILE"

echo " => cleaning up"
aptitude $AOPTS --purge-unused purge $TO_BE_PURGED