-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·82 lines (66 loc) · 1.54 KB
/
functions.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
#!/bin/bash
OS_FUNCTIONS=$(uname | tr '[:upper:]' '[:lower:]')-functions.sh
if [ -f "$OS_FUNCTIONS" ]; then
source $OS_FUNCTIONS
else
echo "Couldn't find $OS_FUNCTIONS. This OS isn't supported"
exit 1
fi
function is_sudo {
! [ -z "$SUDO_USER" ] && return
}
# This function assumes the command name is used as the name of the package.
# This is true for all cases in this project so far.
function install_command {
for c in "$@" ; do
if ! [ -x "$(command -v $c)" ]; then
install_package $c
fi
done
}
function install_from_url {
install_command wget tar
if [ "$#" -lt 3 ] ; then
echo "This function expects at least 3 parameters"
exit 1
fi
local URL=$1
local PROGRAM_NAME=$2
local DIST_NAME=$3
if [ "$#" -gt 3 ] ; then
BASE_PATH=$4
else
BASE_PATH=/opt
fi
if [ "$#" -gt 4 ] ; then
local PATH_SUFFIX=$5
else
local PATH_SUFFIX=bin
fi
DEST_PATH=$BASE_PATH/$DIST_NAME
echo "Package url is $URL"
if ! is_sudo ; then
TAR_CMD="sudo tar"
RM_CMD="sudo rm"
else
TAR_CMD="tar"
RM_CMD="rm"
fi
if [[ $URL == *.gz ]] ; then
TAR_CMD="$TAR_CMD -xzf"
elif [[ $URL == *.xz ]] ; then
TAR_CMD="$TAR_CMD -J -xvf"
else
echo "Unsupported file extension $URL"
exit 1
fi
if [ -d $DEST_PATH ] ; then
echo "Replacing contents on $DEST_PATH"
$RM_CMD -r $DEST_PATH
else
echo "Installing on $DEST_PATH"
fi
wget --quiet -O - $URL | $TAR_CMD - -C $BASE_PATH
ENV_FILE=$(add_to_path $PROGRAM_NAME "$DEST_PATH/$PATH_SUFFIX")
echo "Created $ENV_FILE"
}