-
Notifications
You must be signed in to change notification settings - Fork 3
/
uninstall-gcc-without-xcode
executable file
·148 lines (128 loc) · 3.41 KB
/
uninstall-gcc-without-xcode
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
#!/bin/zsh
# ------------------------------------------------------------------------------
# FILE: uninstall-gcc-without-xcode
# DESCRIPTION: Uninstalls GCC Without Xcode.
# AUTHOR: Sorin Ionescu <sorin.ionescu@gmail.com>
# ------------------------------------------------------------------------------
script_name="${0:t}"
script_version='1.0.2'
cached_argv="${argv}"
# Writes to standard error.
function print-error() {
print "${script_name}: ${@}" >&2
}
# Writes to standard output.
function print-info() {
print "${@}" >&1
}
# Writes version information to standard errror.
function version() {
print "${script_name} ${script_version}
Copyright (c) 2011 Sorin Ionescu
This program is free software. You may modify or distribute it
under the terms of the MIT License." >&2
}
# Writes help to standard error.
function help() {
print "Usage: ${script_name} [‐option ...] archive [directory]
Options:
-v, --version Display version and copyright
-h, --help Display this help
Report bugs to <sorin.ionescu@gmail.com>." >&2
}
# Parse switches.
while [[ "${1}" == -* ]]; do
case "${1}" in
( -v | --version )
version
exit 0
;;
( -h | --help )
help
exit 0
;;
( -- )
shift
break
;;
( -* )
print-error "invalid option: ${1}"
help
exit 0
;;
esac
done
# Get archive.
if (( ${#} < 1 )); then
print-error "No archive given."
help
exit 1
else
if [[ ! -e "${1}" ]]; then
print-error "Archive '${1}' does not exist."
help
exit 1
else
archive="$(cd "${1:h}" && pwd)/${1:t}"
if [[ "${archive}" == *.xz ]]; then
if (( ! ${+commands[xzcat]} )); then
print-error "Cannot extract '${archive}' because XZ Utils are not installed."
exit 1
else
decompressor='xzcat'
fi
else
decompressor='gzcat'
fi
shift
fi
fi
# Get directory.
if (( ${#} < 1 )); then
print-error "No directory given, assuming /."
directory='/'
else
if [[ ! -e "${1}" ]]; then
print-error "Directory '${1}' does not exist."
help
exit 1
else
directory="${1}"
shift
fi
fi
# Root is required to uninstall.
if [[ ! -w "${directory}" ]]; then
print-info "Root privileges are required; running sudo..."
exec sudo "${0}" "${cached_argv[@]}"
exit ${?}
fi
# Directories will be deleted last.
directories="$(mktemp -t uninstall-gcc-without-xcode.directories)"
# Change CWD to installation directory.
cd "${directory}"
print-info 'Purging:'
print-info ' Files'
while read item; do
if [[ -L "${item}" ]]; then
unlink "${item}"
elif [[ -f "${item}" ]]; then
rm -f "${item}"
else
if [[ -d "${item}" ]]; then
print "${item}" >> "${directories}"
fi
fi
done < <( "${decompressor}" < "${archive}" | cpio -it 2> /dev/null | sed -e 's:/*$::g' )
print-info ' Directories'
while read item; do
ds_store="${item}/.DS_Store"
if [[ -f "${ds_store}" ]]; then
rm -f "${ds_store}"
fi
if [[ -n "$(find "${item}" -type d -maxdepth 0 -empty 2> /dev/null)" ]]; then
rmdir "${item}"
fi
done < "${directories}"
print-info ' Temporary Files'
rm "${directories}"