#------------------------------------------------------------------------------
# Modified Calculate Utilities 3.7.6.8
# Processed template files:
# /var/db/repos/calculate/profiles/templates/2_ac_install_merge/sys-apps/calculate-utils/profile.bashrc.d/50-setup-package
#------------------------------------------------------------------------------
# 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"
[[ -d /var/lib/calculate ]] || mkdir /var/lib/calculate

# setup package and create postmerge package list
setup_merge_package() {
	rm -f /var/lib/calculate/-postmerge
	if ! [[ -f /var/lib/calculate/calculate-core/cache/merge-setup.list ]] ||
		grep ${CATEGORY}/${PN} /var/lib/calculate/calculate-core/cache/merge-setup.list &>/dev/null
	then
		if [ -f ${CL_UPDATE_PROG} ] && check_skip
		then
			if ! CTARGET=${CTARGET} GCC_CONFIG_VER=${GCC_CONFIG_VER} ${RUN_CORE} ${CORE_METHOD} --no-progress --pkg-version ${PVR} --pkg-slot ${SLOT/\/*} --pkg-category ${CATEGORY} --pkg-path "/" --pkg-name ${PN}
			then
				echo $CATEGORY $PN $PVR ${SLOT/\/*} >>/var/lib/calculate/calculate-core/setup-failed.list
				return 1
			fi
		fi
	fi
	rm -f /var/lib/calculate/-runlevels-$PN
}

# remove mark: package updating
clean_update_mark() {
	rm -f /var/lib/calculate/-merge-$PN-*
}

# remove contents file which need for cl-core-setup
clean_contents_mark() {
	rm -f /var/lib/calculate/-CONTENTS-*
}

# mark: package updating
mark_update_package() {
	if [[ -d /var/lib/calculate ]]
	then
		touch "/var/lib/calculate/-merge-$PN--${SLOT/\/*}-$PPID"
	fi
}

# save services runlevels
save_runlevels() {
	if [[ -d /var/lib/calculate ]]
	then
		find /etc/runlevels/{default,boot,sysinit,shutdown} >/var/lib/calculate/-runlevels-$PN
	fi
}

# dispatching unmerge or update package
pkg_postrm_dispatch() {
	if [[ ! -f /var/lib/calculate/-merge-$PN--${SLOT/\/*}-$PPID ]]
	then
		pkg_postrm_unmerge
	else
		pkg_postrm_update
	fi
}

# restore files of packages which stashed for resolve automagic depends
unstash_automagic_requires() {
	local automagic_dir=/var/lib/calculate/calculate-builder/automagic-requires
	if [[ -d $automagic_dir ]]
	then
		# -K - need to resolve lib -> lib64 symlink problem
		# -X - to keep file capabilities
		/usr/bin/rsync -aXKx ${automagic_dir}/ / && rm -rf ${automagic_dir}
	fi
}

# setup package umerging
pkg_postrm_unmerge() {
	if ! [[ -f /var/lib/calculate/calculate-core/cache/merge-setup.list ]] ||
		grep ${CATEGORY}/${PN} /var/lib/calculate/calculate-core/cache/merge-setup.list &>/dev/null
	then
		[ -f ${CL_UPDATE_PROG} ] && check_skip && ${RUN_CORE} ${CORE_METHOD} --no-progress --pkg-version ${PVR} --pkg-slot ${SLOT/\/*} --pkg-category ${CATEGORY} --pkg-path / --pkg-name ${PN}
	fi
}

# save contents file for correct package updating by cl-core-setup
pkg_postrm_update() {
	if [[ -d /var/lib/calculate ]]
	then
		cp /var/db/pkg/${CATEGORY}/${PF}/CONTENTS /var/lib/calculate/-CONTENTS-$PN
	fi
}

pre_pkg_postrm() {
	pkg_postrm_dispatch
	clean_update_mark
}

pre_pkg_postinst() {
	setup_merge_package
	clean_contents_mark
	clean_update_mark
	unstash_automagic_requires
}

pre_pkg_preinst() {
	mark_update_package
	save_runlevels
}
