192 lines
4.6 KiB
Bash
Executable file
192 lines
4.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# ./install --system --forced
|
|
# --forced (not forced, does not delete old version, just ends in failure)
|
|
# --system (refault user)
|
|
|
|
# Defaults
|
|
force=false
|
|
system=false
|
|
|
|
# Parse arguments
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--forced)
|
|
forced=true
|
|
;;
|
|
--system)
|
|
system=true
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Set INSTALL_HOME to $HOME if not already set
|
|
: "${INSTALL_HOME:=$HOME}"
|
|
|
|
if [ "$system" = true ] && [ "$EUID" -ne 0 ]; then
|
|
echo "Not running as root. Re-running with sudo... $0"
|
|
exec sudo INSTALL_HOME="$INSTALL_HOME" "$0" "$@"
|
|
fi
|
|
|
|
REPO_URL="https://gitlab.com/artur.gurgul/home.git"
|
|
|
|
is_debian_like() {
|
|
if [ -r /etc/os-release ]; then
|
|
. /etc/os-release
|
|
if [ "$ID" = "debian" ] || printf '%s' "$ID_LIKE" | grep -qi 'debian'; then
|
|
return 0
|
|
fi
|
|
fi
|
|
[ -r /etc/debian_version ]
|
|
}
|
|
|
|
is_macos() {
|
|
[ "$(uname)" = "Darwin" ]
|
|
}
|
|
|
|
echo "Forced: $forced"
|
|
echo "System: $system"
|
|
|
|
# local
|
|
if [ "$system" = false ]; then
|
|
DAT_ROOT="\$HOME/.dat"
|
|
else
|
|
# system/linux
|
|
if is_debian_like; then
|
|
DAT_ROOT="/dat"
|
|
# MacOS
|
|
elif is_macos; then
|
|
DAT_ROOT="/opt/dat"
|
|
else
|
|
echo "OS is not supported"
|
|
exit -1
|
|
fi
|
|
fi
|
|
|
|
####################### ZSHRC ================================
|
|
|
|
echo "Create ~/.zshrc"
|
|
echo "export DAT_ROOT=$DAT_ROOT" > "$INSTALL_HOME/.zshrc"
|
|
echo ". \$DAT_ROOT/bin/zshrc/init" >> "$INSTALL_HOME/.zshrc"
|
|
|
|
export DAT_ROOT=$(echo "$DAT_ROOT" | envsubst)
|
|
|
|
####################### Dependencies =========================
|
|
|
|
debian_install_packages() {
|
|
# List of required packages
|
|
local packages=("git" "ruby")
|
|
local to_install=()
|
|
|
|
# Determine if we need to use sudo
|
|
local SUDO=""
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
SUDO="sudo"
|
|
fi
|
|
|
|
# Check each package
|
|
for pkg in "${packages[@]}"; do
|
|
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
|
|
to_install+=("$pkg")
|
|
fi
|
|
done
|
|
|
|
# Install missing packages
|
|
if [ "${#to_install[@]}" -gt 0 ]; then
|
|
# Update and upgrade
|
|
$SUDO apt update
|
|
$SUDO apt upgrade -y
|
|
|
|
$SUDO apt install -y "${to_install[@]}"
|
|
else
|
|
echo "All required packages are already installed."
|
|
fi
|
|
}
|
|
|
|
macos_install_packages() {
|
|
# List of required packages
|
|
local packages=("git" "ruby")
|
|
local to_install=()
|
|
|
|
# Check for Homebrew
|
|
if ! command -v brew >/dev/null 2>&1; then
|
|
echo "Homebrew not found. Installing Homebrew..."
|
|
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
|
|
eval "$(/opt/homebrew/bin/brew shellenv)" # Adjust for Apple Silicon
|
|
fi
|
|
|
|
# Check each package
|
|
for pkg in "${packages[@]}"; do
|
|
if ! brew list "$pkg" >/dev/null 2>&1; then
|
|
to_install+=("$pkg")
|
|
fi
|
|
done
|
|
|
|
# Install missing packages
|
|
if [ "${#to_install[@]}" -gt 0 ]; then
|
|
# Update Homebrew
|
|
brew update
|
|
brew install "${to_install[@]}"
|
|
else
|
|
echo "All required packages are already installed."
|
|
fi
|
|
}
|
|
|
|
|
|
if is_debian_like; then
|
|
debian_install_packages
|
|
elif is_macos; then
|
|
macos_install_packages
|
|
fi
|
|
|
|
####################### Cleaning previous installs ============
|
|
## if forced
|
|
clean_previous_installs() {
|
|
if [ "$system" = true ]; then
|
|
rm -rf /usr/bin/dat
|
|
fi
|
|
rm -rf $DAT_ROOT
|
|
}
|
|
|
|
if [ "$forced" = true ]; then
|
|
clean_previous_installs
|
|
fi
|
|
|
|
####################### Installing... =========================
|
|
|
|
install_dat() {
|
|
if [ -d "$DAT_ROOT" ]; then
|
|
if [ -d "$DAT_ROOT/.git" ]; then
|
|
echo "Directory exists and is a Git repository. Pulling latest changes..."
|
|
git -C "$DAT_ROOT" pull
|
|
else
|
|
echo "Directory exists but is not a Git repository."
|
|
read -p "Do you want to delete it and clone the repository? [y/N]: " confirm
|
|
if [[ "$confirm" =~ ^[Yy]$ ]]; then
|
|
rm -rf "$DAT_ROOT"
|
|
echo "Cloning repository..."
|
|
git clone $REPO_URL "$DAT_ROOT"
|
|
else
|
|
echo "Aborting installation."
|
|
return 1
|
|
fi
|
|
fi
|
|
else
|
|
echo "Directory does not exist. Cloning repository..."
|
|
git clone $REPO_URL "$DAT_ROOT"
|
|
fi
|
|
}
|
|
install_dat
|
|
|
|
####################### Make it global ========================
|
|
# Not done yet
|
|
install_global_zsh_deb() {
|
|
ln -s /dat/bin/dat /usr/bin/dat
|
|
cat > /etc/profile.d/dat.sh <<EOF
|
|
export RUBYLIB=$DAT_ROOT
|
|
EOF
|
|
}
|
|
|
|
####################### Make shell setable by user =============
|
|
####################### Set daemon that updates the dat ========
|
|
|