summaryrefslogtreecommitdiff
blob: 0571dd1fb2073842c08f75764ad7c9dfc9130803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: prefix.eclass
# @MAINTAINER:
# Feel free to contact the Prefix team through <prefix@gentoo.org> if
# you have problems, suggestions or questions.
# @SUPPORTED_EAPIS: 5 6 7 8
# @BLURB: Eclass to provide Prefix functionality
# @DESCRIPTION:
# Gentoo Prefix allows users to install into a self defined offset
# located somewhere in the filesystem.  Prefix ebuilds require
# additional functions and variables which are defined by this eclass.

case ${EAPI:-0} in
	[5678]) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

if [[ -z ${_PREFIX_ECLASS} ]]; then
_PREFIX_ECLASS=1

# @ECLASS-VARIABLE: EPREFIX
# @DESCRIPTION:
# The offset prefix of a Gentoo Prefix installation.  When Gentoo Prefix
# is not used, ${EPREFIX} should be "".  Prefix Portage sets EPREFIX,
# hence this eclass has nothing to do here in that case.
# Note that setting EPREFIX in the environment with Prefix Portage sets
# Portage into cross-prefix mode.
if [[ ! ${EPREFIX+set} ]]; then
	export EPREFIX=''
fi


# @FUNCTION: eprefixify
# @USAGE: <list of to be eprefixified files>
# @DESCRIPTION:
# replaces @GENTOO_PORTAGE_EPREFIX@ with ${EPREFIX} for the given files,
# dies if no arguments are given, a file does not exist, or changing a
# file failed.
eprefixify() {
	[[ $# -lt 1 ]] && die "at least one argument required"

	einfo "Adjusting to prefix ${EPREFIX:-/}"
	local x
	for x in "$@" ; do
		if [[ -e ${x} ]] ; then
			ebegin "  ${x##*/}"
			sed -i -e "s|@GENTOO_PORTAGE_EPREFIX@|${EPREFIX}|g" "${x}"
			eend $? || die "failed to eprefixify ${x}"
		else
			die "${x} does not exist"
		fi
	done

	return 0
}

# @FUNCTION: hprefixify
# @USAGE: [ -w <line match> ] [ -e <extended regex> ] [ -q <quotation char> ] <list of files>
# @DESCRIPTION:
# Tries a set of heuristics to prefixify the given files. Dies if no
# arguments are given, a file does not exist, or changing a file failed.
#
# Additional extended regular expression can be passed by -e or
# environment variable PREFIX_EXTRA_REGEX.  The default heuristics can
# be constrained to lines that match a sed expression passed by -w or
# environment variable PREFIX_LINE_MATCH.  Quotation characters can be
# specified by -q or environment variable PREFIX_QUOTE_CHAR, unless
# EPREFIX is empty.
#
# @EXAMPLE:
# Only prefixify the 30th line,
#   hprefixify -w 30 configure
# Only prefixify lines that contain "PATH",
#   hprefixify -w "/PATH/" configure
# Also delete all the /opt/gnu search paths,
#   hprefixify -e "/\/opt\/gnu/d" configure
# Quote the inserted EPREFIX
#   hprefixify -q '"' etc/profile
hprefixify() {
	use prefix || return 0

	local xl=() x
	while [[ $# -gt 0 ]]; do
		case $1 in
			-e) local PREFIX_EXTRA_REGEX="$2"
				shift
				;;
			-w) local PREFIX_LINE_MATCH="$2"
				shift
				;;
			-q) local PREFIX_QUOTE_CHAR="${EPREFIX:+$2}"
				shift
				;;
			*)
				xl+=( "$1" )
				;;
		esac
		shift
	done
	local dirs="/(usr|lib(|[onx]?32|n?64)|etc|bin|sbin|var|opt|run)" \
		  eprefix="${PREFIX_QUOTE_CHAR}${EPREFIX}${PREFIX_QUOTE_CHAR}"

	[[ ${#xl[@]} -lt 1 ]] && die "at least one file operand is required"
	einfo "Adjusting to prefix ${EPREFIX:-/}"
	for x in "${xl[@]}" ; do
		if [[ -e ${x} ]] ; then
			ebegin "  ${x##*/}"
			sed -r \
				-e "${PREFIX_LINE_MATCH}s,([^[:alnum:]}\)\.])${dirs},\1${eprefix}/\2,g" \
				-e "${PREFIX_LINE_MATCH}s,^${dirs},${eprefix}/\1," \
				-e "${PREFIX_EXTRA_REGEX}" \
				-i "${x}"
			eend $? || die "failed to prefixify ${x}"
		else
			die "${x} does not exist"
		fi
	done
}

# @FUNCTION: prefixify_ro
# @USAGE: <file>
# @DESCRIPTION:
# prefixify a read-only file.
# copies the files to ${T}, prefixies it, echos the new file.
# @EXAMPLE:
# doexe "$(prefixify_ro "${FILESDIR}"/fix_libtool_files.sh)"
# epatch "$(prefixify_ro "${FILESDIR}"/${PN}-4.0.2-path.patch)"
prefixify_ro() {
	if [[ -e $1 ]] ; then
		local f=${1##*/}
		cp "$1" "${T}" || die "failed to copy file"
		local x="${T}"/${f}
		# redirect to stderr because stdout is used to
		# return the prefixified file.
		if grep -qs @GENTOO_PORTAGE_EPREFIX@ "${x}" ; then
			eprefixify "${T}"/${f} 1>&2
		else
			hprefixify "${T}"/${f} 1>&2
		fi
		echo "${x}"
	else
		die "$1 does not exist"
	fi
}
# vim: tw=72:

fi