Skip to content

Instantly share code, notes, and snippets.

@razielanarki
Last active June 1, 2024 15:32
Show Gist options
  • Save razielanarki/b86da5d484ffd30e2b7ea1949ba01e22 to your computer and use it in GitHub Desktop.
Save razielanarki/b86da5d484ffd30e2b7ea1949ba01e22 to your computer and use it in GitHub Desktop.
bpt : 'backports-enabled apt' - an tiny apt front by razielanarki
#!/usr/bin/env bash
#============================================================================
# bpt : 'backports-enabled apt' - a tiny apt front by razielanarki
#============================================================================
# auto adds "--target-release $(lsb_release -cs)-backports" where applicable
# (ex.: '--target-release oracular-backports')
#----------------------------------------------------------------------------
# install: move to ~/.local/bin/bpt (or anywhere in PATH)
#----------------------------------------------------------------------------
# usage:
# $ bpt [task] [...args]
#----------------------------------------------------------------------------
# available task shorthands
# - bpt : update (when called with no arguments)
# - bpt up : update + upgrade + autopurge
# - bpt full : update + full-upgrade + autopurge
# - bpt ls : list
# - bpt q : search
# - bpt dl : download
# - bpt cl : autoremove (via autopurge)
# - bpt i : install
# - bpt u : uninstall (via purge)
# - bpt re : reinstall
#----------------------------------------------------------------------------
# - environment variables:
# - BPT_APT : apt command to invoke (default: apt)
# - BPT_TARGET : target release, eg.: 'proposed' etc (default: backports)
#----------------------------------------------------------------------------
# shellcheck disable=SC2155 disable=SC2206
#----------------------------------------------------------------------------
shopt -s extglob
#----------------------------------------------------------------------------
function dbg::log { printf "'%s' " "$@"; echo; }
# use colors if called from a tty
function bpt::tput { tty -s && [ "$(tput colors)" -ge 8 ]; }
function sgr { bpt::tput && echo -n $'\033'"[;$(IFS=';' ; echo "${*}")m"; }
function str { echo -n "$(sgr "${@:2}")$1$(sgr)"; }
#--------------------------------------------------------------
# recursively call bpt with each subcommand specified
# (shifting until no args left)
# set env var '_sequence_' for each subpocess step
function bpt::seq { (( n=$# ))
while (( $# )); do echo; (( ++i ))
echo "$(str '[' 90)" "$(str $i 36)" 'of' "$(str $n 36)" "$(str ']' 90)"
"$this" "$1"
shift; done; exit;
}
#----------------------------------------------------------------------------
declare -g this="$(realpath "$0")"
declare -g exec="sudo"
declare -g call="${BPT_APT:-"apt"}"
declare -g task="${1:-"update"}"
declare -a args=(
"--target-release"
"$(lsb_release -cs 2>/dev/null)-${BPT_TARGET:-"backports"}"
)
#--------------------------------------------------------------
[ "$task" ] && shift
case "$task" in
up) :; bpt::seq update upgrade autopurge ;;
full) :; bpt::seq update full-upgrade autopurge ;;
ls|list) :; task="list" ;&
show) :; unset exec args ;;
q|search) :; task="search" ; unset exec ;;
dl|download) :; task="download" ; unset exec ;;
cl|clean) :; task="autopurge" ; unset args ;;
re|readd) :; task="reinstall" ;;
i|in|add) :; task="install" ;;
u|un|del) :; task="purge" ;;
esac
#--------------------------------------------------------------
ps="${exec:+"#"}"
declare -a logcall=("$(str '->' $(( ${#exec} ? 31 : 32 )) 2)" "$(str "${ps:-"$"}" 90)")
[ "$call" ] && logcall+=("$(str "$call" 32 )")
logcall+=("$(str "$task" 33)")
[ "${args[*]}" ] && logcall+=("$(str "${args[0]}" 34)" "$(str "${args[1]}" 35)")
logcall+=("${@}")
echo -e "${logcall[@]}"
#--------------------------------------------------------------
declare -a aptcall=($exec "$call" "$task" "${args[@]}" "${@}")
# dbg::log "${aptcall[@]}"
"${aptcall[@]}"
#--------------------------------------------------------------
# note to self: this may help sometimes
# -o Debug::pkgProblemResolver=yes
#--------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment