-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_multiple_files.sh
127 lines (118 loc) · 4.16 KB
/
replace_multiple_files.sh
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
#!/bin/bash
# Variables
GITHUB_REPO="ANKUSHJAIN91/NCUpdateScripts" # GitHub repository in the format username/repository
BRANCH="main" # Branch from which to download the files
FILES=("updated/routes.php"
"updated/NavigationManager.php"
"updated/development.notice.php"
"updated/overview.php"
"updated/additional-mail.php"
) # Array of file paths in the repository
TARGET_DIRS=("/var/www/html/apps/settings/appinfo/"
"/var/www/html/lib/private/"
"/var/www/html/apps/settings/templates/settings/personal"
"/var/www/html/apps/settings/templates/settings/admin"
"/var/www/html/apps/settings/templates/settings/admin"
) # Corresponding array of target directories
# Arrays for directories and files to be removed
REMOVE_DIRS=("/var/www/html/apps/firstrunwizard" ) # Directories to remove
REMOVE_FILES=(
"/var/www/html/dist/federatedfilesharing-vue-settings-personal.js"
"/var/www/html/dist/federatedfilesharing-vue-settings-personal.js.map"
#"/var/www/html/dist/federatedfilesharing-vue-settings-admin.js"
#"/var/www/html/dist/federatedfilesharing-vue-settings-admin.js.map"
"/var/www/html/dist/updatenotification-updatenotification.js.map"
"/var/www/html/dist/updatenotification-updatenotification.js"
"/var/www/html/dist/updatenotification-updatenotification.js.LICENSE.txt"
# "/var/www/html/dist/settings-vue-settings-admin-basic-settings.js.map"
) # Files to remove
SKELETON_DIR="/var/www/html/core/skeleton" # Directory to clear
# Check if the lengths of FILES and TARGET_DIRS arrays match
if [ ${#FILES[@]} -ne ${#TARGET_DIRS[@]} ]; then
echo "Error: The number of files and target directories do not match."
exit 1
fi
# Function to download and replace a file
download_and_replace() {
local file_path=$1
local target_dir=$2
local target_file="${target_dir}/$(basename $file_path)"
# Check if target directory exists
if [ ! -d "$target_dir" ]; then
echo "Target directory does not exist: $target_dir"
exit 1
fi
echo "Downloading ${file_path} from ${GITHUB_REPO} to ${target_dir}..."
curl -o "$target_file" -L "https://raw.githubusercontent.com/${GITHUB_REPO}/${BRANCH}/${file_path}"
if [ $? -eq 0 ]; then
echo "File successfully downloaded and replaced: $target_file"
else
echo "Failed to download file: $file_path"
exit 1
fi
}
# Function to remove directories
remove_directories() {
for dir in "${REMOVE_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "Removing directory: $dir"
rm -rf "$dir"
if [ $? -eq 0 ]; then
echo "Directory successfully removed: $dir"
else
echo "Failed to remove directory: $dir"
exit 1
fi
else
echo "Directory does not exist: $dir"
fi
done
}
# Function to remove files
remove_files() {
for file in "${REMOVE_FILES[@]}"; do
if [ -f "$file" ]; then
echo "Removing file: $file"
rm -f "$file"
if [ $? -eq 0 ]; then
echo "File successfully removed: $file"
else
echo "Failed to remove file: $file"
exit 1
fi
else
echo "File does not exist: $file"
fi
done
}
# Function to clear all files and folders in the specified directory
clear_skeleton_directory() {
local dir=$1
if [ -d "$dir" ]; then
echo "Clearing directory: $dir"
rm -rf "${dir:?}"/* # The :? ensures that the directory is not empty and avoids rm -rf /
if [ $? -eq 0 ]; then
echo "Directory successfully cleared: $dir"
else
echo "Failed to clear directory: $dir"
exit 1
fi
else
echo "Directory does not exist: $dir"
fi
}
# Check for required tools
if ! command -v curl &> /dev/null; then
echo "Error: curl is not installed."
exit 1
fi
# Remove specified directories and files
remove_directories
remove_files
# Clear the skeleton directory
clear_skeleton_directory "$SKELETON_DIR"
# Iterate over the arrays and download each file to its corresponding target directory
for i in "${!FILES[@]}"; do
download_and_replace "${FILES[$i]}" "${TARGET_DIRS[$i]}"
done
exit 0