-
Notifications
You must be signed in to change notification settings - Fork 8
/
.main.sh
180 lines (152 loc) · 3.32 KB
/
.main.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
### Main ###
set -e
function get_commands()
{
declare -F | grep '^declare -f do_' | sed 's>^.*do_>>'
}
function get_code_of_init()
{
if declare -F | grep -xq 'declare -f init'
then
local declaration="$(declare -f init)"
# Drop first 3 and last lines
echo "${declaration}" | tail -n +3 | head -n -1
fi
}
function show_title()
{
local dirname="$(basename "$(dirname "$0")")"
local prefix=
local args=
if [ "${dirname}" != '.' ]; then
prefix="${dirname}: "
fi
args="$(get_package_args)"
if [ ! "${args}" == '' ]
then
args=" [${args}]"
fi
echo -e "\n### ${prefix}${cmd}${args} ###"
}
function get_package_args()
{
local args=()
if [ "${GLOBAL_INSTALL}" == true ]
then
args+=('global')
fi
if [ "${PRODUCTION}" == true ]
then
args+=('production')
fi
if [ "${FORCE}" == true ]
then
args+=('force')
fi
joined="$(printf ",%s" "${args[@]}")"
echo "${joined:1}"
}
function show_help()
{
cat - << EOF
$(basename "$0") [global-args] command [command-args]
Available commands:
$(get_commands | sort | sed 's>^> * >')
Available global arguments:
-h, --help Show this help
-g, --global Install/make all changes on system
-p, --production Prepare environment for production use
-f, --force Do not ask anything
EOF
}
function run()
{
"./$(basename "$0")" "$@"
}
function apt_get_install()
{
local apt_args=
if [ "${FORCE}" == true ]
then
apt_args='-y'
fi
sudo apt-get install ${apt_args} "$@"
}
function purge()
{
local path="$1"
if [ -e "${path}" ]
then
echo -n "Removing ${path}... "
rm -r "${path}"
echo 'Done'
fi
}
function find_and_purge()
{
find "$@" | while read rm_path
do
if [ -e "${rm_path}" ]
then
purge "${rm_path}"
fi
done
}
function make_default()
{
local prefix="$1"
local suffix="$2"
if [ -e "${prefix}${suffix}" ]
then
return
fi
echo -n "Making default ${prefix}${suffix}... "
cp -p "${prefix}.default${suffix}" "${prefix}${suffix}"
echo "Done"
}
# Init
cmd=
args=
after_separator=false
export GLOBAL_INSTALL=${GLOBAL_INSTALL:-false}
export PRODUCTION=${PRODUCTION:-false}
export FORCE=${FORCE:-false}
while [ $# -gt 0 ]
do
if [ "${cmd}" != '' ]
then
args="${args} $*"
break
fi
case "$1" in
--help|-h) show_help
exit 0
;;
--global|-g) export GLOBAL_INSTALL=true
;;
--production|-p) export PRODUCTION=true
;;
--force|-f) export FORCE=true
;;
*) if get_commands | grep -qx -- "$1"
then
cmd="$1"
else
echo "Unknown command: $1" >&2
exit 1
fi
;;
esac
shift
done
if [ "${cmd}" == '' ]
then
echo "Missing command parameter!" >&2
show_help
exit 1
fi
# Start
eval "$(get_code_of_init)"
show_title
do_${cmd} ${args}
exit 0