summaryrefslogtreecommitdiffstats
path: root/web/install.sh
blob: 998b262929fef7e2716a53dc20ec38ab931d49ef (plain) (blame)
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
#!/usr/bin/env bash
# Zagan-Bet installer for Linux.
#
# Asks the site which build is newest, verifies its SHA-256, installs it for
# the current user and adds a launcher. Point it at a staging site with
# ZAGAN_SITE=http://127.0.0.1:8899.
set -euo pipefail

SITE=${ZAGAN_SITE:-https://zagan.2137697.xyz}
PLATFORM=linux
ARCH=x86_64

say() { printf '%s\n' "$*"; }
die() { printf 'install: %s\n' "$*" >&2; exit 1; }

command -v curl >/dev/null 2>&1 || die "curl is required"
command -v sha256sum >/dev/null 2>&1 || die "sha256sum is required"

latest=$(curl -fsSL --max-time 30 "$SITE/latest.php") \
  || die "could not read $SITE/latest.php"
line=$(printf '%s\n' "$latest" \
  | awk -v p="$PLATFORM" -v a="$ARCH" '$1 == "gui" && $2 == p && $3 == a { print; exit }')
[ -n "$line" ] || die "no Linux build is published yet"

version=$(printf '%s\n' "$line" | awk '{print $4}')
binary=$(printf '%s\n' "$line" | awk '{print $5}')
sha=$(printf '%s\n' "$line" | awk '{print $6}')
[ -n "$version" ] && [ -n "$binary" ] && [ -n "$sha" ] \
  || die "the latest-build file is malformed"

say "Newest Zagan-Bet is $version."

here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source_file=""
for candidate in "$here/$binary" "./$binary" "$HOME/Downloads/$binary"; do
  if [ -f "$candidate" ]; then
    source_file=$candidate
    break
  fi
done

cleanup=""
if [ -z "$source_file" ]; then
  cleanup=$(mktemp) || die "cannot create a temporary file"
  say "Downloading $binary..."
  curl -fsSL --max-time 300 "$SITE/bin/gui/$binary" -o "$cleanup" \
    || die "download failed"
  source_file=$cleanup
fi
trap 'if [ -n "$cleanup" ]; then rm -f "$cleanup"; fi' EXIT

actual=$(sha256sum "$source_file" | awk '{print $1}')
[ "$actual" = "$sha" ] \
  || die "checksum mismatch: expected $sha, got $actual"

bin_dir="${XDG_BIN_HOME:-$HOME/.local/bin}"
data_dir="${XDG_DATA_HOME:-$HOME/.local/share}"
app="$bin_dir/zagan-bet-gui"
desktop_dir="$data_dir/applications"
icon_dir="$data_dir/icons/hicolor/scalable/apps"

mkdir -p "$bin_dir" "$desktop_dir" "$icon_dir"
install -m 0755 "$source_file" "$app"

cat > "$icon_dir/zagan-bet.svg" <<'ICON'
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
  <rect width="64" height="64" rx="14" fill="#0a0e12"/>
  <rect x="9" y="9" width="46" height="46" rx="10" fill="#0f1a14" stroke="#1f7d52" stroke-width="2"/>
  <path d="M22 20h20L22 44h20" fill="none" stroke="#4ec27b" stroke-width="6"
        stroke-linecap="round" stroke-linejoin="round"/>
</svg>
ICON

cat > "$desktop_dir/zagan-bet.desktop" <<DESKTOP
[Desktop Entry]
Type=Application
Name=Zagan-Bet
Comment=Slots, roulette, blackjack and crash
Exec=$app
Icon=zagan-bet
Terminal=false
Categories=Game;
DESKTOP
chmod 0644 "$desktop_dir/zagan-bet.desktop" "$icon_dir/zagan-bet.svg"

if command -v update-desktop-database >/dev/null 2>&1; then
  update-desktop-database "$desktop_dir" >/dev/null 2>&1 || true
fi
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
  gtk-update-icon-cache -q "$data_dir/icons/hicolor" >/dev/null 2>&1 || true
fi

say "Installed Zagan-Bet $version to $app"
say "A launcher was added to your applications menu."
case ":$PATH:" in
  *":$bin_dir:"*) ;;
  *) say "Add $bin_dir to PATH to start it as 'zagan-bet-gui'." ;;
esac