#!/bin/bash

#    This file is part of vlbuildbot.
#
#    vlbuildbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    vlbuildbot 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.
#
#    See http://www.gnu.org/licenses/ for complete licensing terms.

SB=$(basename $1)
APP=$(basename $SB .SlackBuild)

if [ ! -z $REPO ]; then
	SBREPO=$REPO/var/vabs
else
	SBREPO="/home/slackbuilds/var/vabs"
fi

DEPS=$(grep ^MAKEDEPENDS $1|cut -d'"' -f2) || exit 1
BUILDLOC=/tmp/builds


# 1 arg = sbpath
function can_source() {
    # echoes "YES" if the given SlackBuild can be sourced.
    # echoes "NO" otherwise.
    # ARGUMENTS:
		#1. sbpath = path to slackbuild file
	_sbpath=$1
    res="NO"
    guess="$(grep "NORUN" ${_sbpath})"
    if [ ! "x$guess" = "x" ]; then
		for line in $guess; do
			first="${line:0:1}"
			if [ "$first" != "#" ]; then
				res="YES"
				break
			fi
		done
    else
		res="NO"
    fi
    echo $res

}

function list_deps() {
    sb=$1
	if [ "$(can_source $sb)" = "YES" ]; then
	# we can source this SB
	NORUN=1 source $sb
	echo $MAKEDEPENDS
        return 0
    else
	deps=$(grep ^MAKEDEPENDS $sb|cut -d'"' -f2) || return 1
        echo $deps
	return 0
    fi
}

function refresh_repo_data() {
    slapt-get -u
    return $?
}

function install_dep_from_repo() {
    dep=$1
    slapt-get -y -i $dep
    return $?
}

function install_dep_from_source() {
    dep=$1
    if [ "$dep" = "$BUILDER" ]; then
	echo ""
	echo "===> $0 ERROR:  Redundant dependency (dep hell) detected in $BUILDER dependency sequence. <===" >&2
	echo ""
	return 1
    fi
#	echo "===> Building $dep dependency for $BUILDER <==="
    if [ -d $SBREPO/$dep ]; then
	# copy source dir to the build location
	cp -ar $SBREPO/$dep /tmp/builds || return 1
	cd /tmp/builds/$dep/src || return 1
	echo "==> Building $dep from source ..." >&2
	# recursive resolve deps
	# call vldepper again
	# This time, we can use the repos... we are only doing 1st level of deps from source
	GROUPBUILD=NO vldepper /tmp/builds/$dep/src/$dep.SlackBuild || return 1
	sh ./$dep.SlackBuild || return 1
	installpkg /tmp/builds/$dep/$dep*.t?z || return 1
    fi
    return 0
}

function uninstall_dep() {
# Uninstall a package... used when the SB requests a conflicting package to be removed
    dep=$1
    removepkg $dep
    return 0
}
 

function process_deps() {
    sbpath=$1
    deps=$(list_deps $sbpath)
    if [ "x$deps" != "x" ]; then
	echo ""
	echo "$sbpath requests the following dependencies:"
	for item in $deps; do
		echo "$item"
	done
	echo ;
	echo ""
	# Check if we want a full rebuild from source
	if [ "$GROUPBUILD" == "TRUE" ]; then
		echo "GROUPBUILD is set to $GROUPBUILD.  Forcing first-level deps to be built from source"
		echo ""
	    # We want to force builds from source straight up
	    for dep in $deps; do
		if [ ${dep:0:1} == '!' ]; then
			pkg=$(echo ${dep} | sed 's/!//g')
			echo "$sbpath requested to remove $pkg" >&2
			uninstall_dep $pkg

		elif [ -f $BUILDLOC/$dep/$dep-*.t?z ]; then
		    echo "==> Installing previously built $dep package"
		    installpkg $BUILDLOC/$dep/$dep-*.t?z
		elif [ -d $SBREPO/$dep ]; then
		    cp -ar $SBREPO/$dep /tmp/builds || return 1
		    install_dep_from_source $dep
		else
		    echo "Unable to fulfill $dep dependancy.  Cannot continue" >&2
		    return 1
		fi
	    done
	    return 0
	fi

	# Try to solve it with repos
	refresh_repo_data

	for dep in $deps; do
	    if [ ${dep:0:1} == '!' ]; then
		pkg=$(echo ${dep} | sed 's/!//g')
		echo "$sbpath requested to remove $pkg" >&2
		removepkg $pkg
	    else
		    echo "==> installing $dep from online repos"
#		    Process="slapt-get -y -i $dep"
#		    $Process &
#		    PID=$!
#		    trap "kill -9 $PID;exit" SIGTERM
#		    wait $PID
		    slapt-get -y -i $dep
		    if [ $? != 0 ]; then
			    echo " -- $dep not found in online repos" >&2
			    # try the local build dir to see if we have built this.
			    if [ -f $BUILDLOC/$dep/$dep*.t?z ]; then
				    echo ""
				    echo "==> installing previously built $dep package"
				    echo ""
				    installpkg $BUILDLOC/$dep/$dep*.t?z
			    # copy the source tree to the build dir
			    elif [ -d $SBREPO/$dep ]; then
				    cp -ar $SBREPO/$dep /tmp/builds/ || return 1
				    echo "<==> Building $dep from source code..." >&2
				    install_dep_from_source $dep
				    if [ $? != 0 ]; then
					    echo "-- Unable to resolve $dep dependancy for $sbpath" >&2
					    return 1
				    fi
			    else
				    echo "> Unable to resolve $dep dependancy for $sbpath" >&2
				    echo "> Package $dep cannot be found in the repos or source tree" >&2
				    return 1
			    fi
		    fi

	    fi
    	done
	else
		echo ""
		echo "No dependency information found on $sbpath" >&2
		echo ""
   fi
return 0
}

process_deps $1

