Última actividad 1789531939

Automatically sync .bashrc for all users for Administrator.

sync-bashrc-all.sh Sin formato
1#!/bin/bash
2#
3# sync-bashrc-all.sh (v3)
4#
5# 0. Pastikan dependency vivid + fastfetch tersedia
6# 1. Sinkronkan /etc/skel/.bashrc
7# 2. Sinkronkan /etc/shell-prompt.sh
8# 3. Sinkronkan /etc/fastfetch/config.jsonc
9# 4. Terapkan .bashrc ke semua user existing di /home
10# 5. Terapkan .bashrc yang sama ke root
11#
12# Jalankan: sudo bash sync-bashrc-all.sh
13
14set -euo pipefail
15export DEBIAN_FRONTEND=noninteractive
16
17BASHRC_URL="https://gist.darto.id/dartokloning/866220d4bda046fe93508ed50dac1965/download/HEAD/.bashrc"
18PROMPT_URL="https://gist.darto.id/dartokloning/def50cb1b3024c3b8683960c873caf34/download/HEAD/shell-prompt.sh"
19FASTFETCH_CONFIG_URL="https://gist.darto.id/dartokloning/655822e458964d0a93085c205506443f/download/HEAD/config.jsonc"
20
21SKEL_BASHRC="/etc/skel/.bashrc"
22PROMPT_FILE="/etc/shell-prompt.sh"
23FASTFETCH_CONFIG_FILE="/etc/fastfetch/config.jsonc"
24
25if [ "$(id -u)" -ne 0 ]; then
26 echo "Skrip ini harus dijalankan sebagai root (pakai sudo)." >&2
27 exit 1
28fi
29
30TMP_DIR="$(mktemp -d)"
31trap 'rm -rf "$TMP_DIR"' EXIT
32
33fetch() {
34 local url="$1" dest="$2"
35 if ! wget -q -O "$dest" "$url"; then
36 echo "Error: gagal download $url (cek koneksi internet)." >&2
37 exit 1
38 fi
39}
40
41sync_file() {
42 local url="$1" dest="$2" label="$3" mode="${4:-}"
43 local tmp="$TMP_DIR/$(basename "$dest")"
44
45 fetch "$url" "$tmp"
46 mkdir -p "$(dirname "$dest")"
47
48 if [ ! -f "$dest" ] || ! cmp -s "$tmp" "$dest"; then
49 cp "$tmp" "$dest"
50 [ -n "$mode" ] && chmod "$mode" "$dest"
51 echo " -> $label diperbarui."
52 return 0
53 else
54 echo " -> $label sudah sesuai, tidak ada perubahan."
55 return 1
56 fi
57}
58
59apply_bashrc() {
60 local user="$1" home_dir="$2"
61 local target="$home_dir/.bashrc"
62 local backup="$home_dir/.bashrc.original"
63
64 if [ -f "$target" ] && cmp -s "$SKEL_BASHRC" "$target"; then
65 echo "[SKIP] $user - .bashrc sudah sesuai template"
66 return
67 fi
68
69 if [ -f "$target" ] && [ ! -f "$backup" ]; then
70 cp -a "$target" "$backup"
71 echo "[BACKUP] $user - .bashrc lama disimpan sebagai .bashrc.original"
72 fi
73
74 cp "$SKEL_BASHRC" "$target"
75
76 if [ "$user" = "root" ]; then
77 chown root:root "$target"
78 else
79 chown "$user":"$(id -gn "$user")" "$target"
80 fi
81
82 echo "[APPLY] $user - .bashrc diperbarui dari template skel"
83}
84
85echo "== 0. Pastikan dependency (vivid, fastfetch) terinstall =="
86need_install=()
87command -v vivid >/dev/null 2>&1 || need_install+=("vivid")
88command -v fastfetch >/dev/null 2>&1 || need_install+=("fastfetch")
89
90if [ "${#need_install[@]}" -gt 0 ]; then
91 echo " -> Menginstall: ${need_install[*]}"
92 apt-get update -qq
93 apt-get install -y "${need_install[@]}"
94else
95 echo " -> vivid & fastfetch sudah terinstall, skip."
96fi
97
98echo
99echo "== 1. Cek /etc/skel/.bashrc =="
100sync_file "$BASHRC_URL" "$SKEL_BASHRC" "$SKEL_BASHRC" || true
101
102echo
103echo "== 2. Cek /etc/shell-prompt.sh =="
104sync_file "$PROMPT_URL" "$PROMPT_FILE" "$PROMPT_FILE" "644" || true
105
106echo
107echo "== 3. Cek /etc/fastfetch/config.jsonc =="
108sync_file "$FASTFETCH_CONFIG_URL" "$FASTFETCH_CONFIG_FILE" "$FASTFETCH_CONFIG_FILE" "644" || true
109
110echo
111echo "== 4. Terapkan template ke user existing di /home =="
112for home_dir in /home/*/; do
113 [ -d "$home_dir" ] || continue
114 home_dir="${home_dir%/}"
115 user="$(basename "$home_dir")"
116
117 id "$user" >/dev/null 2>&1 || continue
118 apply_bashrc "$user" "$home_dir"
119done
120
121echo
122echo "== 5. Terapkan template ke root =="
123apply_bashrc "root" "/root"
124
125echo
126echo "Selesai."
127echo "User yang sedang login perlu 'source ~/.bashrc' atau login ulang."
128echo "Untuk menguji root prompt: sudo -i"