diff options
author | Squibid <me@zacharyscheiman.com> | 2024-08-09 20:40:20 -0400 |
---|---|---|
committer | Squibid <me@zacharyscheiman.com> | 2024-08-09 20:40:20 -0400 |
commit | cec832f43f8971eb331678ce8d8a66ed9df816d6 (patch) | |
tree | f4a6e391b13eef67d37dd3135e05049b96589502 /auto. | |
download | auto.s-cec832f43f8971eb331678ce8d8a66ed9df816d6.tar.gz auto.s-cec832f43f8971eb331678ce8d8a66ed9df816d6.tar.bz2 auto.s-cec832f43f8971eb331678ce8d8a66ed9df816d6.zip |
inital commit
Diffstat (limited to 'auto.')
-rwxr-xr-x | auto. | 177 |
1 files changed, 177 insertions, 0 deletions
@@ -0,0 +1,177 @@ +#!/bin/sh + +# defaults +topography=.topography + +help() { +printf "Usage: auto. [OPTION]\n\ + -t,\tspecify the topography of your dotfiles (default: .topography)\n\ + -i,\tinstall dotfiles specified by -t\n\ + -u,\tuninstall dotfiles specified by -t\n\ + -b,\texit as soon as a command errors\n\ + -h,\tshows help text\n" +exit 0 +} + +# setup package management util +for file in\ + /etc/os-release\ + /etc/lsb-release\ + /usr/lib/os-release\ + /etc/openwrt_release +do + [ -f "$file" ] && . "$file" && break +done + +####################################### +# do root - runs a command as root +# Arguments: +# ... command +####################################### +dort() { + if [ -x /bin/doas ] && [ -f /etc/doas.conf ]; then + doas "$@" + elif [ -x /bin/sudo ]; then + sudo "$@" + fi +} + +case "$ID" in + void) + pkg_man=xbps + pkg_install() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: xbps-install -y -- %s\033[m\n" "$*" + dort xbps-install -y -- $* + } + + pkg_uninstall() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: xbps-remove -Roy -- %s\033[m\n" "$*" + dort xbps-remove -Roy -- $* + } + + pkg_search() { + xbps-query --search= | fzf -f "$1" | head -1 | cut -d ' ' -f 2 + } + ;; + arch) + pkg_man=pacman + pkg_install() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: pacman -s --noconfirm -- %s\033[m\n" "$*" + dort pacman -S --noconfirm -- $* + } + + pkg_uninstall() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: pacman -Runs --noconfirm -- %s\033[m\n" "$*" + dort pacman -Runs --noconfirm -- $* + } + + pkg_search() { + pacman -Slq | fzf -f "$1" | head -1 + } + ;; + pop|debian|ubuntu) + pkg_man=apt + pkg_install() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: apt-get install -y -- %s\033[m\n" "$*" + dort apt-get install -y -- $* + } + + pkg_uninstall() { + [ -z "$*" ] && return 1 + printf "\033[31mRunning: apt-get purge --autoremove -y -- %s\033[m\n" "$*" + dort apt-get purge --autoremove -y -- $* + } + + pkg_search() { + apt list | fzf -f "$1" | head -1 | cut -d '/' -f 1 + } + ;; +esac + +# parse command line options +while getopts :biuht: c; do + case $c in + t) + topography="${OPTARG}" + + [ ! -f "$topography" ] &&\ + printf "invalid topography file\n" &&\ + exit 1 + ;; + # only install if not already uninstalling + i) [ -z $uninstall ] && install=1 ;; + # only uninstall if not already installing + u) [ -z $install ] && uninstall=1 ;; + b) + set -e + bailout() { + [ $? -eq 0 ] && return # don't run when the script ends successfully + printf "\033[31mlast command failed, bailing out!\033[m\n" + } + + trap bailout EXIT + ;; + h|*) help ;; + esac +done + +[ ! -f "$topography" ] &&\ + printf "no '.topography' file found\n" &&\ + exit 1 + +[ -z $install ] && [ -z $uninstall ] &&\ + printf "no action specified\n" &&\ + help + +# parse topography file line by line +while read -r line || return 0; do + # skip lines starting with a # (comments) + if [ "$(printf "%s" "$line" | head -c1)" = "#" ]; then + continue + + # run a command + elif [ "$(printf "%s" "$line" | head -c2)" = '$$' ]; then + eval "${line##*\$\$}" + + # install/uninstall a package if on a supported distro + elif [ "$(printf "%s" "$line" | head -c2)" = '::' ] && [ -n "$pkg_man" ]; then + # get line without prefix + s=${line##*\:\:} + + # get number of packages specified + npkgs=$(printf "%s" "$s" | grep -o ',' | wc -l) + npkgs=$((npkgs + 2)) + + i=1 + while [ $i -lt $npkgs ]; do + pkgname="$(printf "%s" "$s" | cut -d ',' -f $i)" + + pkg=$(pkg_search "$pkgname") + + [ $install ] && ipkgs=${ipkgs:+$ipkgs }$pkg + [ $uninstall ] && upkgs=${upkgs:+$upkgs }$pkg + + i=$((i + 1)) + done + + pkg_install $ipkgs + pkg_uninstall $upkgs + + # remove/add a file + elif printf "%s" "$line" | grep -q -- "->"; then + # get the path of the file + sourcef=$(eval echo "${line%%\ ->*}") + + # get the destination for the file + destf=$(eval echo "${line##*->\ }") + + # install/uninstall + [ $install ] && cp -rv -- "$sourcef" "$destf" + [ $uninstall ] && rm -rv -- "$destf" + fi +done < "$topography" |