|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +termux_step_update_alternatives() { |
| 4 | + printf '%s\n' "INFO: Processing 'update-alternatives' entries:" 1>&2 |
| 5 | + for alternatives_file in "${TERMUX_PKG_BUILDER_DIR}"/*.alternatives; do |
| 6 | + [[ -f "$alternatives_file" ]] || continue |
| 7 | + local -a NAME=() |
| 8 | + local -A DEPENDENTS=() LINK=() ALTERNATIVE=() PRIORITY=() |
| 9 | + termux_alternatives__parse_alternatives_file "$alternatives_file" |
| 10 | + |
| 11 | + # Handle postinst script |
| 12 | + [[ -f postinst ]] && mv postinst{,.orig} |
| 13 | + |
| 14 | + local name |
| 15 | + |
| 16 | + { # Splice in the alternatives |
| 17 | + # Use the original shebang if there's a 'postinst.orig' |
| 18 | + [[ -f postinst.orig ]] && head -n1 postinst.orig || echo "#!${TERMUX_PREFIX}/bin/sh" |
| 19 | + # Boilerplate header comment and checks |
| 20 | + echo "# Automatically added by termux_step_update_alternatives" |
| 21 | + echo "if [ \"\$1\" = 'configure' ] || [ \"\$1\" = 'abort-upgrade' ] || [ \"\$1\" = 'abort-deconfigure' ] || [ \"\$1\" = 'abort-remove' ]; then" |
| 22 | + echo " if [ -x \"${TERMUX_PREFIX}/bin/update-alternatives\" ]; then" |
| 23 | + # 'update-alternatives' command for each group |
| 24 | + for name in "${NAME[@]}"; do |
| 25 | + # Main alternative group |
| 26 | + printf '%b' \ |
| 27 | + " # ${name}\n" \ |
| 28 | + " update-alternatives" $' \\\n' \ |
| 29 | + " --install \"${TERMUX_PREFIX}/${LINK[$name]}\" \"${name}\" \"${TERMUX_PREFIX}/${ALTERNATIVE[$name]}\" ${PRIORITY[$name]}" |
| 30 | + # If we have dependents, add those as well |
| 31 | + if [[ -n "${DEPENDENTS[$name]}" ]]; then |
| 32 | + # We need to add a ' \<lf>' to the --install line, |
| 33 | + # and remove the last ' \<lf>' from the dependents. |
| 34 | + printf ' \\\n%s' "${DEPENDENTS[$name]%$' \\\n'}" |
| 35 | + fi |
| 36 | + echo "" |
| 37 | + done |
| 38 | + # Close up boilerplate and add end comment |
| 39 | + echo " fi" |
| 40 | + echo "fi" |
| 41 | + echo "# End automatically added section" |
| 42 | + } > postinst |
| 43 | + if [[ -f postinst.orig ]]; then |
| 44 | + tail -n+2 postinst.orig >> postinst |
| 45 | + rm postinst.orig |
| 46 | + fi |
| 47 | + |
| 48 | + # Handle prerm script |
| 49 | + [[ -f prerm ]] && mv prerm{,.orig} |
| 50 | + |
| 51 | + { # Splice in the alternatives |
| 52 | + # Use the original shebang if there's a 'prerm.orig' |
| 53 | + [[ -f prerm.orig ]] && head -n1 prerm.orig || echo "#!${TERMUX_PREFIX}/bin/sh" |
| 54 | + # Boilerplate header comment and checks |
| 55 | + echo "# Automatically added by termux_step_update_alternatives" |
| 56 | + echo "if [ \"\$1\" = 'remove' ] || [ \"\$1\" != 'upgrade' ]; then" |
| 57 | + echo " if [ -x \"${TERMUX_PREFIX}/bin/update-alternatives\" ]; then" |
| 58 | + # Remove each group |
| 59 | + for name in "${NAME[@]}"; do |
| 60 | + # Log message for this alternative group |
| 61 | + printf 'INFO: %s\n' "${name} -> ${ALTERNATIVE[$name]} (${PRIORITY[$name]})" 1>&2 |
| 62 | + # Removal line |
| 63 | + printf '%s\n' " update-alternatives --remove \"${name}\" \"${TERMUX_PREFIX}/${ALTERNATIVE[$name]}\"" |
| 64 | + done |
| 65 | + # Close up boilerplate and add end comment |
| 66 | + echo " fi" |
| 67 | + echo "fi" |
| 68 | + echo "# End automatically added section" |
| 69 | + } > prerm |
| 70 | + if [[ -f prerm.orig ]]; then |
| 71 | + tail -n+2 prerm.orig >> prerm |
| 72 | + rm prerm.orig |
| 73 | + fi |
| 74 | + done |
| 75 | +} |
0 commit comments