-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerlin-clean.sh
More file actions
executable file
·116 lines (97 loc) · 2.89 KB
/
merlin-clean.sh
File metadata and controls
executable file
·116 lines (97 loc) · 2.89 KB
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
#!/bin/bash
# This script is part of the MerlinMake repository
# Copyright (C) 2018,2019,2020,2021 CoderMerlin.com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This script cleans all .build directories from ~/Merlin
# Arguments:
# --dry-run
# Reference: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
set -eu
shopt -s nullglob
merlinDirectory=~/Merlin
# Reference: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
# Read options
options=d
longOptions=dryrun
! parsed=$(getopt --options=$options --longoptions=$longOptions --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$parsed"
# Defaults
dryrun=false
while true
do
case "$1" in
-d|--dryrun)
dryrun=true
shift 1
;;
--)
shift
break
;;
*)
echo "Unexpected argument $1"
exit 3
;;
esac
done
if $dryrun
then
echo "DRY RUN only. No files will be deleted."
fi
function cleanDirectory {
local directory=$1
if [ -d "$directory" ]
then
directorySize=$(du -sb "$directory" | cut -f1)
totalSize=$((totalSize + directorySize))
echo "Cleaning `(pwd)`/$directory to free $directorySize bytes"
if [ "$dryrun" == 'false' ]
then
rm -rf "$directory"
fi
fi
}
# Regardless from where we're executing, we always operate ONLY on the ~/Merlin directory
pushd "$merlinDirectory" > /dev/null
# Iterate through all missions (first directory level)
totalSize=0
for mission in M[0-9][0-9][0-9][0-9]-[0-9][0-9]*
do
if [ -d "$mission" ]
then
pushd "$mission" > /dev/null
# Iterate through all challenges
for challenge in C[0-9][0-9][0-9]*
do
if [ -d "$challenge" ]
then
# Determine if a .build directory exists
pushd "$challenge" > /dev/null
cleanDirectory '.build'
cleanDirectory '.merlin/build/.build'
popd > /dev/null # Pop challenge
fi
done
popd > /dev/null # Pop mission
fi
done
# Return to our original directory
popd > /dev/null # Pop Merlin mission directory
echo "Total bytes freed: $totalSize"