-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
165 lines (138 loc) · 4.88 KB
/
functions.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
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
158
159
160
161
162
163
164
165
#!/bin/bash
copy_with_prompt() {
local src="$1"
local dest="$2"
# If destination is a directory and doesn't have a filename, add the source filename
if [ -d "$dest" ] && [ -f "$src" ]; then
# Get just the filename from src
local filename=$(basename "$src")
dest="${dest%/}/$filename" # Ensure no trailing slash before adding filename
fi
if [ -d "$src" ]; then
# If it's a directory, call copy_dir_with_prompt recursively
copy_dir_with_prompt "$src" "$dest"
elif [ -f "$src" ]; then
# If it's a file, call copy_file_with_prompt
copy_file_with_prompt "$src" "$dest"
fi
}
# Function to copy file with diff and prompt
copy_file_with_prompt() {
local src="$1"
local dest="$2"
local cwd=$(pwd)
# Check if source and destination are the same file
if cmp -s "$src" "$dest"; then
echo "Source and destination are identical, skipping: $src"
else
# If AUTO_YES is true, skip diffing and copy the file directly
if [ "$AUTO_YES" = true ]; then
cp -f "$src" "$dest"
echo "File copied: $dest"
else
# Check if destination file exists
if [ -f "$dest" ]; then
echo "File already exists: $dest"
# Show diff if files are different
# if ! diff -w -q "$src" "$dest" >/dev/null 2>&1; then
echo "Here's the diff using $(realpath "$src" | sed "s|^$cwd/||"):"
if command -v colordiff >/dev/null 2>&1; then
colordiff -u "$dest" "$src" || true
else
diff -u "$dest" "$src" | grep -vE '^(---|\+\+\+)' | sed -e '/^@/s/^/\x1b[32m/' -e '/^-/s/^/\x1b[31m/' -e '/^+/s/^/\x1b[34m/' -e 's/$/\x1b[0m/' || true
fi
# Prompt user to confirm overwriting the file
read -p "Override existing file? (y/N) " response
if [[ "$response" =~ ^[Yy]$ ]]; then
cp -f "$src" "$dest"
echo "File copied: $dest"
else
echo "Skipping: $dest"
fi
# else
# echo "Files are identical, skipping."
# fi
else
cp -f "$src" "$dest"
echo "File copied: $dest"
fi
fi
fi
}
# Function to handle directory copying with prompts
copy_dir_with_prompt() {
local src="$1"
local dest_dir="$2"
mkdir -p "$dest_dir"
shopt -s nullglob
files=("$src"/*)
if [ ${#files[@]} -eq 0 ]; then
echo "No files found in $src"
return
fi
for src_file in "$src"/*; do
base_name=$(basename "$src_file")
dest_file="$dest_dir/$base_name"
copy_with_prompt "$src_file" "$dest_file"
done
}
# Function to copy files matching a glob pattern
copy_glob_with_prompt() {
local src_dir="$1"
local glob_pattern="$2"
local dest_dir="$3"
echo "Processing $glob_pattern files"
shopt -s nullglob
files=("$src_dir"/$glob_pattern)
if [ ${#files[@]} -eq 0 ]; then
echo "No files found matching $glob_pattern"
return
fi
for src_file in "$src_dir"/$glob_pattern; do
dest_file="$dest_dir/$(basename "$src_file")"
copy_with_prompt "$src_file" "$dest_file"
done
}
# Function to run another installer script
run_installer() {
local script_path="$1"
local script_name="$2"
echo "Running $script_name installer..."
if [ -f "$script_path" ]; then
if [ "$AUTO_YES" = true ]; then
bash "$script_path" -y
else
bash "$script_path"
fi
else
echo "Error: $script_name installer not found at $script_path"
return 1
fi
}
run_installers() {
local dep_names=("$@")
for dep_name in "${dep_names[@]}"; do
# Construct the paths based on the dependency name
local ext_path="./extensions/${dep_name}/install.sh"
local deps_path="./deps/${dep_name}/install.sh"
# Check if the extension installer exists and run it
if [ -f "$ext_path" ]; then
run_installer "$ext_path" "${dep_name}" || \
exit 1
elif [ -f "$deps_path" ]; then
run_installer "$deps_path" "${dep_name}" || \
exit 1
else
test -d extensions/$dep_name || (mkdir -p extensions && git clone https://github.com/bonfire-networks/$dep_name extensions/$dep_name || echo "Could not clone the $dep_name extension")
run_installer "$ext_path" "${dep_name}" || \
(echo "No installers found for dependency: $dep_name" ; exit 1)
fi
done
}
# Function to create multiple directories
create_dirs() {
echo "Creating directories..."
for dir in "$@"; do
mkdir -p "$dir"
done
}