diff --git a/blesh.sh b/blesh.sh new file mode 100644 index 0000000..4a6d9f4 --- /dev/null +++ b/blesh.sh @@ -0,0 +1,223 @@ +{ + #!/usr/bin/env bash + + # ─< Check if the given command exists silently >───────────────────────────────────────── + command_exists() { + command -v "$@" >/dev/null 2>&1 + } + + getImports() { + local url="$1" + local import="$(mktemp)" + if command_exists curl; then + curl -fsSL $url -o $import + elif command_exists wget; then + wget -O $import $url + else + echo "curl/wget is required, but missing.." + exit 69 + fi + + source "$import" + + echo "${BLUE}Sourcing external script:${NC} $url" + sleep 0.1 + rm -f "$import" + } + + setup-env() { + # WHY: + # This import will give you the following variables: + # _sudo="sudo -E" <- only if non root user + # distro = + # arch = bool + # fedora = bool + # opensuse = bool.... + # You can then use it for, `if $arch; then` + # Also this gives you the _install command, which installs a package pased on the packagemanager/distro used. + # + # CAUTION: + # This only wokrs for generic package names, like neovim, or vim, or tmux etc.. + # not every package packagemanager has the same packagenames for their packages.. + # + # local beddu=https://git.k4li.de/scripts/beddu/raw/branch/main/dist/beddu.sh + # local pika=https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh + # dream ~= combined beddu and pika + local dream=https://git.k4li.de/scripts/imports/raw/branch/main/dream.sh + + if ! command_exists pkg-install && ! command_exists check-and-install && ! command_exists pen; then + getImports $dream + fi + } + + getDependencies() { + # INFO: + # ╭─────────────────────────────────────────────────────────────────────────╮ + # │ You can define dependencies for various linux distros here. It will │ + # │ automagically be pulled via the $pkgArray[$distro] variable │ + # ╰─────────────────────────────────────────────────────────────────────────╯ + depsGeneral=( + gmake + make + bash + git + curl + wget + gawk + ) + + depsDebian=() + depsUbuntu=() + depsFedora=() + depsOpensuse=() + depsArch=() + depsAlpine=() + + declare -A deps=( + [debian]="depsDebian" + [ubuntu]="depsUbuntu" + [fedora]="depsFedora" + [arch]="depsArch" + [alpine]="depsAlpine" + [opensuse]="depsOpensuse" + ) + + # INFO: + # ╭────────────────────────────────────────────────────────────────╮ + # │ This variable stores the packages you provided for each distro │ + # ╰────────────────────────────────────────────────────────────────╯ + declare -n pkgArray="${deps[$distro]}" + + pkgArray="${pkgArray[@]} ${depsGeneral[@]}" + + case "$distro" in + debian | ubuntu | arch | fedora | alpine | opensuse) + # installs packages inside the ${pkgArray[@]} list, which is the $distro part of the confiruation above + check-and-install ${pkgArray[@]} + ;; + *) + pen bold yellow "There are no dependencies to install for $distro" + return 0 + ;; + esac + } + + blesh-exists() { + if command-exists ble; then + # ble exists + pen bold red "Blesh already exists on this system!" + return 0 + else + return 69 + fi + } + + clone-blesh() { + local tmpdir out err + + spin bold yellow "Cloning $PACKAGE sources" + + tmpdir=$(mktemp -d) + cd $tmpdir || mkdir -p $tmpdir && cd $tmpdir + + if run --err err --out out git clone --recursive --depth 1 --shallow-submodules https://github.com/akinomyoga/ble.sh.git; then + upclear + check bold yellow "Cloned $PACKAGE into $tmpdir/ble.sh" + else + upclear + throw bold red "Error cloning $PACKAGE" + thro bold yellow "OUT: ${out:-}" + echo-error "${err:-}" + fi + + cd ble.sh + } + + make-blesh() { + local err out + spin bold blue "Compiling $PACKAGE sources.." + + if run --err err --out out make; then + upclear + check bold yellow "Build $PACAKGE complete" + else + upclear + throw bold red "Error building $PACKAGE" + thro bold yellow "OUT: ${out:-}" + echo-error "${err:-}" + fi + + make install + } + + main() { + if $silent; then + pen yellow "ARG: Executing script silently!" + fi + + if ! getDependencies; then + throw "Error when installing dependencies.." + fi + + if ! blesh-exists; then + clone-blesh + make-blesh + else + pen bold blue "Blesh already exists, updating it now!" + ble-update + fi + + # case "$distro" in + # arch) + # echo "arch" + # ;; + # debian) + # echo "debian" + # ;; + # ubuntu) + # echo "ubuntu" + # ;; + # fedora) + # echo "fedora" + # ;; + # alpine) + # echo "alpine" + # ;; + # opensuse) + # echo "opensuse" + # ;; + # *) + # echo "$distro is not supported by this script!" + # exit 69 + # ;; + # esac + } + + if getImports; then + # ─< package variable >─────────────────────────────────────────────────────────────────── + unset PACKAGE + + # ─< argument list variables >──────────────────────────────────────────────────────────── + silent=false + + sleep 0.1 + + PACKAGE=blesh + if command_exists "$PACKAGE"; then + echo_warning "$PACKAGE is already installed!" + echo_warning "Exiting now!" + exit 69 + fi + + # ─< parse arguments and get variable contents >────────────────────────────────────────── + for arg in "$@"; do + case "$arg" in + --silent | -s) + export silent=true + ;; + esac + done + + main + fi +}