forked from szepeviktor/debian-server-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.sh
executable file
·86 lines (70 loc) · 1.9 KB
/
uninstall.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
#!/bin/bash
#
# Uninstall a tool from debian-server-tools.
#
# VERSION :0.1.0
# DATE :2018-01-01
# AUTHOR :Viktor Szépe <[email protected]>
# LICENSE :The MIT License (MIT)
# URL :https://github.com/szepeviktor/debian-server-tools
# BASH-VERSION :4.2+
Die() {
local RET="$1"
shift
echo -e "$*" 1>&2
exit "$RET"
}
Get_meta() {
# defaults to self
local FILE="${1:-$0}"
# defaults to "VERSION"
local META="${2:-VERSION}"
local VALUE
VALUE="$(head -n 30 "$FILE" | grep -m 1 "^# ${META}\s*:" | cut -d ':' -f 2-)"
if [ -z "$VALUE" ]; then
VALUE="(unknown)"
fi
echo "$VALUE"
}
Do_uninstall() {
local FILE="$1"
local SYMLINK
local SCRIPT
SCRIPT="$(Get_meta "$FILE" LOCATION)"
if [ -z "$SCRIPT" ] || [ "$SCRIPT" == "(unknown)" ]; then
Die 2 "Invalid location (${SCRIPT})"
fi
# Check for existence
if [ -f "$SCRIPT" ]; then
echo "Uninstalling ${SCRIPT}"
else
Die 3 "Script does not exist (${SCRIPT})"
fi
# Uninstall
rm -v "$SCRIPT" || Die 11 "Uninstallation failure (${SCRIPT})"
# Create symlink
head -n 30 "$FILE" | grep "^# SYMLINK\s*:" | cut -d':' -f 2- \
| while read -r SYMLINK; do
echo -n "Removing symlink "
rm -v "$SYMLINK" || Die 12 "Symbolic link creation failure (${SYMLINK})"
done
# Cron jobs
if head -n 30 "$FILE" | grep -qi "^# CRON"; then
"$(dirname "$0")/uninstall-cron.sh" "$FILE" || Die 13 "Cron uninstallation failure (${SCRIPT})"
fi
}
UNINSTALL_PATH="$1"
# Check user
if [ "$(id --user)" -ne 0 ]; then
Die 1 "Only root is allowed to uninstall"
fi
# Display version
if [ "$UNINSTALL_PATH" == "--version" ]; then
Get_meta
exit 0
fi
if [ -f "$UNINSTALL_PATH" ]; then
Do_uninstall "$UNINSTALL_PATH"
else
Die 2 "Specified file does not exist (${UNINSTALL_PATH})"
fi