#!/bin/bash
#
#   fblint for Frugalware
#
#   Copyright (c) 2006, 2007, 2008, 2009, 2010 - 2016 by Miklos Vajna <vmiklos@frugalware.org>
#
#   This program 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 program 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 program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

myver="0.6.5"

die()
{
	echo "$0: $@"
	exit 1
}

check()
{
	[ "$_fblint_verbose" ] && echo -n "checking for $1... "
	eval $2
	if [ "$?" = 0 ]; then
		[ "$_fblint_verbose" ] && echo "done."
		_fblint_done=$(($_fblint_done+1))
	else
		[ ! "$_fblint_verbose" -a ! "$_fblint_quiet" ] && echo -n "checking for $1... "
		[ "$_fblint_quiet" ] || echo "failed."
		_fblint_failed=$(($_fblint_failed+1))
	fi
}

get_root()
{
	local i _scm

	i=`git rev-parse --git-dir 2>/dev/null`
	if [ -n "$i" ]; then
		echo $(basename $(dirname $i))
		_scm=git
		return
	fi
	i=`pwd`
	while true
	do
		if [ -e "$i/_darcs" ]; then
			break
		elif [ "$i" == "" ]; then
			break
		fi
		i=`echo $i|sed 's|\(.*\)/.*|\1|'`
	done
	echo $i
	_scm=darcs
}

if [ "$1" = "--version" ]; then
	echo "fblint (pacman-tools) $myver"
	exit 0
fi

if [ "$1" = "--help" ]; then
	man fblint
	exit 0
fi

if [ "$1" = "-q" ]; then
	_fblint_quiet="y"
	shift 1
fi

if [ "$1" = "-v" ]; then
	_fblint_verbose="y"
	shift 1
fi

if [ "$1" = "-p" ]; then
	_fblint_fb="$2"
else
	_fblint_fb="FrugalBuild"
fi

_fblint_done="0"
_fblint_failed="0"
CHROOT=1
if [ -e /usr/lib/frugalware/fwmakepkg ]; then
	. /usr/lib/frugalware/fwmakepkg
else
	echo "Error! Missing /usr/lib/frugalware/fwmakepkg"
	exit 1
fi

[ "$_fblint_verbose" ] && echo -n "Checking if FrugalBuild exists ... "
if [ -e $_fblint_fb ]; then
       [ "$_fblint_verbose" ] && echo "OK!"
else
       echo "Error! File to check does not exist!"
       exit 1
fi

export LC_ALL="C"

check "no last modified line" "! grep -q -i '^# Last Modified: ' $_fblint_fb"
check "sbu" "grep -i -q \"^# Compiling Time: [~0-9\.]\+ SBU$\" $_fblint_fb"
check "maintainer" "grep -q '^# Maintainer: ' $_fblint_fb"
check "syntax errors" "source $_fblint_fb"
check "pkgname" "[ ! -z \"$pkgname\" ]"
# fst is a special exception for chroot builds
check "parent dir is the same as the first group" "[ $groups == `pwd|sed 's|.*/\(.*\)/.*|\1|'` -o tmp == `pwd|sed 's|.*/\(.*\)/.*|\1|'` ]"
check "dirname is the same as pkgname" "[ $pkgname == `pwd|sed 's|.*/||'` -o fst == `pwd|sed 's|.*/||'` ]"
check "uppercase letters in pkgname" "! echo $pkgname | grep -q '[A-Z]'"
check "pkgver" "[ ! -z \"$pkgver\" ]"
check "hyphen-less pkgver" "[ ! `echo $pkgver | grep '-'` ]"
check "pkgrel" "[ ! -z "$pkgrel" ]"
check "hyphen-less pkgrel" "[ ! `echo $pkgrel | grep '-'` ]"
check "pkgdesc" "[ ! -z \"$pkgdesc\" ]"
check "url" "[ ! -z \"$url\" ]"
check "depends" "[ ! -z \"$depends\" -o ! -z \"$rodepends\" ] || set|grep -q ^depends="
check "buggy empty depends" "[ ! -z \"$depends\" -o ${#depends[@]} == 0 ]"
check "groups" "[ ! -z \"$groups\" ]"
# this var is used internally by gensync/updatesync, FBs should not deal
# with it
check "empty option" "[ -z \"$option\" ]"
check "valid first group" "! echo $groups |grep -q -- - || echo $groups|grep -q -- -extra$"
check "valid options()" "[ -z \"$(echo ${options[@]}|tr ' ' '\n' |grep -v '\(clang\|ldlld\|ldbfd\|ldgold\|ldefs\|nowfsecurity\|noclash\|ldlld\|ldbfd\|ofaster\|ofast\|omedium\|osmall\|odebug\|nodevel\|nolto\|nopie\|plt\|nonow\|noprotector\|norelro\|nofortify\|libtool\|static\|nodocs\|nostrip\|force\|nobuild\|nofakeroot\|scriptlet\|stick\|nomirror\|noversrc\|noccache\|asneeded\|noasneeded\)')\" ]"
check "archs" "[ ! -z \"$archs\" ]"
check "up2date" "test -n \"`echo $up2date|sed 's/\$(/\\\\\$(/g'`\""
_fblint_have_remote_source=n
for i in ${source[@]}
do
	if echo $i|grep -q ://; then
		_fblint_have_remote_source=y
	else
		if git rev-parse --show-cdup &>/dev/null && ! echo ${options[@]}|grep -q nobuild; then
			# we are in a git repo
			check "not downloadable file $i is added to the repo" "git ls-files |grep -q $i"
		fi
	fi
