xref: /OK3568_Linux_fs/yocto/scripts/postinst-intercepts/postinst_intercept (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# SPDX-License-Identifier: MIT
4*4882a593Smuzhiyun#
5*4882a593Smuzhiyun# This script is called from inside postinstall scriptlets at do_rootfs time. It
6*4882a593Smuzhiyun# actually adds, at the end, the list of packages for which the intercept script
7*4882a593Smuzhiyun# is valid. Also, if one wants to pass any variables to the intercept script from
8*4882a593Smuzhiyun# the postinstall itself, they will be added immediately after the shebang line.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun# Usage: postinst_intercept <intercept_script_name> <package_name> <mlprefix=...> <var1=...> ... <varN=...>
11*4882a593Smuzhiyun#  * intercept_script_name - the name of the intercept script we want to change;
12*4882a593Smuzhiyun#  * package_name - add the package_name to list of packages the intercept script
13*4882a593Smuzhiyun#                   is used for;
14*4882a593Smuzhiyun#  * mlprefix=... - this one is needed in order to have separate hooks for multilib.
15*4882a593Smuzhiyun#  * var1=... - var1 will have the value we provide in the intercept script. This
16*4882a593Smuzhiyun#               is useful when we want to pass on variables like ${libdir} to
17*4882a593Smuzhiyun#               the intercept script;
18*4882a593Smuzhiyun#
19*4882a593Smuzhiyun[ $# -lt 3 ] && exit 1
20*4882a593Smuzhiyun
21*4882a593Smuzhiyunintercept_script=$INTERCEPT_DIR/$1 && shift
22*4882a593Smuzhiyunpackage_name=$1 && shift
23*4882a593Smuzhiyunmlprefix=$(echo $1 |sed -ne "s/^mlprefix=\(.*\)-/\1/p") && shift
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun# if the hook we want to install does not exist, then there's nothing we can do
26*4882a593Smuzhiyun[ -f "$intercept_script" ] || exit 1
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun# if the postinstall wanting to install the hook belongs to a multilib package,
29*4882a593Smuzhiyun# then we'd better have a separate hook for this because the default ${libdir} and
30*4882a593Smuzhiyun# ${base_libdir} will point to the wrong locations
31*4882a593Smuzhiyunif [ -n "$mlprefix" ]; then
32*4882a593Smuzhiyun	ml_intercept_script=$intercept_script-$mlprefix
33*4882a593Smuzhiyun	# if the multilib hook does not exist, create it from the default one
34*4882a593Smuzhiyun	if [ ! -f "$ml_intercept_script" ]; then
35*4882a593Smuzhiyun		cp $intercept_script $ml_intercept_script
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun		# clear the ##PKGS: line and the already set variables
38*4882a593Smuzhiyun		[ -x "$ml_intercept_script" ] && sed -i -e "2,$(($#+1)) {/.*/d}" -e "/^##PKGS: .*/d" $ml_intercept_script
39*4882a593Smuzhiyun	fi
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun	intercept_script=$ml_intercept_script
42*4882a593Smuzhiyunfi
43*4882a593Smuzhiyun
44*4882a593Smuzhiyunchmod +x "$intercept_script"
45*4882a593Smuzhiyun
46*4882a593Smuzhiyunpkgs_line=$(grep "##PKGS:" $intercept_script)
47*4882a593Smuzhiyunif [ -n "$pkgs_line" ]; then
48*4882a593Smuzhiyun	# line exists, add this package to the list only if it's not already there
49*4882a593Smuzhiyun	if [ -z "$(echo "$pkgs_line" | grep " $package_name ")" ]; then
50*4882a593Smuzhiyun		sed -i -e "s/##PKGS:.*/\0${package_name} /" $intercept_script
51*4882a593Smuzhiyun	fi
52*4882a593Smuzhiyunelse
53*4882a593Smuzhiyun	for var in "$@"; do
54*4882a593Smuzhiyun		sed -i -e "\%^#\!/bin/.*sh%a $var" $intercept_script
55*4882a593Smuzhiyun	done
56*4882a593Smuzhiyun	echo "##PKGS: ${package_name} " >> $intercept_script
57*4882a593Smuzhiyunfi
58*4882a593Smuzhiyun
59