-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-cannon-env
executable file
·160 lines (139 loc) · 5.08 KB
/
copy-cannon-env
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
#!/bin/bash
#EOC
#------------------------------------------------------------------------------
# GEOS-Chem Global Chemical Transport Model !
#------------------------------------------------------------------------------
#BOP
#
# !IROUTINE: copy-cannon-env
#
# !DESCRIPTION: Script to distribute startup files from the env repository
# to the appropriate locations in your home directory. Original files
# will be moved to timestamped backup folders.
#\\
#\\
# !CALLING SEQUENCE:
# Type at the command-line prompt:
#
# ./copy-cannon-env
#EOP
#------------------------------------------------------------------------------
#BOC
function backup_dir_name() {
#========================================================================
# Returns the name of the backup folder.
#
# 1st argument: Date & time string
# 2nd argument: Target directory
#========================================================================
if [[ $# -eq 2 ]]; then
printf "${2}/.backup.${1}" # With target dir prefaced
else
printf ".backup.${1}" # Without target dir prefaced
fi
return 0
}
function backup_and_copy() {
#========================================================================
# Copies a file from one directory to anoth
# A backup is made before the file is copied.
#
# 1st argument: Date & time string
# 2nd argument: Source directory
# 3rd argument: Target directory
#========================================================================
# Variables
srcDir="${2}" # Source dir
trgDir="${3}" # Target dir
bckDir=$(backup_dir_name "${1}" "${trgDir}") # Backup dir
# Create the target directory if it doesn't exitst
echo ""
echo "%%% In ${srcDir} %%%"
echo "Copying config files to ${trgDir}..."
if [[ ! -d "${trgDir}" ]]; then
echo "Creating ${trgDir} folder..."
mkdir -p "${trgDir}"
backupDone=0
else
# Create backup folder (only if the target dir already exists)
echo "Creating backup directory $(basename ${bckDir})..."
mkdir -p "${bckDir}"
backupDone=1
fi
# Loop over files in the source directory
for f in ${srcDir}/*; do
# Strip the source directory from the file name
file="${f/${srcDir}}"
file="${file/\/}"
# Create a backup of each file in the backup folder
if [[ -f "${trgDir}/${file}" && ! -d "${trgDir}/${file}" ]]; then
echo "Backing up ${file}..."
mv -f "${trgDir}/${file}" "${bckDir}/${file}"
fi
# Copy the files from the source dir to the target dir
echo "Copying ${file} to ${trgDir}..."
cp -fR "${srcDir}/${file}" "${trgDir}/${file}"
done
# Return whether or not a backup folder was created via $?
return ${backupDone}
}
function main() {
#========================================================================
# Main program
#========================================================================
# Variables
root="${HOME}" # Root dir (edit for debugging)
dtStr="$(date -Iseconds)" # Date & time string
dtStr="${dtStr::-6}" # w/ time zone offset removed
dtStr="${dtStr/\:/\-}" # w/ ":" replaced by ":"
dtStr="${dtStr/\:/\-}" # w/ ":" replaced by ":" again
bkDir=$(backup_dir_name "${dtStr}") # Backup folder template
folders=("root" \
"bin" \
"bin/netcdf-scripts" \
"IDL" \
"envs" \
"envs/gnu10" \
"envs/gnu12" \
"envs/intel23" ) # Folders w/ config files
# Tell bash to pick up hidden files in for loops
shopt -s dotglob
# Back up and copy files in each folder of cannon-env
# to the correspondiong folders in the user's root directory.
# Aslo keep track of the number of backups done.
let "backups=0"
for folder in ${folders[@]}; do
if [[ "x${folder}" == "xroot" ]]; then
backup_and_copy "${dtStr}" "${folder}" "${root}"
else
backup_and_copy "${dtStr}" "${folder}" "${root}/${folder}"
fi
let "backups+=$?"
done
# Echo message upon completion.
# Skip backup info unless at least one backup has been done.
echo ""
echo "#################################################################"
echo "Configuration files have been copied to the proper locations."
if [[ ${backups} -gt 0 ]]; then
echo ""
echo "Backups of original files have been created in these folders:"
for folder in ${folders[@]}; do
if [[ "x${folder}" == "xroot" ]]; then
echo "${root}/${bkDir}"
else
echo "${root}/${folder}/${bkDir}"
fi
done
echo ""
echo "You may copy over any prior settings that you wish to preserve"
echo "from the backed-up files to the new configuration files."
echo "If you wish, you may also delete the backup folders later on."
fi
echo "#################################################################"
# Restore settings and return
shopt -u dotglob
return 0
}
main $@
exit 0