-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·134 lines (100 loc) · 3.41 KB
/
install.sh
File metadata and controls
executable file
·134 lines (100 loc) · 3.41 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
set -e
# Project installation script
#
# Usage:
# $ [options] /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/make-ops-tools/detect-operating-system/main/install.sh)"
#
# Options:
# BRANCH_NAME=other-branch-than-main # Default is `main`
# INSTALL_DIR=other-dir-than-install-dir # Default is `~/.local/share/$ORG_NAME/$REPO_NAME`
# CLONE_REPO=true # Default is `false`
# VERBOSE=true # Show all the executed commands, default is `false`
# ==============================================================================
export TERM=xterm-256color
SCRIPT_DIR=$([ -n "${BASH_SOURCE[0]}" ] && cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd || dirname "$(readlink -f "$0")")
ORG_NAME=make-ops-tools
REPO_NAME=detect-operating-system
PROJECT_NAME=$ORG_NAME-$REPO_NAME
BRANCH_NAME=${BRANCH_NAME:-main}
INSTALL_DIR=${INSTALL_DIR:-$HOME/.local/share/$ORG_NAME/$REPO_NAME}
CLONE_REPO=${CLONE_REPO:-false}
CMD_PATH=$HOME/.local/bin
CMD_NAME=$REPO_NAME.sh
# ==============================================================================
function main() {
cd $SCRIPT_DIR
clone || download
check && install
finish
}
function clone() {
if (is-arg-false "$CLONE_REPO" || ! is-arg-true "$CLONE_REPO") then
return 1
fi
if ! [ -d "$INSTALL_DIR/.git" ]; then
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"
git clone https://github.com/$ORG_NAME/$REPO_NAME.git .
else
cd "$INSTALL_DIR"
git pull --all
fi
git checkout "$BRANCH_NAME"
}
function download() {
curl -L \
"https://github.com/$ORG_NAME/$REPO_NAME/tarball/$BRANCH_NAME?$(date +%s)" \
-o /tmp/$PROJECT_NAME.tar.gz
tar -zxf /tmp/$PROJECT_NAME.tar.gz -C /tmp
rm -rf /tmp/$PROJECT_NAME.tar.gz
rm -rf $INSTALL_DIR
mkdir -p $(dirname $INSTALL_DIR)
mv /tmp/$PROJECT_NAME-* "$INSTALL_DIR"
cd "$INSTALL_DIR"
}
# ==============================================================================
function check() {
present=$(tput setaf 64; printf present;tput sgr0)
missing=$(tput setaf 196; printf missing;tput sgr0)
printf "Prerequisites:\n"
[ -x /bin/bash ] && value=$present || value=$missing
printf "bash [%s]\n" "$value"
(make --version 2> /dev/null | grep -i "gnu make" | grep -Eq '[4]\.[0-9]+') && value=$present || value=$missing
printf "make [%s]\n" "$value"
which git > /dev/null 2>&1 && value=$present || value=$missing
printf "git [%s]\n" "$value"
which docker > /dev/null 2>&1 && value=$present || value=$missing
printf "docker [%s]\n" "$value"
}
function install() {
mkdir -pv $CMD_PATH
ln -sfv \
$INSTALL_DIR/scripts/$CMD_NAME \
$CMD_PATH/$CMD_NAME
}
function finish() {
printf "Project directory %s\n" "$INSTALL_DIR"
printf "Executable %s\n" "$CMD_PATH/$CMD_NAME"
printf "Add to your PATH the $CMD_PATH directory, i.e. 'PATH=\"$CMD_PATH:\$PATH\"' or\n"
printf "create an alias i.e. 'alias detect-os=\"$CMD_PATH/$CMD_NAME | sed 's/export //'\"'\n"
}
# ==============================================================================
function is-arg-true() {
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return 0
else
return 1
fi
}
function is-arg-false() {
if [[ "$1" =~ ^(false|no|n|off|0|FALSE|NO|N|OFF)$ ]]; then
return 0
else
return 1
fi
}
# ==============================================================================
is-arg-true "$VERBOSE" && set -x
main $*
exit 0