eselect-ld/ld.eselect
2022-03-30 23:37:07 +02:00

108 lines
2.7 KiB
Text

# Copyright (C) 2022 Martin Matouš
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
DESCRIPTION="Manage the ld symlink"
MAINTAINER="Martin Matouš <m@matous.dev>"
VERSION="1.0.0"
BIN_DIR="${EROOT%/}/usr/bin"
find_targets() {
local linker
# get all ld.* files
for linker in "${BIN_DIR}/"ld.*; do
[[ -f ${linker} ]] && basename "${linker}"
done
}
remove_symlink() {
[[ ! -e "${BIN_DIR}/ld" ]] && return
[[ -L "${BIN_DIR}/ld" ]] || die -q "${BIN_DIR}/ld is not a symlink!"
rm -f "${BIN_DIR}/ld"
}
set_symlink() {
local target="$1"
if is_number "${target}"; then
local -r targets=( $(find_targets) )
[[ ${target} -ge 1 && ${target} -le ${#targets[@]} ]] \
|| die -q "Number out of range: $1"
target=${targets[target-1]}
elif [[ ! "$target" == "ld."* ]]; then
target="ld.$target"
fi
if [[ ! -L "${BIN_DIR}/${target}" ]]; then
die -q "Target \"${1}\" (${BIN_DIR}/${target}) doesn't appear to be valid!"
fi
remove_symlink || die -q "Couldn't remove existing symlink"
ln -s "${target}" "${BIN_DIR}/ld"
}
### show action ###
describe_show() {
echo "Show the current linker symlink"
}
do_show() {
write_list_start "Current linker symlink:"
if [[ -L "${BIN_DIR}/ld" ]]; then
# "dereference" only 1 layer od symlinking, unlike readlink/canonicalize
local -r linker=$(find "${BIN_DIR}/ld" -printf %l)
write_kv_list_entry "${linker}" ""
else
write_kv_list_entry "(unset)" ""
fi
}
### list action ###
describe_list() {
echo "List available linkers"
}
do_list() {
local i
local targets=( $(find_targets) )
write_list_start "Available linker symlink targets:"
for (( i = 0; i < ${#targets[@]}; i++ )); do
[[ ${targets[i]} = $(find "${BIN_DIR}/ld" -printf %l) ]] \
&& targets[i]=$(highlight_marker "${targets[i]}")
done
write_numbered_list -m "(none found)" "${targets[@]}"
}
### set action ###
describe_set() {
echo "Set a new linker symlink target"
}
describe_set_parameters() {
echo "<target>"
}
describe_set_options() {
echo "target : Target name or number (from 'list' action)"
}
do_set() {
local -r linker="$1"
[[ -z "$linker" ]] && die -q "You didn't tell me what to set the symlink to"
[[ $# -gt 1 ]] && die -q "Too many parameters"
set_symlink "$linker" || die -q "Couldn't set a new symlink"
}