upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/install-bitflash-wine.sh
blob: 91fef5963d0129688b51ec7db9d55e0f422a0c38 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
# Bitflash GUI installer for Linux (via Wine) — hardened
set -euo pipefail

INSTALL_DIR="$HOME/.btf-wine"
WINE_DATA="$HOME/.wine/drive_c/users/$USER/AppData/Roaming/Bitflash"
LINUX_DATA="$HOME/.bitflash"
BIN_DIR="$HOME/.local/bin"
REPO="Bitflash-sh/bitflash"

echo "==> Bitflash GUI installer"
echo

# ---- Safety: don't run as root (breaks Wine + pollutes /root)
if [ "$(id -u)" -eq 0 ]; then
    echo "ERROR: do not run this as root. Run as your normal user (it will call sudo only when needed)." >&2
    exit 1
fi

need() { command -v "$1" >/dev/null 2>&1; }

# ---- Wine
echo "==> Checking Wine..."
if ! need wine; then
    echo "    Installing Wine..."
    if ! need apt-get; then
        echo "ERROR: apt-get not found. Install 'wine' manually, then re-run." >&2
        exit 1
    fi
    sudo apt-get update -qq
    sudo apt-get install -y wine
fi
echo "    Wine: $(wine --version)"

# ---- db-util (db_dump / db_load for wallet conversion)
echo "==> Checking db-util..."
if ! need db_dump; then
    echo "    Installing db-util..."
    sudo apt-get install -y db-util
fi
echo "    db_dump: $(command -v db_dump)"

# ---- unzip + sha256sum must exist
for tool in unzip sha256sum curl; do
    if ! need "$tool"; then
        echo "    Installing $tool..."
        sudo apt-get install -y "$tool"
    fi
done

# ---- Resolve latest release (single API call)
echo "==> Fetching latest release metadata from GitHub..."
API_JSON=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest")

RELEASE_URL=$(printf '%s\n' "$API_JSON" | grep '"browser_download_url"' \
    | grep -iE 'win|windows|gui' | head -1 | cut -d '"' -f 4 || true)
[ -z "$RELEASE_URL" ] && RELEASE_URL=$(printf '%s\n' "$API_JSON" | grep '"browser_download_url"' \
    | grep -iE '\.zip"?$' | head -1 | cut -d '"' -f 4 || true)

if [ -z "$RELEASE_URL" ]; then
    echo "ERROR: no downloadable release asset found at https://github.com/$REPO/releases" >&2
    exit 1
fi
ASSET_NAME=$(basename "$RELEASE_URL")
echo "    Asset: $ASSET_NAME"

# checksum manifest published alongside the release (SHA256SUMS)
SUMS_URL=$(printf '%s\n' "$API_JSON" | grep '"browser_download_url"' \
    | grep -iE 'sha256sums|checksums|\.sha256"?$' | head -1 | cut -d '"' -f 4 || true)

# ---- Download the release
TMPZIP=$(mktemp /tmp/bitflash-XXXXXX.zip)
cleanup() { rm -f "$TMPZIP" "${TMPSUMS:-}"; }
trap cleanup EXIT
echo "==> Downloading release..."
curl -fL --retry 3 --retry-delay 2 -o "$TMPZIP" "$RELEASE_URL"
echo "    Downloaded: $(du -h "$TMPZIP" | cut -f1)"

# ---- MANDATORY integrity check
echo "==> Verifying integrity..."
GOT=$(sha256sum "$TMPZIP" | awk '{print $1}')
if [ -n "${EXPECTED_SHA256:-}" ]; then
    # Strongest: user pinned a known-good hash out-of-band.
    if [ "$GOT" != "$EXPECTED_SHA256" ]; then
        echo "ERROR: checksum mismatch. expected $EXPECTED_SHA256, got $GOT" >&2
        exit 1
    fi
    echo "    OK (matches pinned EXPECTED_SHA256)"
elif [ -n "$SUMS_URL" ]; then
    TMPSUMS=$(mktemp /tmp/bitflash-sums-XXXXXX)
    curl -fsSL --retry 3 -o "$TMPSUMS" "$SUMS_URL"
    WANT=$(grep -iF "$ASSET_NAME" "$TMPSUMS" | awk '{print $1}' | head -1 || true)
    if [ -z "$WANT" ]; then
        echo "ERROR: no checksum for $ASSET_NAME in published SHA256SUMS." >&2
        exit 1
    fi
    if [ "$GOT" != "$WANT" ]; then
        echo "ERROR: checksum mismatch. published $WANT, got $GOT" >&2
        exit 1
    fi
    echo "    OK (matches published SHA256SUMS)"
