# Calculate comment=# protected
# vim: set syntax=sh
#
# Copyright 2014-2015 Calculate Ltd. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
CL_UPDATE_PROG=/usr/sbin/cl-core
if [[ -f /usr/lib64/python2.7/site-packages/calculate/core/wsdl_core.py ]]
then
	RUN_CORE="/usr/bin/python2 ${CL_UPDATE_PROG}"
else
	RUN_CORE="/usr/bin/python3 ${CL_UPDATE_PROG}"
fi
CORE_METHOD="--method core_setup"

post_setup_package() {
	local remove_dir_list=""
	if [ -f ${CL_UPDATE_PROG} ] && [[ $PN == "calculate-utils" ]] && [[ -f /var/lib/calculate/calculate-core/setup-failed.list ]]
	then
		while read category pkgname mypvr myslot;do
			if [[ -n $category ]]
			then
				pkg_lockfile_dir=/var/calculate/tmp/portage/$category
				pkg_lockfile="$pkg_lockfile_dir"/.${pkgname}.calculate_lockfile
				if ! [[ -d $pkg_lockfile_dir ]]
				then
					mkdir -p $pkg_lockfile_dir
					remove_dir_list="$remove_dir_list $pkg_lockfile_dir"
				fi
				(
					flock -x -w 1 73 || exit 1;
					fuser /var/db/pkg/$category/.${pkgname}*.portage_lockfile &>/dev/null || CTARGET=${CTARGET} GCC_CONFIG_VER=${GCC_CONFIG_VER} ${RUN_CORE} ${CORE_METHOD} --no-progress --pkg-name $pkgname --pkg-category $category --pkg-slot $myslot --pkg-version $mypvr --pkg-path "/";
					rm -f /var/lib/calculate/-runlevels-$pkgname
					rm $pkg_lockfile
				) 73>$pkg_lockfile
			fi
		done </var/lib/calculate/calculate-core/setup-failed.list
		rm -f /var/lib/calculate/calculate-core/setup-failed.list
	fi

	if [ -f ${CL_UPDATE_PROG} ] && [[ -f  /var/lib/calculate/-postmerge ]] && check_skip
	then
		OLDIFS=$IFS
		IFS=/
		while read category pkgname;do
			[ -d /var/db/pkg/${category}/${pkgname}-* ] || continue
			IFS=$OLDIFS
			if ! [[ -f /var/lib/calculate/calculate-core/cache/merge-setup.list ]] ||
				grep ${category}/${pkgname} /var/lib/calculate/calculate-core/cache/merge-setup.list &>/dev/null
			then
				pkg_lockfile_dir=/var/calculate/tmp/portage/$category
				pkg_lockfile="$pkg_lockfile_dir"/.${pkgname}.calculate_lockfile
				if ! [[ -d $pkg_lockfile_dir ]]
				then
					mkdir -p $pkg_lockfile_dir
					remove_dir_list="$remove_dir_list $pkg_lockfile_dir"
				fi
				(
					flock -x -w 1 73 || exit 1;
					fuser /var/db/pkg/$category/.${pkgname}*.portage_lockfile &>/dev/null || CTARGET=${CTARGET} GCC_CONFIG_VER=${GCC_CONFIG_VER} ${RUN_CORE} ${CORE_METHOD} --no-progress --pkg-name $pkgname --pkg-category $category --pkg-path "/";
					rm $pkg_lockfile
				) 73>$pkg_lockfile
			fi
			IFS=/
		done < <(awk '!x[$0]++' /var/lib/calculate/-postmerge)
		IFS=$OLDIFS
	fi
	rm -f /var/lib/calculate/-postmerge
	[[ -n $remove_dir_list ]] && rmdir $remove_dir_list
}

fix_kernel_symlink() {
	# restore kernel symlink to preferred by cl_install_kernel_build variable
	local kernel_build=$(/usr/libexec/calculate/cl-variable --value install.cl_install_kernel_build 2>/dev/null)
	if [[ $? -ne 1 ]] && [[ -n $kernel_build ]] && [[ -L /usr/src/linux ]]
	then
		[[ "$kernel_build" =~ ^[0-9]+\.[0-9]+$ ]] && kernel_build="${kernel_build}."
		last_kernel="$(ls -1d /usr/src/linux-${kernel_build}* | sed 's/.*linux-//' | sort -V | tail -1)"
		if [[ -n $last_kernel ]]
		then
			if [[ "$(readlink /usr/src/linux)" != "linux-${last_kernel}" ]]
			then
				rm /usr/src/linux
				ln -sf linux-${last_kernel} /usr/src/linux
			fi
		fi
	fi
}

clean_binary_bdepends() {
	local bdeps=$(/usr/libexec/calculate/cl-variable --value update.cl_update_with_bdeps_set 2>/dev/null)
	local _instmark=/var/db/pkg/$CATEGORY/$PF/EMERGE_FROM
	echo -n $EMERGE_FROM >$_instmark
	
	if [[ $EMERGE_FROM == binary ]] && [[ $bdeps == auto ]]
	then
		local _bdep=/var/db/pkg/$CATEGORY/$PF/BDEPEND
		local _dep=/var/db/pkg/$CATEGORY/$PF/DEPEND
		[[ -f $_bdep ]] && mv $_bdep $_bdep.binary
		[[ -f $_dep ]] && mv $_dep $_dep.binary
	fi
}

post_pkg_postinst() {
	[[ "$PN" == "calculate-sources" ]] && fix_kernel_symlink
	post_setup_package
	calculate_unlock_setup
	clean_binary_bdepends
}
