-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-zsh-profile.sh
More file actions
157 lines (129 loc) · 4.59 KB
/
Copy pathreset-zsh-profile.sh
File metadata and controls
157 lines (129 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# reset-zsh-profile.sh - Script to reset zsh profile to system defaults
# Usage: sudo ./reset-zsh-profile.sh [username]
# Author: Luis Miguel P. Freitas
# Contact me at: luis@digitalxs.ca or https://digitalxs.ca
# YOU ARE ABSOLUTELY FREE TO USE THIS SCRIPT AS YOU WANT. IT'S YOUR RESPONSABILITY ALONE!
# Function to check if script is run as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Error: Please run as root (use sudo)"
exit 1
fi
}
# Function to validate username
validate_user() {
local username=$1
if ! id "$username" >/dev/null 2>&1; then
echo "Error: User '$username' does not exist"
exit 1
fi
}
# Function to get user home directory
get_home_dir() {
local username=$1
echo "$(getent passwd "$username" | cut -d: -f6)"
}
# Main script
main() {
# Check if username is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 [username]"
echo "Example: $0 johndoe"
exit 1
fi
# Store username
local username=$1
# Check root privileges
check_root
# Validate username
validate_user "$username"
# Get user's home directory
local home_dir
home_dir=$(get_home_dir "$username")
echo "Resetting zsh profile for user: $username"
echo "Home directory: $home_dir"
# Create backup directory
local backup_dir
backup_dir="$home_dir/zsh_backup_$(date +%Y%m%d_%H%M%S)"
echo "Creating backup directory: $backup_dir"
mkdir -p "$backup_dir"
# Note: Zsh typically doesn't ship defaults in /etc/skel — this
# script creates a minimal ~/.zshrc below instead of copying one.
# Backup existing files
echo "Backing up existing files..."
for file in .zshrc .zshenv .zprofile .zlogin .zlogout .zsh_history .zsh_plugins .zsh_aliases .oh-my-zsh; do
if [ -e "$home_dir/$file" ]; then
# If it's a directory, use -r flag for cp
if [ -d "$home_dir/$file" ]; then
cp -r "$home_dir/$file" "$backup_dir/" 2>/dev/null || echo "Warning: Could not backup $file"
else
cp "$home_dir/$file" "$backup_dir/" 2>/dev/null || echo "Warning: Could not backup $file"
fi
fi
done
# Also backup any Zsh configuration in .config/zsh if it exists
if [ -d "$home_dir/.config/zsh" ]; then
mkdir -p "$backup_dir/.config"
cp -r "$home_dir/.config/zsh" "$backup_dir/.config/" 2>/dev/null || echo "Warning: Could not backup .config/zsh"
fi
# Check for and remove symlinks and existing files
for file in .zshrc .zshenv .zprofile .zlogin .zlogout .zsh_plugins .zsh_aliases; do
if [ -L "$home_dir/$file" ] || [ -f "$home_dir/$file" ]; then
echo "Removing Zsh configuration file: $file"
rm -f "$home_dir/$file"
fi
done
# Create minimal default files
echo "Creating default Zsh configuration files..."
# Create minimal .zshrc
cat > "$home_dir/.zshrc" << EOL
# Default .zshrc created by reset-zsh-profile
# Feel free to customize this file
# Set up basic PATH
export PATH=\$PATH:\$HOME/.local/bin:/usr/local/bin
# Basic history settings
HISTFILE=\$HOME/.zsh_history
HISTSIZE=1000
SAVEHIST=1000
# Basic keybindings
bindkey -e # emacs key bindings
# Basic completion system
autoload -Uz compinit
compinit
# Add your customizations below
EOL
# Create minimal .zshenv if it doesn't exist
if [ ! -f "$home_dir/.zshenv" ]; then
cat > "$home_dir/.zshenv" << EOL
# Default .zshenv created by reset-zsh-profile
# Environment variables go here
EOL
fi
# Set permissions
echo "Setting correct permissions..."
for file in .zshrc .zshenv .zprofile .zlogin .zlogout; do
if [ -f "$home_dir/$file" ]; then
chown "$username:$(id -gn $username)" "$home_dir/$file"
chmod 644 "$home_dir/$file"
fi
done
# Remove Oh My Zsh if it exists
if [ -d "$home_dir/.oh-my-zsh" ]; then
echo "Removing Oh My Zsh installation..."
# Just move it to backup instead of deleting
mv "$home_dir/.oh-my-zsh" "$backup_dir/" 2>/dev/null || echo "Warning: Could not move Oh My Zsh to backup"
fi
echo "
Zsh profile reset completed successfully!
- Backup files are stored in: $backup_dir
- Default files have been created
- Permissions have been set
- Symlinks and custom configurations have been removed
To apply changes, the user should either:
1. Log out and log back in
2. Run: source ~/.zshrc
Note: If you had custom modifications, check the backup files to restore them manually."
}
# Run main script
main "$@"