#!/usr/local/bin/bash
# $Id: frontend.in,v 1.9 2003/01/08 19:45:14 jmmv Exp $
# bt_lint's frontend.
#
# buildtool
# Copyright (c) 2002, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

if [ x"$__BUILDTOOL" != x"yes" ]; then
    echo "bt_lint: this program must be run through buildtool"
    exit 1
fi

check_rootfiles() {
    echo "=> Checking root files"
    if [ ! -f "README.bt" ]; then
        warn "README.bt not found; it is highly recommended"
    else
        grep -v "\$Id.*\$" /usr/local/share/buildtool-0/templates/README.bt > test.1
        grep -v "\$Id.*\$" README.bt > test.2
        _res=`cmp test.1 test.2`
        rm -f test.1 test.2
        if [ -n "$_res" ]; then
            warn "README.bt found but it is outdated or modified"
        fi
    fi
    if [ ! -f "CHANGES" ]; then
        warn "CHANGES not found; it is recommended"
    fi
    if [ ! -f "PEOPLE" ]; then
        warn "PEOPLE not found; it is recommended"
    fi
    if [ ! -f "README" ]; then
        warn "file README not found; it is recommended"
    fi
    if [ -f "AUTHORS" ]; then
        warn "file AUTHORS found; PEOPLE is suggested"
    fi
    if [ -f "THANKS" ]; then
        warn "file THANKS found; PEOPLE is suggested"
    fi
}

check_defs() {
    echo "=> Checking buildtool.d/defs"
    if [ -z "$BT_REQUIRE" ]; then
        fatal "no Buildtool version explicitly required"
    fi
    if [ -z "$BT_PKG_NAME" ]; then
        fatal "no package name"
    fi
    if [ -z "$BT_PKG_VERSION" ]; then
        fatal "no package version"
    fi
    if [ -z "$BT_PKG_LICENSE" ]; then
        warn "no package license"
    fi
    if [ -z "$BT_PKG_COMMENT" ]; then
        warn "no package comment"
    fi
    if [ -z "$BT_PKG_MAINTAINER" ]; then
        warn "no package maintainer"
    fi
    if [ -z "$BT_PKG_HOMEPAGE" ]; then
        warn "no package homepage"
    fi
}

check_mk() {
    local res
    echo "=> Checking Makefile.bt's"
    if [ -f "Makefile.bt" ]; then
        res=`grep ".include <bt.*.mk>" Makefile.bt`
        if [ -n "$res" ]; then
            res=`grep "^bt_generate_configmk" buildtool.d/config`
            if [ -z "$res" ]; then
                fatal "bt_generate_configmk is required for bt_make operation"
            fi
        fi
    fi
}

fatal() {
    echo "FATAL: $*"
    Fatal=$(($Fatal + 1))
}

warn() {
    echo "WARN: $*"
    Warn=$(($Warn + 1))
}

if [ ! -f "./buildtool.d/defs" ]; then
    echo "bt_lint: cannot find buildtool.d/defs file"
    exit 1
fi

. ./buildtool.d/defs

Fatal=0
Warn=0

check_rootfiles
check_defs
check_mk

echo "=> Summary"

if [ $Fatal -gt 0 ]; then
    echo "Package is INVALID; $Fatal fatal errors"
    exit 1
fi

if [ $Warn -gt 0 ]; then
    echo "Package should be corrected; $Warn warning messages."
    echo "Note that warning messages can be IGNORED."
    exit 0
fi

echo "Package is OK!"

exit 0

# Local Variables: ***
# mode: shell-script ***
# End: ***
