# Copyright 1999-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 DESCRIPTION="Manage /usr/bin/racket implementation" MAINTAINER="tgbugs@gmail.com" VERSION="0.1" BINARYLIST="drracket gracket gracket-text mred mred-text mzc mzpp mzscheme mztext pdf-slatex plt-games plt-help plt-r5rs plt-r6rs plt-web-server raco scribble setup-plt slatex slideshow swindle" find_targets() { # Return the list of available Racket binaries local j for j in "${EROOT}"/usr/bin/racket{cs,bc,cgc}; do [[ -f ${j} ]] && sed 's/racket//' <(basename ${j}) done } remove_symlinks() { # Remove existing symlinks to binaries, man pages, # and the env file (own function) local f for f in racket ${BINARYLIST}; do rm -f "${EROOT}/usr/bin/${f}" done } set_bin_symlinks() { # Set symlinks to binaries in /usr/bin/ local target=$1 f for f in ${BINARYLIST}; do # set symlink only if target binary actually exists if [[ -f ${EROOT}/usr/bin/${f}${target} ]]; then ln -s "${f}${target}" "${EROOT}/usr/bin/${f}" || die \ "Couldn't set ${f}${target} ${EROOT}/usr/bin/${f} symlink" fi done } set_symlinks() { # Set symlinks to binaries and man pages, update info path local target=$1 nomain=$2 targets suffix i # target may be specified by its name or its index if is_number "${target}"; then # numeric index, find the target's name targets=( $(find_targets) ) [[ ${target} -ge 1 && ${target} -le ${#targets[@]} ]] \ || die -q "Number out of range: $1" target=${targets[target-1]} fi # is the target valid, i.e. does an Racket binary with this name exist? [[ -f ${EROOT}/usr/bin/racket${target} ]] \ || die -q "Target \"$1\" doesn't appear to be valid!" echo "Switching racket to ${target} ..." remove_symlinks || die -q "Couldn't remove existing symlink" # the main /usr/bin/racket symlink is only set for Racket binaries if [[ -z ${nomain} ]]; then ln -s "racket${target}" "${EROOT}/usr/bin/racket" \ || die "Couldn't set ${target} ${EROOT}/usr/bin/racket symlink" fi set_bin_symlinks "${target}" # update /etc/profile.env from /etc/env.d files do_action env update noldconfig return 0 } test_for_root() { # checks if the user has rights to modify /usr/bin/ [[ -w ${EROOT}/usr/bin ]] || die -q "You need root privileges!" } ### show action ### describe_show() { echo "Show the current target of the Racket symlink" } do_show() { [[ $# -gt 0 ]] && die -q "Too many parameters" write_list_start "Current target of Racket symlink:" if [[ -L ${EROOT}/usr/bin/racket && -e ${EROOT}/usr/bin/racket ]]; then write_kv_list_entry \ "$(basename "$(readlink "${EROOT}/usr/bin/racket")")" "" elif [[ -e ${EROOT}/usr/bin/racket ]]; then write_kv_list_entry \ "(not a symlink or target of symlink does not exist)" "" else write_kv_list_entry "(unset)" "" fi } ### list action ### describe_list() { echo "List available Racket symlink targets" } do_list() { [[ $# -gt 0 ]] && die -q "Too many parameters" local i targets targets=( $(find_targets) ) for (( i = 0; i < ${#targets[@]}; i++ )); do # Highlight the currently chosen version [[ racket${targets[i]} = \ "$(basename "$(readlink "${EROOT}/usr/bin/racket")")" ]] \ && targets[i]=$(highlight_marker "${targets[i]}") done write_list_start "Available Racket symlink targets:" write_numbered_list -m "(none found)" "${targets[@]}" } ### set action ### describe_set() { echo "Set a new Racket symlink" } describe_set_options() { echo "target : Target name or number (from 'list' action)" } describe_set_parameters() { echo "" } do_set() { [[ -z $1 ]] && die -q "You didn't tell me what to set the symlink to" [[ $# -gt 1 ]] && die -q "Too many parameters" test_for_root if [[ -e ${EROOT}/usr/bin/racket && ! -L ${EROOT}/usr/bin/racket ]]; then die -q "${EROOT}/usr/bin/racket exists but is not a symlink" fi set_symlinks "$1" || die -q "Couldn't set a new symlink" }