#!/bin/bash # # sync-bashrc-all.sh (v2) # # Skrip all-in-one: # 0. Pastikan dependency (vivid, fastfetch) terinstall via apt # 1. Cek /etc/skel/.bashrc vs versi remote (gist) -> update kalau beda # 2. Cek /etc/shell-prompt.sh vs versi remote (gist) -> update kalau beda # 3. Cek /etc/fastfetch/config.jsonc vs versi remote (gist) -> update kalau beda # 4. Terapkan /etc/skel/.bashrc ke semua user existing di /home yang # .bashrc-nya belum sesuai # # Jalankan sebagai root: sudo bash sync-bashrc-all.sh set -euo pipefail export DEBIAN_FRONTEND=noninteractive BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc" PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh" FASTFETCH_CONFIG_URL="https://gist.darto.id/dartokloning/655822e458964d0a93085c205506443f/download/HEAD/config.jsonc" SKEL_BASHRC="/etc/skel/.bashrc" PROMPT_FILE="/etc/shell-prompt.sh" FASTFETCH_CONFIG_FILE="/etc/fastfetch/config.jsonc" if [ "$(id -u)" -ne 0 ]; then echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2 exit 1 fi TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT fetch() { # fetch URL DEST_TMPFILE -> berhenti dengan pesan jelas kalau gagal local url="$1" dest="$2" if ! wget -q -O "$dest" "$url"; then echo "Error: gagal download $url (cek koneksi internet)." >&2 exit 1 fi } sync_file() { # sync_file URL DEST_PATH LABEL [CHMOD_MODE] # return 0 kalau file diperbarui, 1 kalau sudah sesuai (tidak ada perubahan) local url="$1" dest="$2" label="$3" mode="${4:-}" local tmp="$TMP_DIR/$(basename "$dest")" fetch "$url" "$tmp" mkdir -p "$(dirname "$dest")" if [ ! -f "$dest" ] || ! cmp -s "$tmp" "$dest"; then cp "$tmp" "$dest" [ -n "$mode" ] && chmod "$mode" "$dest" echo " -> $label diperbarui." return 0 else echo " -> $label sudah sesuai, tidak ada perubahan." return 1 fi } echo "== 0. Pastikan dependency (vivid, fastfetch) terinstall ==" need_install=() command -v vivid >/dev/null 2>&1 || need_install+=("vivid") command -v fastfetch >/dev/null 2>&1 || need_install+=("fastfetch") if [ "${#need_install[@]}" -gt 0 ]; then echo " -> Menginstall: ${need_install[*]}" apt-get update -qq apt-get install -y "${need_install[@]}" else echo " -> vivid & fastfetch sudah terinstall, skip." fi echo echo "== 1. Cek /etc/skel/.bashrc ==" sync_file "$BASHRC_URL" "$SKEL_BASHRC" "$SKEL_BASHRC" || true echo echo "== 2. Cek /etc/shell-prompt.sh ==" sync_file "$PROMPT_URL" "$PROMPT_FILE" "$PROMPT_FILE" "644" || true echo echo "== 3. Cek /etc/fastfetch/config.jsonc ==" sync_file "$FASTFETCH_CONFIG_URL" "$FASTFETCH_CONFIG_FILE" "$FASTFETCH_CONFIG_FILE" "644" || true echo echo "== 4. Terapkan template ke user existing di /home ==" for home_dir in /home/*/; do home_dir="${home_dir%/}" user="$(basename "$home_dir")" # skip kalau bukan user valid (mis. lost+found, direktori nyasar) if ! id "$user" >/dev/null 2>&1; then continue fi target="$home_dir/.bashrc" # kalau .bashrc user sudah identik dengan template skel, skip if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then echo "[SKIP] $user - .bashrc sudah sesuai template" continue fi # backup .bashrc lama, hanya sekali (tidak menimpa backup yang sudah ada) if [ -f "$target" ] && [ ! -f "$home_dir/.bashrc.original" ]; then cp -a "$target" "$home_dir/.bashrc.original" echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original" fi cp "$SKEL_BASHRC" "$target" chown "$user":"$user" "$target" echo "[APPLY] $user - .bashrc diperbarui dari template skel" done echo echo "Selesai." echo "Catatan: user yang sedang login perlu 'source ~/.bashrc' atau login ulang" echo "agar shell aktifnya ikut memakai .bashrc yang baru."} echo "== 1. Cek /etc/skel/.bashrc ==" tmp_bashrc="$TMP_DIR/.bashrc" fetch "$BASHRC_URL" "$tmp_bashrc" if [ ! -f "$SKEL_BASHRC" ] || ! cmp -s "$tmp_bashrc" "$SKEL_BASHRC"; then cp "$tmp_bashrc" "$SKEL_BASHRC" echo " -> $SKEL_BASHRC diperbarui dari gist." else echo " -> $SKEL_BASHRC sudah sesuai dengan gist, tidak ada perubahan." fi echo echo "== 2. Cek /etc/shell-prompt.sh ==" tmp_prompt="$TMP_DIR/shell-prompt.sh" fetch "$PROMPT_URL" "$tmp_prompt" if [ ! -f "$PROMPT_FILE" ] || ! cmp -s "$tmp_prompt" "$PROMPT_FILE"; then cp "$tmp_prompt" "$PROMPT_FILE" chmod 644 "$PROMPT_FILE" echo " -> $PROMPT_FILE diperbarui dari gist." else echo " -> $PROMPT_FILE sudah sesuai dengan gist, tidak ada perubahan." fi echo echo "== 3. Terapkan template ke user existing di /home ==" for home_dir in /home/*/; do home_dir="${home_dir%/}" user="$(basename "$home_dir")" # skip kalau bukan user valid (mis. lost+found, direktori nyasar) if ! id "$user" >/dev/null 2>&1; then continue fi target="$home_dir/.bashrc" # kalau .bashrc user sudah identik dengan template skel, skip if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then echo "[SKIP] $user - .bashrc sudah sesuai template" continue fi # backup .bashrc lama, hanya sekali (tidak menimpa backup yang sudah ada) if [ -f "$target" ] && [ ! -f "$home_dir/.bashrc.original" ]; then cp -a "$target" "$home_dir/.bashrc.original" echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original" fi cp "$SKEL_BASHRC" "$target" chown "$user":"$user" "$target" echo "[APPLY] $user - .bashrc diperbarui dari template skel" done echo echo "Selesai." echo "Catatan: user yang sedang login perlu 'source ~/.bashrc' atau login ulang" echo "agar shell aktifnya ikut memakai .bashrc yang baru."