done
if [ "$_fblint_have_remote_source" = "y" ]; then
	check "md5sums/sha1sums/signatures" "[ ${#md5sums[@]} -gt 0 -o ${#sha1sums[@]} -gt 0 -o ${#signatures[@]} -gt 0 ]"
fi
if [ ${#md5sums[@]} -gt 0 ]; then
	check "right number of md5sums" "[ ${#md5sums[@]} -eq ${#source[@]} ]"
fi
if [ ${#sha1sums[@]} -gt 0 ]; then
	check "the need of sha1sums (they are not needed when signatures present)" "[ ${#signatures[@]} -eq 0 ]"
	check "right number of sha1sums" "[ ${#sha1sums[@]} -eq ${#source[@]} ]"
fi
if [ ${#signatures[@]} -gt 0 ]; then
	check "right number of signatures" "[ ${#signatures[@]} -eq ${#source[@]} ]"
fi

if [ ! -z "$subpkgs" ]; then
	for subpkg in "${subpkgs[@]}"
	do
		check "uppercase letters in subpkgs" "! echo $subpkg | grep -q [A-Z]"
	done
	check "subdescs" "[ ! -z \"$subdescs\" ]"
	check "subgroups" "[ ! -z \"$subgroups\" ]"
	check "subarchs" "[ ! -z \"$subarchs\" ]"
	check "right number of subdescs" "[ \"${#subdescs[@]}\" = 0 -o \"${#subdescs[@]}\" == \"${#subpkgs[@]}\" ]"
	check "right number of sublicense" "[ \"${#sublicense[@]}\" = 0 -o \"${#sublicense[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subreplaces" \
		"[ \"${#subreplaces[@]}\" = 0 -o \"${#subreplaces[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subgroups" "[ \"${#subgroups[@]}\" = 0 -o \"${#subgroups[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subarchs" "[ \"${#subarchs[@]}\" = 0 -o \"${#subarchs[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subdepends" "[ \"${#subdepends[@]}\" = 0 -o \"${#subdepends[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subrodepends" \
		"[ \"${#subrodepends[@]}\" = 0 -o \"${#subrodepends[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subremoves" "[ \"${#subremoves[@]}\" = 0 -o \"${#subremoves[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subconflicts" \
		"[ \"${#subconflicts[@]}\" = 0 -o \"${#subconflicts[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subprovides" \
		"[ \"${#subprovides[@]}\" = 0 -o \"${#subprovides[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subbackup" "[ \"${#subbackup[@]}\" = 0 -o \"${#subbackup[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subtriggers" "[ \"${#subtriggers[@]}\" = 0 -o \"${#subtriggers[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of subinstall" "[ \"${#subinstall[@]}\" = 0 -o \"${#subinstall[@]}\" = \"${#subpkgs[@]}\" ]"
	check "right number of suboptions" "[ \"${#suboptions[@]}\" = 0 -o \"${#suboptions[@]}\" = \"${#subpkgs[@]}\" ]"
fi

codename=$(grep ^VERSION $(git rev-parse --show-cdup)/docs/Makefile|sed 's/.*(\(.*\))/\1/'|tr '[A-Z]' '[a-z]')
if [ "`get_root`" = "stable" -o "`get_root`" = "$codename" ]; then
	check "unchanged pkgrel" "echo $pkgrel | grep -q '$codename'"
fi
check "trailing whitespace" "! grep -q ' $' $_fblint_fb"
check "existence of trailing newline" "! cat $_fblint_fb |perl -le '@a=<>; exit \$a[\$#a] =~ /\\n\$/s '?' 1 : 0'"
[ "$_fblint_verbose" ] && echo "done: $_fblint_done, failed $_fblint_failed"

if [ $_fblint_failed -gt 0 ]; then
	exit 1
else
	exit 0
fi
