#!/bin/bash

in_array()
{
    local i needle
    needle=$1
    shift 1
    # array() undefined
    [ -z "$1" ] && return 1
    for i in $*
    do
        echo $i |grep -q "^$needle" && return 0
    done
    return 1
}

if [ "$1" == "--help" ]; then
	echo "Missing kde applications packages"
	exit 1
fi


if [ ! -d ../.git -o ! -d ../source ]; then
	echo "hey, where are you?"
	exit 1
fi

cd ..

ignore=(
'kirigami-gallery'
)

	source source/include/kf5-version.sh

	upstream=$(for i in applications
	do
		lynx -dump https://download.kde.org/stable/$i/$_F_kdever_apps/src|grep xz$|sed 's|.*/\(.*\)-[0-9].*|\1|'
	done |sort -u |tr '[A-Z]' '[a-z]')

	for j in $upstream
	do
		if [ -d source/*/$j ] || [ -d source/*/${j}5 ]
		then
			continue
		fi
		if in_array $j ${ignore[@]}
		then
			continue
		fi

		cmakelists_in_branch=$(lynx -dump "https://cgit.kde.org/${j}.git/tree/CMakeLists.txt?h=v${_F_kdever_apps}")
		if $(echo $cmakelists_in_branch | egrep -q -i -v kf5) || $(echo $cmakelists_in_branch | egrep -q -i "invalid branch")
		then
			continue
		fi
		echo $j
	done
