Files
main/update.sh

158 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE_URL="${BASE_URL:-https://git.jewguard.xyz/linusware/main/raw/branch/main}"
INSTALL_DIR="${LINUSWARE_DIR:-${HOME}/linusware}"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() {
echo -e "${GREEN}[*]${NC} $1"
}
warn() {
echo -e "${YELLOW}[!]${NC} $1"
}
error() {
echo -e "${RED}[x]${NC} $1"
}
command -v curl >/dev/null 2>&1 || { error "curl is required"; exit 1; }
command -v md5sum >/dev/null 2>&1 || { error "md5sum is required"; exit 1; }
command -v tar >/dev/null 2>&1 || { error "tar is required"; exit 1; }
download() {
local url="$1" dest="$2"
local tmp; tmp="$(mktemp)"
curl -fsSL -o "$tmp" "$url" || { rm -f "$tmp"; return 1; }
mv "$tmp" "$dest"
}
update_edition() {
local edition="$1"
local run_path="${INSTALL_DIR}/${edition}/linusware/run.sh"
local ver_file="${INSTALL_DIR}/${edition}/version.txt"
[ -f "$run_path" ] || return 0
local cur_ver=""
[ -f "$ver_file" ] && cur_ver=$(cat "$ver_file")
local tmp_status; tmp_status="$(mktemp)"
if ! download "${BASE_URL}/${edition}/status.json" "$tmp_status"; then
rm -f "$tmp_status"
warn "${edition}: could not fetch remote status"
return
fi
local remote_ver updated
remote_ver=$(sed -n 's/.*"version":[[:space:]]*"\([^"]*\)".*/\1/p' "$tmp_status")
updated=$(sed -n 's/.*"updated":[[:space:]]*\([a-z]*\).*/\1/p' "$tmp_status")
rm -f "$tmp_status"
if [ "$updated" != "true" ]; then
info "${edition}: not available (not yet released)"
return
fi
if [ "$remote_ver" = "$cur_ver" ]; then
info "${edition}: already up-to-date (v${cur_ver})"
return
fi
info "${edition}: ${cur_ver:-none} -> v${remote_ver}"
local md5_file; md5_file="$(mktemp)"
if ! download "${BASE_URL}/${edition}/bin/MD5SUM" "$md5_file"; then
rm -f "$md5_file"
warn "${edition}: could not fetch MD5SUM"
return
fi
local expected_hash
expected_hash=$(tr -cd '[:xdigit:]' < "$md5_file"); rm -f "$md5_file"
local tmp_archive; tmp_archive="$(mktemp)"
if ! download "${BASE_URL}/${edition}/bin/linusware.tar.gz" "$tmp_archive"; then
rm -f "$tmp_archive"
warn "${edition}: download failed"
return
fi
local actual_hash
actual_hash=$(md5sum "$tmp_archive" | cut -d' ' -f1)
if [ "$actual_hash" != "$expected_hash" ]; then
rm -f "$tmp_archive"
error "${edition}: MD5 mismatch"
return
fi
rm -rf "${INSTALL_DIR}/${edition}/linusware"
tar -xzf "$tmp_archive" -C "${INSTALL_DIR}/${edition}"
rm -f "$tmp_archive"
local new_run="${INSTALL_DIR}/${edition}/linusware/run.sh"
chmod +x "$new_run"
echo "$remote_ver" > "$ver_file"
info "${edition}: updated to v${remote_ver}"
}
# main
echo -e "${GREEN}------ LinusWare Updater ------${NC}"
echo ""
for dir in "${INSTALL_DIR}"/*/; do
[ -d "$dir" ] || continue
update_edition "$(basename "$dir")"
done
# --- self-update ---
self_update() {
local script="$1"
local url="${BASE_URL}/${script}"
local cache="${INSTALL_DIR}/.scripts"
local remote_id
case "$url" in
file://*)
local filepath="${url#file://}"
remote_id=$(stat -c %Y "$filepath" 2>/dev/null) || return 0
;;
*)
local headers; headers=$(curl -fsSL -I "$url" 2>/dev/null) || return 0
remote_id=$(echo "$headers" | grep -im1 '^etag:' | tr -d '\r') || true
remote_id="${remote_id#*: }"
if [ -z "$remote_id" ]; then
remote_id=$(echo "$headers" | grep -im1 '^last-modified:' | tr -d '\r') || true
remote_id="${remote_id#*: }"
fi
[ -n "$remote_id" ] || return 0
;;
esac
local local_id
local_id=$(grep -F "$script " "$cache" 2>/dev/null | head -1 | cut -d' ' -f2-) || true
[ "$remote_id" = "$local_id" ] && { info "${script}: up-to-date"; return; }
info "${script}: updating..."
local tmp; tmp="$(mktemp)"
curl -fsSL -o "$tmp" "$url" || { rm -f "$tmp"; warn "${script}: download failed"; return; }
chmod +x "$tmp"
mv "$tmp" "${INSTALL_DIR}/${script}"
local tmp_cache; tmp_cache="$(mktemp)"
grep -vF "$script " "$cache" 2>/dev/null > "$tmp_cache" || true
echo "$script $remote_id" >> "$tmp_cache"
mv "$tmp_cache" "$cache"
info "${script}: updated"
}
self_update "launcher.sh"
self_update "update.sh"
echo ""
info "Done."