-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·266 lines (227 loc) · 8.2 KB
/
install.sh
File metadata and controls
executable file
·266 lines (227 loc) · 8.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash
set -euo pipefail
# OpenFOIA installer
# Usage: curl -fsSL https://raw.githubusercontent.com/JordanCoin/openfoia/main/install.sh | bash
#
# Portable USB install:
# cd /Volumes/MY_USB
# curl -fsSL https://raw.githubusercontent.com/JordanCoin/openfoia/main/install.sh | bash --portable
REPO="JordanCoin/openfoia"
info() { printf '\033[0;34m%s\033[0m\n' "$*"; }
ok() { printf '\033[0;32m%s\033[0m\n' "$*"; }
warn() { printf '\033[0;33m%s\033[0m\n' "$*"; }
die() { printf '\033[0;31m%s\033[0m\n' "$*" >&2; exit 1; }
# --- Detect portable mode ---
PORTABLE=false
MINIMAL=false
for arg in "$@"; do
case "$arg" in
--portable) PORTABLE=true ;;
--minimal) MINIMAL=true ;;
esac
done
if [ -f ".openfoia-portable" ] || [ -n "${OPENFOIA_DATA_DIR:-}" ]; then
PORTABLE=true
fi
# --- Set paths based on mode ---
if [ "$PORTABLE" = true ]; then
# Everything on the USB / current directory
BASE_DIR="$(pwd)"
INSTALL_DIR="${BASE_DIR}/bin"
VENV_DIR="${BASE_DIR}/.openfoia-venv"
DATA_DIR="${BASE_DIR}/openfoia-data"
# Create portable marker
touch "${BASE_DIR}/.openfoia-portable"
else
# Standard install — home directory
BASE_DIR="$HOME"
INSTALL_DIR="${OPENFOIA_INSTALL_DIR:-$HOME/.local/bin}"
VENV_DIR="${OPENFOIA_VENV_DIR:-$HOME/.openfoia-venv}"
DATA_DIR="$HOME/.openfoia"
fi
# --- Detect platform ---
detect_platform() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux) os="linux" ;;
Darwin) os="macos" ;;
MINGW*|MSYS*|CYGWIN*) os="windows" ;;
*) die "Unsupported OS: $os" ;;
esac
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) die "Unsupported architecture: $arch" ;;
esac
echo "${os}-${arch}"
}
# --- Download binary from latest release ---
download_binary() {
local platform="$1"
local name="pdf-extract-${platform}"
[ "$platform" = "windows-x86_64" ] && name="${name}.exe"
info "Downloading pdf-extract for ${platform}..."
local url
# Check all releases for binaries (they live on whichever release the
# glyph-api CI pushed them to — not necessarily the latest release)
url=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases" \
| grep "browser_download_url.*${name}" \
| head -1 \
| cut -d '"' -f 4) || true
if [ -z "$url" ]; then
warn "No pdf-extract binary found for ${platform} in latest release."
warn "PDF text extraction will be unavailable. OCR (tesseract) will be used instead."
return 1
fi
mkdir -p "$INSTALL_DIR"
curl -fsSL "$url" -o "${INSTALL_DIR}/pdf-extract"
# Verify SHA256 checksum if .sha256 file exists in the release
local sha_url="${url}.sha256"
local expected_checksum
expected_checksum=$(curl -fsSL "$sha_url" 2>/dev/null | awk '{print $1}') || true
if [ -n "$expected_checksum" ]; then
local actual_checksum
if command -v sha256sum &>/dev/null; then
actual_checksum=$(sha256sum "${INSTALL_DIR}/pdf-extract" | awk '{print $1}')
elif command -v shasum &>/dev/null; then
actual_checksum=$(shasum -a 256 "${INSTALL_DIR}/pdf-extract" | awk '{print $1}')
else
warn "No sha256sum or shasum found — skipping checksum verification."
actual_checksum="$expected_checksum" # skip comparison
fi
if [ "$actual_checksum" != "$expected_checksum" ]; then
rm -f "${INSTALL_DIR}/pdf-extract"
die "Checksum mismatch for pdf-extract! Expected ${expected_checksum}, got ${actual_checksum}. Aborting."
fi
ok "Checksum verified (SHA256)."
else
warn "No .sha256 file found in release — skipping checksum verification."
fi
chmod +x "${INSTALL_DIR}/pdf-extract"
ok "Installed pdf-extract to ${INSTALL_DIR}/pdf-extract"
}
# --- Install Python package ---
install_python() {
info "Installing openfoia..."
# Find Python 3
local python=""
for candidate in python3 python; do
if command -v "$candidate" &>/dev/null; then
version=$("$candidate" --version 2>&1 | grep -oE '[0-9]+\.[0-9]+')
major=$(echo "$version" | cut -d. -f1)
minor=$(echo "$version" | cut -d. -f2)
if [ "$major" -ge 3 ] && [ "$minor" -ge 11 ]; then
python="$candidate"
break
fi
fi
done
if [ -z "$python" ]; then
die "Python 3.11+ not found. Install Python first: https://python.org"
fi
info "Using $python ($($python --version))"
# Determine extras
# Default: core only (~30MB). GLiNER adds ~2.3GB (PyTorch + model).
# Let the user opt in — don't surprise them with a 2GB download.
local extras=""
if [ "$MINIMAL" = false ]; then
# Check if user explicitly wants NER
for arg in "$@"; do
case "$arg" in
--ner) extras="[ner]"; info "Including GLiNER entity extraction (~2GB download)" ;;
--all) extras="[all]"; info "Full install (all optional features)" ;;
esac
done
fi
if [ -z "$extras" ]; then
info "Core install (~30MB). For entity extraction: openfoia install-extras ner"
fi
local pkg="git+https://github.com/${REPO}.git"
# For non-portable: try pipx first
if [ "$PORTABLE" = false ] && command -v pipx &>/dev/null; then
pipx install "${pkg}${extras}"
ok "Installed via pipx"
return
fi
# Create isolated venv
info "Creating isolated environment at ${VENV_DIR}..."
"$python" -m venv "$VENV_DIR"
"${VENV_DIR}/bin/pip" install --upgrade pip -q
"${VENV_DIR}/bin/pip" install "${pkg}${extras}"
# Symlink the openfoia command
mkdir -p "$INSTALL_DIR"
ln -sf "${VENV_DIR}/bin/openfoia" "${INSTALL_DIR}/openfoia"
ok "Installed openfoia CLI (venv at ${VENV_DIR})"
}
# --- Ensure PATH ---
ensure_path() {
case ":$PATH:" in
*":${INSTALL_DIR}:"*) return ;;
esac
if [ "$PORTABLE" = true ]; then
warn "Add to your PATH for this session:"
warn " export PATH=\"${INSTALL_DIR}:\$PATH\""
return
fi
local shell_rc=""
if [ -n "${ZSH_VERSION:-}" ] || [ "$(basename "$SHELL")" = "zsh" ]; then
shell_rc="$HOME/.zshrc"
elif [ -n "${BASH_VERSION:-}" ] || [ "$(basename "$SHELL")" = "bash" ]; then
shell_rc="$HOME/.bashrc"
fi
if [ -n "$shell_rc" ]; then
echo "export PATH=\"${INSTALL_DIR}:\$PATH\"" >> "$shell_rc"
warn "Added ${INSTALL_DIR} to PATH in ${shell_rc}. Restart your shell or run:"
warn " export PATH=\"${INSTALL_DIR}:\$PATH\""
fi
}
# --- Main ---
main() {
info ""
if [ "$PORTABLE" = true ]; then
info " OpenFOIA — PORTABLE install (everything stays here)"
info " Location: $(pwd)"
else
info " OpenFOIA — your data never leaves your machine"
fi
info ""
platform=$(detect_platform)
info "Platform: ${platform}"
install_python
download_binary "$platform" || true
ensure_path
# Initialize database
mkdir -p "$DATA_DIR"
if [ "$PORTABLE" = true ]; then
export OPENFOIA_DATA_DIR="$DATA_DIR"
fi
if command -v "${INSTALL_DIR}/openfoia" &>/dev/null; then
"${INSTALL_DIR}/openfoia" init 2>/dev/null || true
elif command -v openfoia &>/dev/null; then
openfoia init 2>/dev/null || true
fi
echo ""
ok "OpenFOIA installed."
info ""
if [ "$PORTABLE" = true ]; then
info " PORTABLE MODE — everything is in $(pwd)"
info ""
info " To use: export PATH=\"${INSTALL_DIR}:\$PATH\""
info " openfoia guide"
info ""
info " Unplug the USB and nothing remains on the host."
else
info " openfoia guide Quickstart walkthrough"
info " openfoia serve Start the local web UI"
info " openfoia request new File a FOIA request"
info " openfoia --help See all commands"
info ""
info " All data stored in ${DATA_DIR}"
fi
info ""
info " To uninstall: curl -fsSL https://raw.githubusercontent.com/${REPO}/main/uninstall.sh | bash"
info ""
}
main "$@"