#!/bin/bash # Burrow installer and updater. # # curl -fsSL https://rerereally.com/burrow/install.sh | bash install or update # curl -fsSL https://rerereally.com/burrow/install.sh | bash -s -- --uninstall # curl -fsSL https://rerereally.com/burrow/install.sh | bash -s -- --force reinstall # # It reads latest.json next to this script, downloads that release with curl, checks its # SHA-256, the app's bundle identifier and its code signature, then swaps it into place. # The previous copy is kept until the new one is in place, so a failed step changes nothing. # # The app's own updater runs the bundled copy of this script with: # --from-zip --sha256 --target --wait-pid --relaunch # # Environment: # BURROW_BASE_URL release folder (default https://rerereally.com/burrow) # BURROW_INSTALL_DIR folder to install into (default /Applications, else ~/Applications) set -euo pipefail BASE_URL="${BURROW_BASE_URL:-https://rerereally.com/burrow}" BUNDLE_ID="fit.burrow.macos" APP_NAME="Burrow.app" LAUNCH_AGENT_LABEL="fit.burrow.scheduledscan" MIN_MACOS_MAJOR=14 say() { printf '%s\n' "$*"; } fail() { printf '\n오류: %s\n' "$1" >&2 [[ -n "${2:-}" ]] && printf '해결: %s\n' "$2" >&2 exit 1 } WORK_DIR="" cleanup() { if [[ -n "$WORK_DIR" && -d "$WORK_DIR" ]]; then rm -rf "$WORK_DIR" # SAFE: private mktemp folder created by this script fi } trap cleanup EXIT check_system() { [[ "$(uname -s)" == "Darwin" ]] || fail "Burrow는 macOS 전용입니다." [[ "$(id -u)" -ne 0 ]] || fail "sudo 없이 실행해 주세요." "curl -fsSL $BASE_URL/install.sh | bash" # Rosetta reports x86_64 from uname, so ask the kernel whether the Mac is Apple Silicon. [[ "$(/usr/sbin/sysctl -n hw.optional.arm64 2> /dev/null || echo 0)" == "1" ]] || fail "지금 버전은 Apple Silicon(M1 이상) Mac에서만 실행됩니다." "Intel Mac 지원은 준비 중입니다." local major major="$(/usr/bin/sw_vers -productVersion | cut -d. -f1)" [[ "$major" -ge "$MIN_MACOS_MAJOR" ]] || fail "macOS $MIN_MACOS_MAJOR 이상이 필요합니다 (현재 $(/usr/bin/sw_vers -productVersion))." "시스템 설정 → 일반 → 소프트웨어 업데이트" } json_value() { /usr/bin/plutil -extract "$2" raw -o - "$1" 2> /dev/null } default_install_dir() { if [[ -n "${BURROW_INSTALL_DIR:-}" ]]; then printf '%s\n' "$BURROW_INSTALL_DIR" elif [[ -w /Applications ]]; then printf '%s\n' /Applications else printf '%s\n' "$HOME/Applications" fi } burrow_pids() { /usr/bin/pgrep -x -U "$(id -u)" Burrow 2> /dev/null || true } wait_for_exit() { local pid="$1" seconds="$2" i for ((i = 0; i < seconds * 4; i++)); do kill -0 "$pid" 2> /dev/null || return 0 sleep 0.25 done return 1 } # Asks a running Burrow to quit (SIGTERM is the normal Quit for an AppKit app). stop_running_app() { local pids pid pids="$(burrow_pids)" [[ -n "$pids" ]] || return 1 say "실행 중인 Burrow를 종료합니다..." for pid in $pids; do kill -TERM "$pid" 2> /dev/null || true done for pid in $pids; do wait_for_exit "$pid" 15 || fail "Burrow가 종료되지 않았습니다." "Burrow를 직접 종료한 뒤 다시 실행하세요." done return 0 } fetch_release_info() { local feed="$WORK_DIR/latest.json" say "최신 버전 정보를 확인합니다..." /usr/bin/curl -fsSL --retry 2 --connect-timeout 10 "$BASE_URL/latest.json" -o "$feed" || fail "버전 정보를 받지 못했습니다 ($BASE_URL/latest.json)." "인터넷 연결을 확인하고 다시 실행하세요." RELEASE_VERSION="$(json_value "$feed" version)" || fail "버전 정보 형식이 올바르지 않습니다." RELEASE_URL="$(json_value "$feed" url)" || fail "버전 정보에 다운로드 주소가 없습니다." EXPECTED_SHA="$(json_value "$feed" sha256)" || fail "버전 정보에 SHA-256 값이 없습니다." local min_macos min_macos="$(json_value "$feed" minimumMacOS || echo "$MIN_MACOS_MAJOR")" if [[ "$(/usr/bin/sw_vers -productVersion | cut -d. -f1)" -lt "${min_macos%%.*}" ]]; then fail "Burrow $RELEASE_VERSION은 macOS $min_macos 이상이 필요합니다." fi case "$RELEASE_URL" in https://*) ;; http://127.0.0.1:* | http://localhost:*) ;; # local test server only *) fail "안전하지 않은 다운로드 주소입니다: $RELEASE_URL" ;; esac } download_release() { say "Burrow $RELEASE_VERSION 내려받는 중..." ZIP_PATH="$WORK_DIR/Burrow.zip" /usr/bin/curl -fL --retry 2 --connect-timeout 10 --progress-bar "$RELEASE_URL" -o "$ZIP_PATH" || fail "앱 파일을 받지 못했습니다." "잠시 뒤 다시 실행하세요." } verify_and_extract() { local actual actual="$(/usr/bin/shasum -a 256 "$ZIP_PATH" | awk '{print $1}')" [[ "$actual" == "$EXPECTED_SHA" ]] || fail "내려받은 파일이 손상되었거나 바뀌었습니다 (SHA-256 불일치)." "다시 실행하세요. 계속되면 설치하지 마세요." /usr/bin/ditto -x -k "$ZIP_PATH" "$WORK_DIR/unpacked" || fail "압축을 풀지 못했습니다." NEW_APP="$WORK_DIR/unpacked/$APP_NAME" [[ -d "$NEW_APP" && ! -L "$NEW_APP" ]] || fail "압축 파일 안에 $APP_NAME 이 없습니다." local bundle_id bundle_id="$(/usr/bin/plutil -extract CFBundleIdentifier raw -o - "$NEW_APP/Contents/Info.plist" 2> /dev/null || true)" [[ "$bundle_id" == "$BUNDLE_ID" ]] || fail "Burrow 앱이 아닙니다 (번들 ID: ${bundle_id:-없음})." /usr/bin/codesign --verify --deep --strict "$NEW_APP" 2> /dev/null || fail "앱 서명 검사에 실패했습니다." "다시 내려받아 주세요." NEW_VERSION="$(/usr/bin/plutil -extract CFBundleShortVersionString raw -o - "$NEW_APP/Contents/Info.plist" 2> /dev/null || echo "?")" } # Swaps NEW_APP into TARGET. The old copy is renamed first and put back if the move fails. replace_app() { local target="$1" parent backup="" [[ "$(basename "$target")" == "$APP_NAME" ]] || fail "설치 위치가 올바르지 않습니다: $target" [[ ! -L "$target" ]] || fail "설치 위치가 심볼릭 링크입니다: $target" parent="$(dirname "$target")" mkdir -p "$parent" [[ -w "$parent" ]] || fail "$parent 폴더에 쓸 수 없습니다." "BURROW_INSTALL_DIR=\"\$HOME/Applications\" 를 앞에 붙여 다시 실행하세요." if [[ -e "$target" ]]; then local old_id old_id="$(/usr/bin/plutil -extract CFBundleIdentifier raw -o - "$target/Contents/Info.plist" 2> /dev/null || true)" [[ "$old_id" == "$BUNDLE_ID" ]] || fail "$target 은 Burrow가 아닌 다른 앱입니다. 덮어쓰지 않았습니다." backup="$parent/.Burrow.app.previous-$$" mv "$target" "$backup" || fail "기존 앱을 옮기지 못했습니다." fi if ! /usr/bin/ditto "$NEW_APP" "$target"; then rm -rf "$target" # SAFE: partial copy of the new Burrow.app made by the ditto above [[ -n "$backup" ]] && mv "$backup" "$target" fail "새 버전을 설치하지 못했습니다. 기존 앱은 그대로 두었습니다." fi if [[ -n "$backup" ]]; then rm -rf "$backup" # SAFE: the previous Burrow.app this script moved aside above fi /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$target" > /dev/null 2>&1 || true } uninstall() { local target target="$(default_install_dir)/$APP_NAME" stop_running_app || true /bin/launchctl bootout "gui/$(id -u)/$LAUNCH_AGENT_LABEL" > /dev/null 2>&1 || true local agent="$HOME/Library/LaunchAgents/$LAUNCH_AGENT_LABEL.plist" if [[ -f "$agent" ]]; then rm -f "$agent" # SAFE: Burrow's own scheduled-scan LaunchAgent say "예약 점검을 해제했습니다." fi if [[ -d "$target" ]]; then local trash="$HOME/.Trash/Burrow $(date +%Y%m%d-%H%M%S).app" mv "$target" "$trash" && say "Burrow를 휴지통으로 옮겼습니다: $trash" else say "$target 에 Burrow가 없습니다." fi say "설정과 기록(~/Library/Application Support/Burrow)은 남겨 두었습니다. 필요 없으면 직접 지우세요." } main() { local mode="install" from_zip="" sha="" target="" wait_pid="" relaunch=0 force=0 while [[ $# -gt 0 ]]; do case "$1" in --uninstall) mode="uninstall" ;; --from-zip) from_zip="${2:-}"; shift ;; --sha256) sha="${2:-}"; shift ;; --target) target="${2:-}"; shift ;; --wait-pid) wait_pid="${2:-}"; shift ;; --relaunch) relaunch=1 ;; --force) force=1 ;; -h | --help) sed -n '2,16p' "${BASH_SOURCE[0]:-/dev/null}" 2> /dev/null || true return 0 ;; *) fail "알 수 없는 옵션: $1" ;; esac shift done check_system if [[ "$mode" == "uninstall" ]]; then uninstall return 0 fi WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/burrow-install.XXXXXX")" [[ -n "$target" ]] || target="$(default_install_dir)/$APP_NAME" if [[ -n "$from_zip" ]]; then [[ -f "$from_zip" && -n "$sha" ]] || fail "--from-zip 에는 파일과 --sha256 값이 필요합니다." ZIP_PATH="$from_zip" EXPECTED_SHA="$sha" else fetch_release_info local installed installed="$(/usr/bin/plutil -extract CFBundleShortVersionString raw -o - "$target/Contents/Info.plist" 2> /dev/null || true)" if [[ "$installed" == "$RELEASE_VERSION" && "$force" -eq 0 ]]; then say "이미 최신 버전(Burrow $installed)이 설치되어 있습니다: $target" say "다시 설치하려면 끝에 --force 를 붙이세요." return 0 fi download_release fi verify_and_extract local was_running=0 first_install=0 [[ -e "$target" ]] || first_install=1 if [[ -n "$wait_pid" ]]; then wait_for_exit "$wait_pid" 60 || fail "Burrow가 종료되지 않아 업데이트를 멈췄습니다." was_running=1 elif stop_running_app; then was_running=1 fi replace_app "$target" say "" say "✅ Burrow $NEW_VERSION 설치 완료: $target" if [[ $first_install -eq 1 ]]; then say "처음 열면 안내에 따라 '전체 디스크 접근 권한'을 허용해 주세요." else say "업데이트 뒤에는 macOS가 '전체 디스크 접근 권한'을 다시 물을 수 있습니다." say "시스템 설정 → 개인정보 보호 및 보안 → 전체 디스크 접근 권한에서 Burrow를 껐다 켜 주세요." fi if [[ $first_install -eq 1 || $was_running -eq 1 || $relaunch -eq 1 ]]; then /usr/bin/open "$target" || true fi } main "$@"