-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller
executable file
·158 lines (125 loc) · 4.06 KB
/
installer
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
#!/bin/bash
#
# blib installer
#
# Copyright (C) 2018 David Hobach LGPLv3
# 0.6
function errorOut {
>&2 echo "ERROR: $1"
>&2 echo "Aborting..."
exit 1
}
#name of the directory this script resides in (hopefully)
SCRIPT_DIR="$(readlink -f "${BASH_SOURCE[0]}")" || errorOut "Failed to execute readlink."
SCRIPT_DIR="$(dirname "$SCRIPT_DIR")" || errorOut "Failed to execute dirname."
#name of this script
SCRIPT_NAME="${BASH_SOURCE[0]##*/}"
#default installation directory
DEFAULT_INSTALL_DIR="/usr/lib"
#name of the library
LIB_NAME="blib"
#where the binary link will be installed to
BINARY_INSTALL="/usr/bin/$LIB_NAME"
#path to the file where to install the manpage
MAN_INSTALL_PATH="/usr/share/man/man3/${LIB_NAME}.3"
function usage {
echo "Usage: $SCRIPT_NAME [install|uninstall|reinstall] [install dir]"
echo ""
echo "Installer for $LIB_NAME."
echo ""
echo "[install dir]: where to install the blib library to or remove it from (a blib subdirectory is created/removed there); default: $DEFAULT_INSTALL_DIR"
exit 1
}
function isInstalled {
local targetDir="$1"
#NOTE: -e doesn't work on dead symlinks (returns false)
[ -e "$targetDir" ] || [ -e "$BINARY_INSTALL" ] || [ -h "$BINARY_INSTALL" ]
}
function updateMan {
echo "Updating the mandb... "
mandb > /dev/null || errorOut "Failed to update the mandb."
}
#installR [install dir] [run man update]
function installR {
local installDir="$1"
local runMan="${2:-0}"
local targetDir="$installDir/$LIB_NAME"
local docDir="$targetDir/doc"
local generatedMan="$docDir/${LIB_NAME}.man"
#checks
[ ${BASH_VERSINFO[0]} -lt 4 ] && errorOut "blib only works with bash versions >= 4."
isInstalled "$targetDir" && errorOut "blib already appears to be installed. Please remove older installations first."
local blib="$SCRIPT_DIR/$LIB_NAME"
[ ! -f "$blib" ] && errorOut "Could not find $blib. Thus cannot install."
echo "Installing to ${targetDir} ..."
mkdir -p "$targetDir" || errorOut "Failed to create $targetDir."
cp -rT "$SCRIPT_DIR" "$targetDir" || errorOut "Failed to copy the blib files."
echo "Fixing access rights..."
chmod -R a+r "$targetDir" || errorOut "Failed to modify the access rights of $targetDir."
#gendoc must be able to write to $docDir:
chmod -R a+rw "$docDir" || errorOut "Failed to modify the access rights of $docDir."
echo "Creating a $LIB_NAME symlink to $BINARY_INSTALL ..."
ln -s "$targetDir/$LIB_NAME" "$BINARY_INSTALL" || errorOut "Failed to create the symlink."
if command -v pandoc &> /dev/null ; then
blib gendoc html || exit $?
blib gendoc pdf || exit $?
blib gendoc man || exit $?
ln -s "$generatedMan" "$MAN_INSTALL_PATH" || errorOut "Failed to install the $LIB_NAME manpage."
[ $runMan -eq 0 ] && updateMan
else
echo "Skipping doc installation as pandoc is not installed."
fi
return 0
}
#uninstallR [install dir] [run man update]
function uninstallR {
local installDir="$1"
local runMan="${2:-0}"
local targetDir="$installDir/blib"
[ ! -d "$targetDir" ] && errorOut "blib doesn't seem to be installed in $targetDir."
echo "Removing ${targetDir} ..."
rm -rf "$targetDir"
echo "Removing $BINARY_INSTALL ..."
rm -f "$BINARY_INSTALL"
if [ -h "$MAN_INSTALL_PATH" ] || [ -e "$MAN_INSTALL_PATH" ] ; then
echo "Removing $MAN_INSTALL_PATH ..."
rm -f "$MAN_INSTALL_PATH"
[ $runMan -eq 0 ] && updateMan
fi
return 0
}
function reinstallR {
local installDir="$1"
uninstallR "$installDir" 1 || errorOut "Failed to uninstall."
installR "$installDir"
}
function main {
[ $# -lt 1 ] && usage
[ $# -gt 2 ] && usage
[ "$(whoami)" != "root" ] && errorOut "This script must be run as root."
#checks (otherwise we might remove /usr/lib/ ...)
command -v basename &> /dev/null || errorOut "basename is not installed."
command -v dirname &> /dev/null || errorOut "dirname is not installed."
command -v readlink &> /dev/null || errorOut "readlink is not installed."
#parse commands
local cmd="$1"
local installDir="$2"
[ -z "$installDir" ] && installDir="$DEFAULT_INSTALL_DIR"
case "$cmd" in
install)
installR "$installDir"
;;
uninstall)
uninstallR "$installDir"
;;
reinstall)
reinstallR "$installDir"
;;
*)
usage
;;
esac
echo "All done."
exit 0
}
main "$@"