blob: ba1ddb34fec186d252e7e6d91099f4bd8f46f9b7 (
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
#!/bin/bash
# Simona Pisano - 2018-11-27
# simona-scripts
# Libertamente utilizzabile sotto GPL v3
if [[ $1 == "-h" || $1 == "--help" ]] ; then
cat << EOF
macinstall command help
Use this command to change local installation.
Directory /etc/macinstall.d contains all alternative install files related.
Usage
macinstall [--help|-h]
macinstall [--version]
macinstall name-install [local-import]
Optional parameters
--help -h -> this help page
--versione -> version
-y --ask-confirm -> ask confirm before proceeed
---this-is-not-an-exercise -> force real work without this none i really done. this is a secure check.
Required
sed sudo
Cfg file
/etc/macinstall.d/macinstall.conf
Propname (wold before '=' char) can't contain spaces but file name accept '"' and delimiter and single '\' (before space)
EOF
exit 0
elseif [[ $1 == "--version" ]]
echo "macinstall Version 0.0"
exit 0
fi
#
## who am I
#
path_prefix_system_semaphore="/etc/macinstall.d/CurrentSystem-"
#path_prefix_system_semaphore="/home/simona/My Work/my-pkg simonascripts/root-test/etc/CurrentSystem-"
numSem=`ls "$path_prefix_system_semaphore"* 2> /dev/null | wc -l`
case $numSem in
1) currSystem=`ls -1 "$path_prefix_system_semaphore"* 2> /dev/null`
currSystem="${currSystem:${#path_prefix_system_semaphore}:${#currSystem}-${#path_prefix_system_semaphore}}"
echo "I am $currSystem"
;;
0) echo "Can't detect current system."
;;
*) echo "Ambiguous CurrentSystem ($numSem)!!!"
;;
esac
[[ ! -r /bin/sed ]] && { echo -e "$ERR Missing sed utility! Exit"; exit 1; }
[[ ! -r /bin/sudo ]] && { echo -e "$ERR Missing sudo utility! Exit"; exit 1; }
[[ $1 != "" ]] && install_macname=$1 || exit 0
#[[ $1 != "" ]] && install_macname=$1 || { echo "Please insert install name as first command parameter!!"; exit 0; }
runLocalImport=false
[[ $2 == "local-import" ]] && runLocalImport=true
flagForceRealWork=false
flagAskConfirm=false
while [ $# -ne 0 ] ; do
case $1 in
-y|--ask-confirm) flagAskConfirm=true ;;
---this-is-not-an-exercise) flagForceRealWork=true;;
*) ;;
esac
shift
done
#color Foreground Background
#black 30 40
#red 31 41
#green 32 42
#yellow 33 43
#blue 34 44
#magenta 35 45
#cyan 36 46
#white 37 47
EchoReset="\e[0m"
echo -e -n "$EchoReset"
OK="\E[32;40m[OK]${EchoReset}"
WARN="\E[33;40m[WARN]${EchoReset}"
ERR="\E[31;40m[ERR]${EchoReset}"
#echo -e "$OK $WARN $ERR"
#path_cfg must be '/' terminated
#path_cfg="../etc-macinstall.d/"
path_cfg="/etc/macinstall.d/"
#if last char is not '/' add it
[[ ${path_cfg:${#path_cfg}-1:1} != "/" ]] && path_cfg="$path_cfg/"
filename_cfg="macinstall.conf"
#path_dest must be '/' terminated
#path_dest="/home/simona/My Work/my-pkg simonascripts/root-test/"
path_dest="$HOME/test/"
#path_dest="/"
#if last char is not '/' add it
[[ ${path_dest:${#path_dest}-1:1} != "/" ]] && path_dest="$path_dest/"
postfix_bak_file_name=".bak"
#cfg related
prop_cfg_file_copy_num="CopyFilesNum"
prop_cfg_dir_copy_num="CopyDirectoriesNum"
prop_cfg_file_copy="CopyFile"
prop_cfg_dir_copy="CopyDirectory"
prop_cfg_write_sem="WriteSemaphore"
prop_cfg_read_sem="AllowOnlyIfPreviousMacIs"
if [[ ! -d $path_cfg ]] ; then
mkdir -p $path_cfg
[[ $? -ne 0 ]] && { echo -e "$ERR Fail '$path_cfg' mkdir cmd creation !!"; exit 1; }
fi
if [[ ! -r $path_cfg$filename_cfg ]] ; then
touch $path_cfg$filename_cfg
[[ $? -ne 0 ]] && { echo -e "$ERR Fail '$path_cfg$filename_cfg' touch cmd creation !!"; exit 1; }
fi
if [[ ! -d $path_dest ]] ; then
mkdir -p $path_dest
[[ $? -ne 0 ]] && { echo -e "$ERR Fail $path_dest mkdir cmd creation !!"; exit 1; }
fi
if [[ "$flagAskConfirm" == true ]] ; then
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user."; exit 0; }
fi
number_files=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_file_copy_num[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
number_directories=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_dir_copy_num[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
ok_write_semaphore=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_write_sem[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ $ok_write_semaphore == "" ]] && ok_write_semaphore="yes" #default
[[ $ok_write_semaphore != "yes" && $ok_write_semaphore != "no" ]] && ok_write_semaphore="yes" #default
#Check cfg file integrity
if [[ $number_files -lt 1 && $number_directories -lt 1 ]] ; then
echo -e "$ERR $filename_cfg: None to do!!! 'CopyFilesNum' and 'CopyDirectoriesNum' not correctly defined. Please modify cfg."
exit 1
fi
if [[ $number_files -gt 0 ]] ; then
for (( counter=1; counter <= $number_files; counter++ )) ; do
dupe=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_file_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ $dupe == "" ]] && { echo -e "$ERR $filename_cfg: '$prop_cfg_file_copy$counter' not found inside cfg. Fix cfg!!"; exit 1; }
[[ ! -r $path_cfg$install_macname$dupe ]] && { echo -e "$ERR File '$path_cfg$install_macname$dupe' defined in config but not found!"; exit 1; }
#remove starting '/' if exist
[[ ${dupe:0:1} == "/" ]] && dupe=${dupe:1:${#dupe}-1}
# extract path from $dupe and mkdir -p it
dupeFilename=${dupe##*/} #extract froms tring all is over last '/' (filename)
dupePath=${dupe:0:${#dupe}-${#dupeFilename}} #extract path (remove filename cutting string)
#echo "filename=$dupeFilename path=$path_dest$dupePath" #debug
if [[ ! -d "$path_dest$dupePath" ]] ; then
mkdir -p "$path_dest$dupePath"
if [[ $? -ne 0 ]] ; then
sudo mkdir -p "$path_dest$dupePath"
[[ $? -ne 0 ]] && { echo -e "$WARN Fail generating directory '$path_dest$dupePath'. Exit."; exit 1; }
fi
fi
done
fi
if [[ $number_directories -gt 0 ]] ; then
for (( counter=1; counter <= $number_directories; counter++ )) ; do
dupe=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_dir_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ $dupe == "" ]] && { echo -e "$ERR $filename_cfg: '$prop_cfg_dir_copy$counter' not found inside cfg. Fix cfg!!"; exit 1; }
[[ ! -d $path_cfg$install_macname$dupe ]] && { echo -e "$ERR Directory '$path_cfg$install_macname$dupe' defined in config but not found!"; exit 1; }
#remove starting '/' if exist
[[ ${dupe:0:1} == "/" ]] && dupe=${dupe:1:${#dupe}-1}
#remove ending '/' if exist
[[ ${dupe:${#dupe}-1:1} == "/" ]] && dupe=${dupe:0:${#dupe}-1}
# extract path from $dupe and mkdir -p it
dupeFoldername=${dupe##*/} #extract froms tring all is over last '/' (foldername)
dupePath=${dupe:0:${#dupe}-${#dupeFoldername}} #extract path (remove filename cutting string)
if [[ ! -d "$path_dest$dupePath" ]] ; then
mkdir -p "$path_dest$dupePath"
if [[ $? -ne 0 ]] ; then
sudo mkdir -p "$path_dest$dupePath"
[[ $? -ne 0 ]] && { echo -e "$WARN Fail generating directory '$path_dest$dupePath'. Exit."; exit 1; }
fi
fi
done
fi
# ------------------------------------------------------------------------------------------------------------------------
# WORK -------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------
if [[ "$flagForceRealWork" == true ]] ; then
echo -e "$WARN Warning: using REAL path. This is not a simulation. I can damage system."
echo "Dest dir used is '$path_dest'. (absolute path starting with '/' are fixed removing it)."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user."; exit 0; }
fi
#TO-DO local-import come secondo parametro
#legge macchina corrente su file semaforo, legge macchina corrente da parametro (devono corrispondere)
# e va ricopiarsi tutti i files elencati sulla macchina (se presente) nella cartella giusta (è un fresh dei files di install)
if [[ $runLocalImport == true ]] ; then
#if current install exist but it is not the one current i can't proceede
numSem=$(ls "$path_prefix_system_semaphore"* 2> /dev/null | wc -l)
if [[ numSem -gt 0 ]] ; then
if [[ ! -r "$path_prefix_system_semaphore$install_macname" ]] ; then
echo -e "$ERR Another install. Can't work"
exit 1
fi
fi
#
exit 0
fi
#?????
#echo "1"
#sed -nr "/^\[$install_macname\]/ { :l /^ciccia[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg
#sed -nr "/^\[$install_macname\]/ { :l /^AllowOnlyIfPreviousMacIs[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg
#
## Verify semaphore file (opzional)
#
AllowOnlyIfPreviousMacIs=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_read_sem[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
if [[ $AllowOnlyIfPreviousMacIs != "" ]] ; then
echo " Found in cfg: AllowOnlyIfPreviousMacIs=$AllowOnlyIfPreviousMacIs"
if [[ ! -r "$path_prefix_system_semaphore$AllowOnlyIfPreviousMacIs" ]] ; then
echo -e "$ERR Missing semaphore '$path_prefix_system_semaphore$AllowOnlyIfPreviousMacIs'. Previous install required but not verified. Stop."
exit 1
else
flag_old_mac_to_erase="$AllowOnlyIfPreviousMacIs"
fi
else
echo -e "$WARN Warning: control of previous installed machine disabled."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user."; exit 0; }
fi
#
## Backup old files
#
if [[ $number_files -gt 0 ]] ; then
for (( counter=1; counter <= $number_files; counter++ )) ; do
backup=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_file_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ ${backup:0:1} == "/" ]] && backup=${backup:1:${#backup}-1} #remove starting '/' if exist
if [[ -r $path_dest$backup ]] ; then
if [[ "$flagForceRealWork" == true ]] ; then
cp -f "$path_dest$backup" "$path_dest$backup$postfix_bak_file_name"
if [[ $? -ne 0 ]] ; then
sudo cp -f "$path_dest$backup" "$path_dest$backup$postfix_bak_file_name"
if [[ $? -ne 0 ]] ; then
echo -e "$WARN Fail generating backup file '$path_dest$backup$postfix_bak_file_name'. Ask User."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user with error."; exit 1; }
else
echo -e "$OK Generation of backup file [1] '$path_dest$backup' as '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$OK Generation of backup file [2] '$path_dest$backup' as '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$OK [Simulated] Generation of backup file '$path_dest$backup' as '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$WARN File '$path_dest$backup' to backup not present. Ignoring backup."
fi
done
fi
if [[ $number_directories -gt 0 ]] ; then
for (( counter=1; counter <= $number_directories; counter++ )) ; do
backup=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_dir_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ ${backup:0:1} == "/" ]] && backup=${backup:1:${#backup}-1} #remove starting '/' if exist
if [[ -d "$path_dest$backup" ]] ; then
if [[ "$flagForceRealWork" == true ]] ; then
#if bak directory alrrady exist to not create a new ssh dir inside it....
if [[ -d "$path_dest$backup$postfix_bak_file_name" ]] ; then
rm -r -f "$path_dest$backup$postfix_bak_file_name"
if [[ $? -ne 0 ]] ; then
sudo rm -r -f "$path_dest$backup$postfix_bak_file_name"
[[ $? -ne 0 ]] && echo -e "$WARN Fail removing old backup directory.'$path_dest$backup$postfix_bak_file_name'."
fi
fi
#do work
mv -f "$path_dest$backup" "$path_dest$backup$postfix_bak_file_name"
if [[ $? -ne 0 ]] ; then
sudo mv -f "$path_dest$backup" "$path_dest$backup$postfix_bak_file_name"
if [[ $? -ne 0 ]] ; then
echo -e "$WARN Fail generating backup directory '$path_dest$backup$postfix_bak_file_name'. Ask User."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user with error."; exit 1; }
else
echo -e "$OK Generation [1] of backup directory '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$OK Generation [2] of backup directory '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$OK [Simulated] Generation of backup directory '$path_dest$backup$postfix_bak_file_name'"
fi
else
echo -e "$WARN Directory '$path_dest$backup' to backup not present. Ignoring backup."
fi
done
fi
#
## Copia nuovi files
#
if [[ $number_files -gt 0 ]] ; then
for (( counter=1; counter <= $number_files; counter++ )) ; do
dupe=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_file_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ ${dupe:0:1} == "/" ]] && dupe=${dupe:1:${#dupe}-1} #remove starting '/' if exist
if [[ "$flagForceRealWork" == true ]] ; then
cp -f "$path_cfg$install_macname/$dupe" "$path_dest$dupe"
if [[ $? -ne 0 ]] ; then
sudo cp -f "$path_cfg$install_macname/$dupe" "$path_dest$dupe"
if [[ $? -ne 0 ]] ; then
echo -e "$ERR Fail copyng file over destination from <$install_macname> to '$path_dest$dupe'. Ask User."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user with error."; exit 1; }
fi
fi
echo -e "$OK Copied file from <$install_macname> '$path_dest$dupe'."
else
echo -e "$OK [Simulated] Copied file from '$path_cfg$install_macname/$dupe' to '$path_dest$dupe'."
fi
done
fi
if [[ $number_directories -gt 0 ]] ; then
for (( counter=1; counter <= $number_directories; counter++ )) ; do
dupe=`sed -nr "/^\[$install_macname\]/ { :l /^$prop_cfg_dir_copy$counter[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $path_cfg$filename_cfg`
[[ ${dupe:0:1} == "/" ]] && dupe=${dupe:1:${#dupe}-1} #remove starting '/' if exist
[[ ${dupe:${#dupe}-1:1} == "/" ]] && dupe=${dupe:0:${#dupe}-1} #remove ending '/' if exist
if [[ "$flagForceRealWork" == true ]] ; then
cp -f -r "$path_cfg$install_macname/$dupe" "$path_dest$dupe"
if [[ $? -ne 0 ]] ; then
sudo cp -f -r "$path_cfg$install_macname/$dupe" "$path_dest$dupe"
if [[ $? -ne 0 ]] ; then
echo -e "$ERR Fail copyng directory over destination from <$install_macname> to '$path_dest$dupe'. Ask User."
echo -n "Continue? (Y/N): "; read ask
[[ ! $ask =~ ^(yes|y|YES|y|s|S|si|SI|sì)$ ]] && { echo "Exit forced by user with error."; exit 1; }
fi
fi
echo -e "$OK Copied directory <$install_macname> '$path_dest$dupe'."
else
echo -e "$OK [Simulated] Copied directory from '$path_cfg$install_macname/$dupe' to '$path_dest$dupe'."
fi
done
fi
#
## Write new semaphore (can be disabled)
#
if [[ $ok_write_semaphore == "yes" ]] ; then
if [[ -r $path_prefix_system_semaphore$flag_old_mac_to_erase ]] ; then
#if [[ "$flagForceRealWork" == true ]] ; then
rm "$path_prefix_system_semaphore$flag_old_mac_to_erase"
if [[ $? -ne 0 ]] ; then
sudo rm "$path_prefix_system_semaphore$flag_old_mac_to_erase"
[[ $? -ne 0 ]] && echo -e "$WARN All was OK but I can't remove '$path_prefix_system_semaphore$flag_old_mac_to_erase' !!"
fi
touch "$path_prefix_system_semaphore$install_macname"
if [[ $? -ne 0 ]] ; then
sudo touch "$path_prefix_system_semaphore$install_macname"
[[ $? -ne 0 ]] && echo -e "$WARN All OK EXCEPT creation of blank file '$path_prefix_system_semaphore$install_macname' !!"
fi
fi
fi
exit 0;
|