-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.sh
executable file
·133 lines (114 loc) · 2.26 KB
/
setup.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
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
#!/usr/bin/env bash
#
# Copyright (C) 2019 Saalim Quadri (danascape)
#
# SPDX-License-Identifier: Apache-2.0 license
#
declare -r app_name='sw'
##
## Following are the install paths
##
# Paths used during the installation process]
declare -r binpath="$HOME/.local/bin"
declare -r srcpath="$HOME/.local/$app_name"
declare -r swbinpath="$HOME/.local/bin/$app_name"
# Source code references
declare -r CONFIGDIR='configs'
declare -r SRCDIR='src'
safe_append()
{
if [[ $(grep -c -x "$1" "$2") == 0 ]]; then
printf '%s\n' "$1" >> "$2"
fi
}
sworkflow_setup_help()
{
echo "Usage: ./setup.sh [options]"
echo -e "\nCommands\n" \
"\tinstall,i - Install sworkflow\n" \
"\thelp,h - Print this help message\n" \
"\tremove,r - Remove sworkflow\n" \
"\tupdate,u - Update sworkflow\n"
}
sworkflow_remove_files()
{
echo "sworkflow: Uninstalling files"
rm "$swbinpath"
rm -rf "$srcpath"
echo "error: Not automatically updating PATH"
}
sworkflow_update_files()
{
echo "sworkflow: Updating files"
echo "sworkflow: Fetching Updates"
git clone --depth 1 https://github.com/danascape/sworkflow /tmp/sworkflow || trigger_error
cd /tmp/sworkflow || trigger_error
./setup.sh i
rm -rf /tmp/sworkflow
}
sworkflow_synchronize_files()
{
mkdir -p "$binpath"
mkdir -p "$srcpath"
cp $app_name "$swbinpath"
rsync -vr $CONFIGDIR "$srcpath"
rsync -vr $SRCDIR "$srcpath"
if [[ -z "$(which bash)" ]]; then
echo "error: No bash"
trigger_error
else
if [[ -f "$HOME/.bashrc" ]]; then
update_path '.bashrc'
else
echo "warning: Unable to find a .bashrc file"
fi
fi
}
trigger_error()
{
echo "sworkflow: Failed to install"
exit 1
}
update_path()
{
local shellrc=${1:-'.bashrc'}
IFS=':' read -ra ALL_PATHS <<< "$PATH"
for path in "${ALL_PATHS[@]}"; do
[[ "$path" -ef "$binpath" ]] && return
done
safe_append "PATH=${HOME}/.local/bin:\$PATH # sw" "${HOME}/${shellrc}"
}
setup()
{
argument="$1"
case "$argument" in
install | i)
(
echo "sworkflow: Installing files"
sworkflow_synchronize_files
)
;;
help | h)
(
sworkflow_setup_help
)
;;
remove | r)
(
sworkflow_remove_files
)
;;
update | u)
(
sworkflow_update_files
)
;;
*)
(
echo "error: Invalid Option"
sworkflow_setup_help
)
;;
esac
}
setup "$@"