aboutsummaryrefslogtreecommitdiff
path: root/root.sh
blob: 7706bcec1a9f9fd5c2f953b26a83e2582ccf0a52 (plain)
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
#!/bin/bash

# Define the source base directory
BASE_DIR="$HOME/extras"

# Check if the script is running with superuser privileges
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root or with sudo"
  exit 1
fi

# Function to backup existing files
backup_existing() {
  local dest=$1
  if [ -e "$dest" ]; then
    echo "Backing up $dest"
    mv "$dest" "$dest.bak"
  fi
}

# Function to copy directories, backup, and change permissions
copy_and_set_permissions() {
  local src=$1
  local dest=$2

  if [ -d "$src" ]; then
    echo "Processing directory $src"

    for file in "$src"/*; {
      dest_file="$dest/$(basename "$file")"

      backup_existing "$dest_file"

      echo "Copying $file to $dest"
      cp -rp "$file" "$dest"

      echo "Setting permissions for $dest_file"
      chown root:root "$dest_file"
      chmod 644 "$dest_file"
    }
  else
    echo "Source directory $src does not exist."
  fi
}

# Iterate over all directories in the extras directory
for dir in "$BASE_DIR"/*; do
  if [ -d "$dir" ]; then
    dest_dir="/${dir##*/}"
    copy_and_set_permissions "$dir" "$dest_dir"
  fi
done

echo "Files copied and permissions set successfully."