-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·89 lines (77 loc) · 2.11 KB
/
install.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
#! /bin/bash
DOT_DIR="$(pwd)/$(dirname "$0")"
# import auxiliary functions
export DOT_OMZ_DIR=$HOME/.dot/apps/omz
source "$DOT_DIR/apps/omz/lib.sh"
env_check_and_setup() {
if ! set_gnu_realpath; then
error "Cannot find GNU realpath, please install via `brew install coreutils`"
exit 1
fi
}
# $1: an empty array. On return, this array will be initialized as app names
find_install_scripts() {
apps=()
for file in "$APP_DIR"/*-install.sh; do
file=$(basename $(realpath "$file"))
app=$(echo "$file" | sed 's/-install.sh//')
apps+=("$app")
done
echo "${apps[@]}"
}
print_help() {
printf -- "-h: %-s\n" "Print this help"
printf -- "-i [app]: %-s\n" "install this app"
printf -- "-l: %-s\n" "List available apps to install"
}
list_apps() {
local apps=("$@")
for idx in ${!apps[@]}; do
name=${apps[$idx]}
printf "%-2s: %-s\n" "$idx" "$name"
done
}
# $1: app name
# $2: the path of install script
install_app() {
name="$1"
install_script="$2"
if ! yes_or_no "Really want to install [$name]"; then
info "'N' is pressed, quit"
exit 0
fi
DOT_LOG_LEVEL="$DOT_LOG_LEVEL" \
APP_DIR="$APP_DIR" \
DOT_DIR="$DOT_DIR" \
exec "$install_script"
}
######################### MAIN #########################
env_check_and_setup
DOT_DIR=$(realpath "$DOT_DIR")
APP_DIR=$(realpath "$DOT_DIR/apps")
DOT_LOG_LEVEL="info"
apps=$(find_install_scripts)
while getopts ":hli:" opt; do
case $opt in
h)
print_help
;;
l)
printf -- "-------------------------- Apps --------------------------\n"
list_apps ${apps[@]}
;;
i)
name="$OPTARG"
install_script=$(realpath "$APP_DIR/$name-install.sh" 2>/dev/null)
if [ ! -f "$install_script" ]; then
error "no [$name] app is found in $APP_DIR"
exit 1
fi
install_app "$name" "$install_script"
;;
*)
error "unknown option: -$OPTARG"
print_help
;;
esac
done