#!/usr/bin/env bash # # sync-zshrc-all.sh - macOS Tahoe / Apple Silicon # # Menyinkronkan: # 1. template .zshrc # 2. /usr/local/etc/shell-prompt.zsh # 3. config Fastfetch per-user # 4. dependency Homebrew: vivid, fastfetch, coreutils, tree # # Jalankan sebagai USER ADMIN biasa, BUKAN dengan "sudo bash ...". # Script akan meminta sudo hanya ketika diperlukan. # # Default: mengambil tiga file dari direktori yang sama dengan script ini. # Untuk mode remote/Gist, isi URL melalui environment variable: # ZSHRC_URL=https://... PROMPT_URL=https://... FASTFETCH_CONFIG_URL=https://... ./sync-zshrc-all.sh set -euo pipefail if [[ "$(uname -s)" != "Darwin" ]]; then echo "Error: script ini hanya untuk macOS." >&2 exit 1 fi if [[ "$(id -u)" -eq 0 ]]; then echo "Jangan jalankan script ini sebagai root." >&2 echo "Jalankan sebagai user admin biasa: ./sync-zshrc-all.sh" >&2 exit 1 fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LOCAL_ZSHRC="$SCRIPT_DIR/zshrc" LOCAL_PROMPT="$SCRIPT_DIR/shell-prompt.zsh" LOCAL_FASTFETCH="$SCRIPT_DIR/config.jsonc" ZSHRC_URL="${ZSHRC_URL:-}" PROMPT_URL="${PROMPT_URL:-}" FASTFETCH_CONFIG_URL="${FASTFETCH_CONFIG_URL:-}" GLOBAL_ZSHRC_TEMPLATE="/usr/local/etc/zshrc-template" PROMPT_FILE="/usr/local/etc/shell-prompt.zsh" TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT need_cmd() { command -v "$1" >/dev/null 2>&1 } fetch_or_local() { # fetch_or_local URL LOCAL_FILE DEST_TMP local url="$1" local_file="$2" dest="$3" if [[ -n "$url" ]]; then if ! curl -fsSL "$url" -o "$dest"; then echo "Error: gagal download $url" >&2 exit 1 fi else if [[ ! -f "$local_file" ]]; then echo "Error: file lokal tidak ditemukan: $local_file" >&2 exit 1 fi cp "$local_file" "$dest" fi } sync_root_file() { # sync_root_file SOURCE DEST LABEL MODE local src="$1" dest="$2" label="$3" mode="$4" if [[ ! -f "$dest" ]] || ! cmp -s "$src" "$dest"; then sudo mkdir -p "$(dirname "$dest")" sudo cp "$src" "$dest" sudo chown root:wheel "$dest" sudo chmod "$mode" "$dest" echo " -> $label diperbarui." else echo " -> $label sudah sesuai, tidak ada perubahan." fi } echo "== 0. Pastikan Homebrew tersedia ==" if ! need_cmd brew; then echo "Error: Homebrew belum terinstall." >&2 echo "Install Homebrew terlebih dahulu dari https://brew.sh" >&2 exit 1 fi eval "$(brew shellenv)" echo echo "== 1. Pastikan dependency terinstall ==" need_install=() need_cmd vivid || need_install+=(vivid) need_cmd fastfetch || need_install+=(fastfetch) need_cmd gls || need_install+=(coreutils) need_cmd tree || need_install+=(tree) if (( ${#need_install[@]} > 0 )); then echo " -> Menginstall: ${need_install[*]}" brew install "${need_install[@]}" else echo " -> vivid, fastfetch, coreutils, tree sudah tersedia." fi echo echo "== 2. Siapkan source file ==" tmp_zshrc="$TMP_DIR/zshrc" tmp_prompt="$TMP_DIR/shell-prompt.zsh" tmp_fastfetch="$TMP_DIR/config.jsonc" fetch_or_local "$ZSHRC_URL" "$LOCAL_ZSHRC" "$tmp_zshrc" fetch_or_local "$PROMPT_URL" "$LOCAL_PROMPT" "$tmp_prompt" fetch_or_local "$FASTFETCH_CONFIG_URL" "$LOCAL_FASTFETCH" "$tmp_fastfetch" echo " -> source file siap." echo echo "== 3. Sinkronkan template dan prompt global ==" sync_root_file "$tmp_zshrc" "$GLOBAL_ZSHRC_TEMPLATE" "$GLOBAL_ZSHRC_TEMPLATE" 644 sync_root_file "$tmp_prompt" "$PROMPT_FILE" "$PROMPT_FILE" 644 echo echo "== 4. Terapkan .zshrc dan Fastfetch ke user manusia di /Users ==" while IFS=: read -r user _ uid _ _ home shell; do # macOS human users umumnya UID >= 501. Skip system/shared/guest users. (( uid >= 501 )) || continue [[ "$user" == "Guest" ]] && continue [[ "$user" == _* ]] && continue [[ -d "$home" ]] || continue [[ "$home" == /Users/* ]] || continue [[ "$shell" == */zsh || "$shell" == */bash ]] || continue target_zshrc="$home/.zshrc" ff_dir="$home/.config/fastfetch" target_fastfetch="$ff_dir/config.jsonc" group="$(id -gn "$user" 2>/dev/null || echo staff)" if [[ -f "$target_zshrc" ]] && cmp -s "$tmp_zshrc" "$target_zshrc"; then echo "[SKIP] $user - .zshrc sudah sesuai template" else if [[ -f "$target_zshrc" && ! -f "$home/.zshrc.original" ]]; then sudo cp -p "$target_zshrc" "$home/.zshrc.original" sudo chown "$user:$group" "$home/.zshrc.original" echo "[BACKUP] $user - .zshrc lama -> .zshrc.original" fi sudo cp "$tmp_zshrc" "$target_zshrc" sudo chown "$user:$group" "$target_zshrc" sudo chmod 644 "$target_zshrc" echo "[APPLY] $user - .zshrc diperbarui" fi sudo mkdir -p "$ff_dir" if [[ ! -f "$target_fastfetch" ]] || ! cmp -s "$tmp_fastfetch" "$target_fastfetch"; then if [[ -f "$target_fastfetch" && ! -f "$target_fastfetch.original" ]]; then sudo cp -p "$target_fastfetch" "$target_fastfetch.original" sudo chown "$user:$group" "$target_fastfetch.original" echo "[BACKUP] $user - Fastfetch config lama -> config.jsonc.original" fi sudo cp "$tmp_fastfetch" "$target_fastfetch" sudo chown -R "$user:$group" "$home/.config" sudo chmod 644 "$target_fastfetch" echo "[APPLY] $user - Fastfetch config diperbarui" else echo "[SKIP] $user - Fastfetch config sudah sesuai" fi done < <(dscl . -list /Users UniqueID | awk '{print $1":"$2}' | while IFS=: read -r u uid; do home=$(dscl . -read "/Users/$u" NFSHomeDirectory 2>/dev/null | awk '{print $2}') shell=$(dscl . -read "/Users/$u" UserShell 2>/dev/null | awk '{print $2}') printf '%s:x:%s:x:x:%s:%s\n' "$u" "$uid" "$home" "$shell" done) echo echo "Selesai." echo "Buka terminal baru atau jalankan: exec zsh"