aboutsummaryrefslogtreecommitdiff
path: root/install_script.sh
blob: 1b3d92aeb64d6bb56b058ce41d2c3d7e577fd82a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
set -euo pipefail

OWNER="bruce-hill"
REPO="tomo"

# Fetch latest release tag
TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/releases/latest" \
      | grep -Po '"tag_name": "\K.*?(?=")')

if [[ -z "$TAG" ]]; then
    echo "Failed to get latest release tag"
    exit 1
fi

# Detect platform
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
    Linux)
        case "$ARCH" in
            x86_64) FILE="tomo-linux-x86_64.tar.gz" ;;
            aarch64|arm64) FILE="tomo-linux-aarch64.tar.gz" ;;
            *) echo "Unsupported architecture: $ARCH"; exit 1 ;;
        esac
        ;;
    Darwin)
        FILE="tomo-macos-universal.tar.gz"
        ;;
    *)
        echo "Unsupported OS: $OS"
        exit 1
        ;;
esac

# Download the artifact (if not present)
if ! [ -e "$FILE" ]; then
    URL="https://github.com/$OWNER/$REPO/releases/download/$TAG/$FILE"
    echo "Downloading $URL ..."
    curl -L -o "$FILE" "$URL"
    echo "Downloaded $FILE"
fi

# Download checksum (if not present)
if ! [ -e "$FILE.sha256" ]; then
    CHECKSUM_URL="$URL.sha256"
    echo "Downloading checksum $CHECKSUM_URL ..."
    curl -L -o "$FILE.sha256" "$CHECKSUM_URL"
fi

# Verify checksum
shasum --check "$FILE.sha256"
echo "Verified checksum"

# Configure `doas` vs `sudo`
if command -v doas >/dev/null 2>&1; then  
    SUDO="doas"  
elif command -v sudo >/dev/null 2>&1; then  
    SUDO="sudo"  
else  
    echo "Neither doas nor sudo found." >&2  
    exit 1  
fi  

# Autodetect package manager:
if [ -z "${PACKAGE_MANAGER:-}" ]; then
    if command -v dnf >/dev/null 2>&1; then
        PACKAGE_MANAGER="dnf"
    elif command -v yay >/dev/null 2>&1; then
        PACKAGE_MANAGER="yay"
    elif command -v paru >/dev/null 2>&1; then
        PACKAGE_MANAGER="paru"
    elif command -v pacman >/dev/null 2>&1; then
        PACKAGE_MANAGER="pacman"
    elif command -v xbps-install >/dev/null 2>&1; then
        PACKAGE_MANAGER="xbps"
    elif command -v pkg_add >/dev/null 2>&1; then
        PACKAGE_MANAGER="pkg_add"
    elif command -v pkg >/dev/null 2>&1; then
        PACKAGE_MANAGER="freebsd-pkg"
    elif command -v brew >/dev/null 2>&1; then
        PACKAGE_MANAGER="brew"
    elif command -v port >/dev/null 2>&1; then
        PACKAGE_MANAGER="macports"
    elif command -v zypper >/dev/null 2>&1; then
        PACKAGE_MANAGER="zypper"
    elif command -v nix-env >/dev/null 2>&1; then
        PACKAGE_MANAGER="nix"
    elif command -v spack >/dev/null 2>&1; then
        PACKAGE_MANAGER="spack"
    elif command -v conda >/dev/null 2>&1; then
        PACKAGE_MANAGER="conda"
    elif command -v apt >/dev/null 2>&1; then
        PACKAGE_MANAGER="apt"
    elif command -v apt-get >/dev/null 2>&1; then
        PACKAGE_MANAGER="apt-get"
    else
        echo "Unsupported package manager" >&2
        exit 1
    fi
fi

# Install packages
echo 'Installing dependencies...'
case "$PACKAGE_MANAGER" in
    apt) $SUDO apt install libgc-dev libunistring-dev binutils libgmp-dev ;;
    apt-get) $SUDO apt-get install libgc-dev libunistring-dev binutils libgmp-dev ;;
    dnf) $SUDO dnf install gc-devel libunistring-devel binutils gmp-devel ;;
    pacman) $SUDO pacman -S gc libunistring binutils gmp ;;
    yay|paru) $PACKAGE_MANAGER -S gc libunistring binutils gmp ;;
    xbps) $SUDO xbps-install -S gc libunistring binutils gmp ;;
    pkg_add) $SUDO pkg_add boehm-gc libunistring binutils gmp ;;
    freebsd-pkg) $SUDO pkg install boehm-gc libunistring binutils gmp ;;
    brew) brew install bdw-gc libunistring binutils llvm gmp ;;
    macports) $SUDO port install boehm-gc libunistring binutils gmp ;;
    zypper) $SUDO zypper install gc-devel libunistring-devel binutils gmp-devel ;;
    nix) nix-env -iA nixpkgs.boehmgc.dev nixpkgs.libunistring nixpkgs.binutils nixpkgs.nixpkgs.gmp ;;
    spack) spack install boehm-gc libunistring binutils gmp ;;
    conda) conda install boehm-gc libunistring binutils gmp ;;
    *)
        echo "Unknown package manager: $PACKAGE_MANAGER" >&2
        exit 1
        ;;
esac

# Choose installation location
default_prefix='/usr/local'
if echo "$PATH" | tr ':' '\n' | grep -qx "$HOME/.local/bin"; then
    default_prefix="~/.local"
fi
printf '\033[1mChoose where to install Tomo (default: %s):\033[m ' "$default_prefix"
read DEST
if [ -z "$DEST" ]; then DEST="$default_prefix"; fi
DEST="${DEST/#\~/$HOME}"

# Install
if ! [ -w "$DEST" ]; then
    USER="$(ls -ld "$DEST" | awk '{print $$3}')"
    $(SUDO) -u "$USER" tar -xzf "$FILE" -C "$DEST" --strip-components=1 "tomo@$TAG"
else
    tar -xzf "$FILE" -C "$DEST" --strip-components=1 "tomo@$TAG"
fi
echo "Installed to $DEST"