aboutsummaryrefslogtreecommitdiff
path: root/misc/qtchooser/files/update-qtchooser-wrapper.in
blob: 2b2b2c76f02e7e4bbaf25b77bb331865142015af (plain) (blame)
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
#!/bin/sh

# If a port installs Qt version-specific binaries (e.g. "designer" which
# existed as a Qt4 application and exists as a Qt5 application and will
# probably be a Qt6 application) which should have a qtchooser-based
# wrapper, the port should set `QT_BINARIES=yes`.
#
# When QT_BINARIES is set to yes, compatibility symlinks (in bin/,
# designer -> qtchooser, so that qtchooser can run designer-qt5 or
# whatever is the selected Qt version) are installed by the port.
#
# Versioned binaries in ports are conventionally installed in <ver>/bin/,
# so qt5/bin/designer is the versioned form of "designer".

PREFIX=%%PREFIX%%
BINDIR=${PREFIX}/bin
QTCHOOSER=${BINDIR}/qtchooser
VERSIONS=%%QT_SUPPORTED%%

# Sanity-checking. Distinguish weird failure cases by exit code.
if [ ! -d ${BINDIR} ] ; then
	echo "Binary directory '${BINDIR}' missing." >&2
	exit 1
fi

if [ ! -x ${QTCHOOSER} ] ; then
	echo "Qtchooser binary '${QTCHOOSER}' missing." >&2
	exit 2
fi

# Remove links-to-qtchooser that are no longer needed.
#
# We check in ${BINDIR} -- only **directly** in bindir, hence the
# `-maxdepth 1` -- for symlinks to qtchooser. Those are unversioned
# names (e.g. designer -> qtchooser) for the case where qtchooser
# should pick the right one.
#
# If there aren't any versioned names for the same, remove the link
# (e.g. when designer is removed from the versioned bin/ directory
# under qt5/bin/, then designer -> qtchooser can go as well).
#
remove_links() {
	for file in $(find -L ${BINDIR} -maxdepth 1 -samefile ${QTCHOOSER}) ; do
		# Only symlinks count, since qtchooser lives here too
		if [ ! -L ${file} ] ; then
			continue
		fi
		# If at least one versioned executable is found for this name,
		# keep the qtchooser compatibility symlink for this name; 
		# otherwise, remove it.
		local found=0
		for version in ${VERSIONS} ; do
			version_bin_dir=${PREFIX}/lib/qt${version}/bin
			target=${version_bin_dir}/$(basename ${file})
			# Only need to find **one** to keep the link.
			if [ -x ${target} ] ; then
				found=1
				break
			fi
		done
		if [ ${found} -eq 0 ] ; then
			rm ${file}
		fi
	done
}

# Create links-to-qtchooser when a versioned application is installed.
#
# We check directly under the versioned bin/ directories, not in subdirectories
# of bin/ -- so we need `-maxdepth 1` -- for names which can be hooked up to
# qtchooser. Each of the names in the versioned bin/ dir is connected to
# qtchooser if there isn't already a link of that name.
#
create_links() {
	for version in ${VERSIONS} ; do
		version_bin_dir=${PREFIX}/lib/qt${version}/bin
		if [ -d ${version_bin_dir} ] ; then
			for file in $(find ${version_bin_dir} -type f -maxdepth 1) ; do
				# target is the **unversioned** one (e.g.
				# designer, living in bin/) which needs to
				# be pointed at qtchooser because there is a
				# **versioned** designer in e.g. qt5/bin/.
				target=${BINDIR}/$(basename ${file})
				if [ ! -L ${target} -a ! -f ${target} ] ; then
					ln -s ${QTCHOOSER} ${target}
				fi
			done
		fi
	done
}

remove_links
create_links