else
    echo "ERROR: no checksum available for this release." >&2
    echo "       Publish a SHA256SUMS asset with the release, or re-run with" >&2
    echo "       EXPECTED_SHA256=<hash> to verify against a known-good value." >&2
    echo "       (To bypass at your own risk: ALLOW_UNVERIFIED=1)" >&2
    if [ "${ALLOW_UNVERIFIED:-0}" != "1" ]; then
        exit 1
    fi
    echo "    WARNING: proceeding UNVERIFIED (ALLOW_UNVERIFIED=1). sha256=$GOT"
fi

# ---- Extract
echo "==> Extracting to $INSTALL_DIR..."
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
unzip -j -o "$TMPZIP" -d "$INSTALL_DIR" >/dev/null

# ---- Rename backslash-mangled entries (Windows zip paths)
count=0
for f in "$INSTALL_DIR"/*; do
    [ -f "$f" ] || continue
    base=$(basename "$f")
    newname=$(printf '%s' "$base" | sed 's/.*\\//')
    if [ "$newname" != "$base" ]; then
        mv "$f" "$INSTALL_DIR/$newname"
        count=$((count+1))
    fi
done
echo "    Renamed $count files; total: $(ls "$INSTALL_DIR" | wc -l)"

# Locate the Bitflash exe case-insensitively (zip may ship Bitflash.exe or bitflash.exe)
EXE=$(find "$INSTALL_DIR" -maxdepth 1 -iname 'bitflash.exe' | head -1)
if [ -z "$EXE" ]; then
    echo "ERROR: bitflash exe not found after extract. Contents:" >&2
    ls "$INSTALL_DIR" >&2
    exit 1
fi
echo "    Found $(basename "$EXE")"

# ---- Init Wine data dir
echo "==> Initializing Wine data directory..."
WINEDLLOVERRIDES="libdb_cxx-6.2=n" wine "$EXE" &
WINE_PID=$!
created=0
for i in $(seq 1 30); do
    sleep 1
    if [ -f "$WINE_DATA/wallet.dat" ]; then
        echo "    wallet.dat created after ${i}s"
        created=1
        break
    fi
done
kill "$WINE_PID" 2>/dev/null || true
wait "$WINE_PID" 2>/dev/null || true
sleep 1
mkdir -p "$WINE_DATA"
[ "$created" -eq 1 ] || echo "    NOTE: Wine did not create wallet.dat in 30s; continuing."

# ---- Optional wallet import (with backup)
echo
if [ -f "$LINUX_DATA/wallet.dat" ]; then
    echo "==> Found existing Linux wallet at $LINUX_DATA/wallet.dat"
    read -r -p "    Import it into the GUI wallet? [Y/n] " ans
    ans=${ans:-Y}
    if [[ "$ans" =~ ^[Yy] ]]; then
        if [ -f "$WINE_DATA/wallet.dat" ]; then
            BK="$WINE_DATA/wallet.dat.bak.$(date +%Y%m%d%H%M%S)"
            cp "$WINE_DATA/wallet.dat" "$BK"
            echo "    Backed up existing GUI wallet -> $BK"
        fi
        DUMP=$(mktemp); CONVERTED=$(mktemp); rm -f "$CONVERTED"
        echo "    Converting Linux wallet format..."
        db_dump -p "$LINUX_DATA/wallet.dat" > "$DUMP"
        db_load -f "$DUMP" "$CONVERTED"
        cp "$CONVERTED" "$WINE_DATA/wallet.dat"
        rm -f "$DUMP" "$CONVERTED"
        echo "    Wallet imported."
    else
        echo "    Skipped. Re-run to import later."
    fi
else
    echo "==> No Linux wallet found, starting fresh."
fi

# ---- Launcher
echo "==> Creating 'bitflash' command..."
mkdir -p "$BIN_DIR"
LAUNCHER_FILE="$BIN_DIR/bitflash"
cat > "$LAUNCHER_FILE" <<EOF
#!/usr/bin/env bash
RELAY="\${BTF_RELAY:-}"
ANNOUNCE=""
[ -n "\$RELAY" ] && ANNOUNCE="-announcerelay=\$RELAY"
WINEDLLOVERRIDES="libdb_cxx-6.2=n" wine "$EXE" \$ANNOUNCE "\$@"
EOF
chmod +x "$LAUNCHER_FILE"

if [[ ":$PATH:" != *":$BIN_DIR:"* ]] && ! grep -qF 'export PATH="$HOME/.local/bin:$PATH"' "$HOME/.bashrc" 2>/dev/null; then
    echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.bashrc"
    echo "    Added ~/.local/bin to PATH (restart shell or: source ~/.bashrc)"
fi

echo
echo "=================================================================="
echo " DONE."
echo " Launch:   bitflash"
echo " Mine:     bitflash -gen"
echo " Relay:    bitflash -announcerelay=YOUR_HOST:8434"
echo " Data:     $WINE_DATA"
echo "=================================================================="