From e5d50eb7e52e7e27959a1b912fb1c156fe2b2ff7 Mon Sep 17 00:00:00 2001 From: Guilherme Rugai Freire Date: Sun, 4 Jun 2023 17:58:40 -0300 Subject: [PATCH] create tar-install script --- bin/tar-install | 1 + tar-install/README.md | 40 +++++++++++++ tar-install/tar-install.sh | 119 +++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 120000 bin/tar-install create mode 100644 tar-install/README.md create mode 100755 tar-install/tar-install.sh diff --git a/bin/tar-install b/bin/tar-install new file mode 120000 index 0000000..948d3dd --- /dev/null +++ b/bin/tar-install @@ -0,0 +1 @@ +../tar-install/tar-install.sh \ No newline at end of file diff --git a/tar-install/README.md b/tar-install/README.md new file mode 100644 index 0000000..9548931 --- /dev/null +++ b/tar-install/README.md @@ -0,0 +1,40 @@ +# tar-install + +## About +Easily install binary tarballs in your home directory without messing with native packages. + +> DO NOT USE AS A PACKAGE MANAGER REPLACEMENT + +### Use cases: + - Testing out binaries without commitment + - Installing software just for your user + - Installing local libs + - Using releases not yet distributed by your package manager + - etc + +## How to use it + +### Preparing the environment + +1. Create a folder in your home dir called `.opt` +2. Add to your path `$HOME/.opt/bin/` +3. That's it! Start installing packages + +### Installing +From `tar-install -h`: +``` +Usage: tar-install [OPTION] FILE + tar-install file.tar Install the tar program in $HOME/.opt + tar-install -u, --uninstall file.tar Uninstall the tar file already installed + +ATENTION: BE SURE THAT YOUR TARBALL HAS THE FOLLOWING FORMAT: + /program_name + /bin + program + /lib + ... + /share + ... +``` + + diff --git a/tar-install/tar-install.sh b/tar-install/tar-install.sh new file mode 100755 index 0000000..7398b10 --- /dev/null +++ b/tar-install/tar-install.sh @@ -0,0 +1,119 @@ +#!/bin/sh + +install_location="$HOME/.opt" + + +help() { + program="$(basename "$0")" + echo "Usage: $program [OPTION] FILE" + echo " $program file.tar Install the tar program in $install_location" + echo " $program -u, --uninstall file.tar Uninstall the tar file already installed" + echo "" + echo "ATENTION: BE SURE THAT YOUR TARBALL HAS THE FOLLOWING FORMAT:" + echo " /program_name" + echo " /bin" + echo " program" + echo " /lib" + echo " ..." + echo " /share" + echo " ..." +} + +uninstall() { + file=$1 + + if [ -z "$file" ]; then + printf "File was not provided.\n\n" + help + exit 1 + fi + + echo "UNINSTALLING $file" + + # Get files to remove + printf "\nListing files for removal in %s\n\n" "$file" + echo "+ tar -tf $file | cut -d'/' -f 2-" + files_and_folders_to_remove="$(tar -tf "$file" | cut -d'/' -f 2-)" + exit_code="$?" + + if [ "$exit_code" -ne 0 ]; then + printf "\nError listing files in %s\n" "$file" + exit "$exit_code"; + fi + + files_to_remove="$(echo "$files_and_folders_to_remove" | awk -F'/' '{if ($NF != "") print $0}')" + folders_to_remove="$(echo "$files_and_folders_to_remove" | awk -F'/' '{if ($NF == "") print $0}' | tac)" + + # Remove files + printf "\nRemoving files\n\n" + echo "+ echo \$files_to_remove | xargs -I'{}' rm -r $install_location/{}" + echo "$files_to_remove" | xargs -I'{}' rm "$install_location"/{} + exit_code="$?" + + if [ "$exit_code" -ne 0 ]; then + printf "\nError removing files in %s\n" "$install_location" + exit "$exit_code"; + fi + + printf "\nRemoving empty folders\n\n" + echo "+ echo \$folders_to_remove | xargs -I'{}' rm -d $install_location/{}" + echo "$folders_to_remove" | xargs -I'{}' rm -d "$install_location"/{} + + printf "\nSuccessefully uninstalled %s\n" "$file" +} + + +install() { + file=$1 + + if [ -z "$file" ]; then + printf "File was not provided.\n\n" + help + exit 1 + fi + + echo "INSTALLING $file" + + tmp_location="${install_location}/tmp" + mkdir "$tmp_location" + + # Extract the archive + printf "\nExtracting %s to %s\n\n" "$file" "$tmp_location" + echo "+ tar -xvf $file -C $tmp_location" + tar -xvf "$file" -C "$tmp_location" + exit_code="$?" + + if [ "$exit_code" -ne 0 ]; then + printf "\nError extracting %s\n" "$file" + exit "$exit_code"; + fi + + extracted_to="$tmp_location/$(tar -tf "$file" | cut -d'/' -f1 | head -n1)" + + # Install to $install_location + printf "\nInstalling %s\n\n" "$file" + + echo "+ find $extracted_to -maxdepth 1 | awk -F '/' '(NR> 1) {print $NF}' | xargs -I'{}' mv $extracted_to/{} $install_location/{}" + find "$extracted_to" -maxdepth 1 | awk -F '/' '(NR> 1) {print $NF}' | xargs -I'{}' cp -r "$extracted_to"/{} "$install_location"/ + exit_code="$?" + + if [ "$exit_code" -ne 0 ]; then + printf "\nError installing %s into %s\n" "$extracted_to" "$install_location" + exit "$exit_code"; + fi + + rm -rf "$tmp_location" + + printf "\nSuccessefully installed %s\n" "$file" +} + +arg="${1}" +case ${arg} in + "-h"|"help") + help;; + "-u"|"uninstall") + uninstall "$2";; + *) + install "$1";; +esac +