Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 38 additions & 94 deletions setup
Original file line number Diff line number Diff line change
@@ -1,102 +1,46 @@
#!/usr/bin/env bash

print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}

red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }

section() {
printf "\n=== %s\n" "$(green_bold "$@")"
}

sudo_copy() {
printf "%s => %s\n" "$(blue "$(printf '%-25s' "$1")")" "$2"

if [[ -n "$3" ]]; then
$sudo install -m "$3" "$1" "$2"
else
$sudo install "$1" "$2"
fi
}

copy_executable() {
sudo_copy "$1" "/usr/local/bin/" "755"
}

copy_man() {
for file in "$1"/*.[1-8]; do
section="${file##*.}"
target_dir="/usr/local/share/man/man$section/"

if [[ ! -d "$target_dir" ]]; then
$sudo mkdir -p "$target_dir"
set -euo pipefail

prepare_installer() {
echo "Initializing installer..."
SSI_VERSION="0.1.5"

# Set up tmp dir where all work is done
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
cd "$tmpdir"
export PATH="$tmpdir:$PATH"

# Download ssi if needed
if ! command -v ssi >/dev/null 2>&1 || [ "$(ssi --version 2>/dev/null)" != "$SSI_VERSION" ]; then
if command -v curl >/dev/null 2>&1; then
curl -fSsL https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi -o ssi
elif command -v wget >/dev/null 2>&1; then
wget -nv -O ssi https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi
else
echo "Error: please install wget or curl, then try again" >&2
exit 1
fi

sudo_copy "$file" "$target_dir"
done

if command_exist makewhatis; then
$sudo makewhatis /usr/local/man
chmod +x ssi
fi
}

command_exist() {
[[ -x "$(command -v "$1")" ]]
}

need() {
command_exist "$1" || fail "Cannot run $1"
printf "%s found\n" "$(blue " $1")"
}

onerror() {
local exit_status=$?
printf "\n=== %s\n" "$(red "Aborted with exit status $exit_status")"
exit $exit_status
}

fail() {
printf "$(red 'ERR') %s\n" "$*"
return 1
}

initialize() {
set -e
trap 'onerror' ERR

# Figure out if we need sudo or not
sudo=''
if [[ $EUID -ne 0 ]]; then
sudo='sudo'
fi

repo="$1"
repo_url="https://github.com/${repo}.git"
pushd "$(mktemp -d)" >/dev/null
}

section "Initializing"
initialize "DannyBen/alf"

section "Checking for pre-requisites"
need git
prepare_installer

section "Cloning $repo"
git clone --depth 1 "$repo_url" .
if ! command -v git >/dev/null 2>&1; then
echo "Error: please install git and try again" >&2
exit 1
fi

section "Copying files"
copy_executable 'alf'
copy_man 'doc'
ssi log info Installing alf
ssi log debug Cloning github repo
git clone -q --depth 1 https://github.com/DannyBen/alf.git alf
ssi install bin alf/alf
ssi install man alf/doc

section "Done"
alf --version
if command -v alf >/dev/null 2>&1; then
ssi log info "alf --version : $(alf --version)"
else
ssi log warn "alf not found on PATH after install"
fi
ssi log debug Installation complete
104 changes: 28 additions & 76 deletions uninstall
Original file line number Diff line number Diff line change
@@ -1,80 +1,32 @@
#!/usr/bin/env bash

print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}

red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
green_bold() { print_in_color "\e[1;32m" "$*"; }
blue() { print_in_color "\e[34m" "$*"; }

section() {
printf "\n=== %s\n" "$(green_bold "$@")"
}

command_exist() {
[[ -x "$(command -v "$1")" ]]
}

rm_executable() {
if [[ -z "$1" ]]; then return; fi

printf "%s %s\n" "$(blue rm)" "$1"
$sudo rm -f "/usr/local/bin/$1"

if command_exist "$1"; then
fail "Command $1 still exists"
set -euo pipefail

prepare_installer() {
echo "Initializing installer..."
SSI_VERSION="0.1.5"

# Set up tmp dir where all work is done
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
cd "$tmpdir"
export PATH="$tmpdir:$PATH"

# Download ssi if needed
if ! command -v ssi >/dev/null 2>&1 || [ "$(ssi --version 2>/dev/null)" != "$SSI_VERSION" ]; then
if command -v curl >/dev/null 2>&1; then
curl -fSsL https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi -o ssi
elif command -v wget >/dev/null 2>&1; then
wget -nv -O ssi https://github.com/DannyBen/ssi/releases/download/v$SSI_VERSION/ssi
else
echo "Error: please install wget or curl, then try again" >&2
exit 1
fi
chmod +x ssi
fi
}

rm_man() {
if [[ ! -d "/usr/local/share/man/man1/" ]]; then return; fi
if [[ -z "$1" ]]; then return; fi

printf "%s %s\n" "$(blue rm)" "/usr/local/share/man/man1/$1*.1"
$sudo rm -f /usr/local/share/man/man1/"$1"*.1

printf "%s %s\n" "$(blue rm)" "/usr/local/share/man/man5/$1*.5"
$sudo rm -f /usr/local/share/man/man5/"$1"*.5

if command_exist makewhatis; then
$sudo makewhatis /usr/local/man
fi
}

onerror() {
local exit_status=$?
printf "\n=== %s\n" "$(red "Aborted with exit status $exit_status")"
exit $exit_status
}

fail() {
printf "$(red 'ERR') %s\n" "$*"
return 1
}

initialize() {
set -e
trap 'onerror' ERR

# Figure out if we need sudo or not
sudo=''
if [[ $EUID -ne 0 ]]; then
sudo='sudo'
fi
}

initialize

section "Removing files"
rm_executable 'alf'
rm_man 'alf'

section "Done"
prepare_installer
ssi log info "Uninstalling alf"
ssi uninstall bin alf
ssi uninstall man alf --all
ssi log debug Uninstallation complete