-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config
151 lines (132 loc) · 3.23 KB
/
config
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
#!/usr/bin/env -S zsh
# File........: config
# Summary.....: Install/Uninstall sourceable files
# Created.....: December 18, 2023
# Authors.....: Alex A. Davronov <al.neodim@gmail.com> (2023-)
# Repository..: N/A
# Description.:
# Usage.......: Run in shell
config-help(){
cat <<EOL
config COMMAND [OPTIONS]
Install/uninstall script for std.sh
COMMANDS
link - link src/ content to \$HOME/.local/bin
install - like link, but copy instead
uninstall - remove installed files
status - list installed files
OPTIONS
--user - user-local install
EOL
}
## report installed files by using ls
config-status(){
local 0="config"
local __PREFIX=${1:? prefix is required}
shift
local __FILES=($@)
ls -uhoa $(printf "${__PREFIX}/%s\n" ${__FILES[@]##src/})
}
## uninstall list of files
config-uninstall(){
local 0="config"
local __PREFIX=${1:? prefix is required}
shift
local __FILES=($@)
__FILES=(${__FILES[@]##src/})
for FILE in ${__FILES[@]}; do
local __FILE_PATH="${__PREFIX}/${FILE}"
[[ -f "${__FILE_PATH}" ]] && rm -i ${__FILE_PATH}
done
}
config(){
local PREFIX=.
local -A __FLAGS_MAP=()
local __FLAGS_REST=()
local __FLAGS=()
local __REST=()
local __INSTALL_FILES=(src/*)
local __INSTALL_USER_LOCAL=false
local __INSTALL_SYSTEM_PREFIX=/usr/bin
local __INSTALL_USER_PREFIX=$HOME/.local/bin
while (( $# > 0 )); do
case ${1} in
(-v|--version)
# config-version
return
;;
(-h|--help)
# config-help
return
;;
(link)
[[ -d "${__INSTALL_USER_PREFIX}" ]] && mkdir -v "${__INSTALL_USER_PREFIX}"
shift
case "${1}" in
(--user)
__INSTALL_USER_LOCAL=true
;;
esac
if ${__INSTALL_USER_LOCAL};
then
for file in ${__INSTALL_FILES[@]}; do
ln -f -s $(realpath $file) "${__INSTALL_USER_PREFIX}/"
done
else
for file in ${__INSTALL_FILES[@]}; do
ln -f -s $(realpath $file) "${__INSTALL_SYSTEM_PREFIX}/"
done
fi
config-status "${__INSTALL_USER_PREFIX}" ${__INSTALL_FILES[@]}
return
;;
(install)
[[ -d "${__INSTALL_USER_PREFIX}" ]] && mkdir -v "${__INSTALL_USER_PREFIX}"
shift
case "${1}" in
(--user)
__INSTALL_USER_LOCAL=true
;;
esac
if ${__INSTALL_USER_LOCAL};
then
install ${__INSTALL_FILES[@]} -t "${__INSTALL_USER_PREFIX}/"
config-status "${__INSTALL_USER_PREFIX}" ${__INSTALL_FILES[@]}
else
install ${__INSTALL_FILES[@]} -t "${__INSTALL_SYSTEM_PREFIX}/"
config-status "${__INSTALL_SYSTEM_PREFIX}" ${__INSTALL_FILES[@]}
fi
return
;;
(status)
config-status "${__INSTALL_USER_PREFIX}" ${__INSTALL_FILES[@]}
config-status "${__INSTALL_SYSTEM_PREFIX}" ${__INSTALL_FILES[@]}
return
;;
(uninstall)
config-uninstall "${__INSTALL_USER_PREFIX}" ${__INSTALL_FILES[@]}
config-uninstall "${__INSTALL_SYSTEM_PREFIX}" ${__INSTALL_FILES[@]}
return
;;
# (-*)
# __FLAGS+=("$1")
# echo -e "${0}: $(tput setaf 1)error:$(tput op) unknown option: ${1}" >&2;
# return 1
# ;;
(*)
config-help
# If you are going to use __REST, remove break
# __REST+=("$1")
shift
break
;;
# switch.stub
esac
done;
}
# Run script only if it was directly invoked
if [[ ${ZSH_ARGZERO} == $0 ]];
then
config "${@}"
fi
# ex: ft=zsh