-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.sh
203 lines (166 loc) · 3.3 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/sh
BISTDIR=$HOME/.bist
BISTDIR_BIN=$BISTDIR/bin
BISTPATH=$BISTDIR_BIN/bist
mkdir -p $BISTDIR_BIN
cat <<'BISTCONTENT' > $BISTPATH
#!/bin/zsh
#
# consts
#
BISTDIR=$HOME/.bist
BISTDIR_BIN=$BISTDIR/bin
BISTDIR_REPOS=$BISTDIR/repos
GIST_URL=https://gist.github.com/
#
# usage
#
function usage() {
cat <<EOT
USAGE:
bist [COMMAND] [ARGS]*
COMMAND:
list
listup local gist repository.
list [GIST_NUMBER]
listup specified gist files.
show [GIST_NUMBER | FILENAME]
cat gist file.
install [GIST_NUMBER]
clone or pull gist files.
update
update all local gist files.
rehash
symbolic link to bin
rm [GIST_NUMBER]
remove gist repository.
EOT
exit 0
}
#
# bist chmod [gist_num]
#
function bist_chmod() {
gistdir=$1
for f in `find $gistdir -type f -depth 1`
do
chmod a+x $f
done
}
#
# bist install [gist_num]
#
function bist_install() {
if [ $# -lt 1 ]; then
usage
fi
gistnum=$1
clonedir=$BISTDIR_REPOS/$gistnum
if [ ! -d $clonedir ]; then
git clone "${GIST_URL}${gistnum}.git" $clonedir > /dev/null 2>&1
else
cd $clonedir
git pull --rebase > /dev/null 2>&1
cd - > /dev/null
fi
bist_chmod $clonedir
}
#
# bist update
#
function bist_update() {
for d in `find $BISTDIR_REPOS -type d -depth 1`
do
gist_num=`basename $d`
echo pull $gist_num
bist_install $gist_num
done
}
#
# bist show [num | file]
#
function bist_show() {
if [ $# -lt 1 ]; then
usage
fi
num_or_file=$1
isnum=`echo $num_or_file | egrep "^[0-9]+$"`
file=""
if [ $isnum -a -d "$BISTDIR_REPOS/$num_or_file" ]; then
file=`find $BISTDIR_REPOS/$num_or_file -type f -depth 1 | head -1`
else
file=`find $BISTDIR_REPOS -type f -depth 2 | grep $num_or_file | head -1`
fi
if [ "$file" -a -f "$file" ]; then
cat $file
else
echo "$file not found." >&2
exit 0
fi
}
#
# bist list [num]?
#
function bist_list() {
if [ $# -eq 1 ]; then
ls "$BISTDIR_REPOS/$1"
else
for d in `find $BISTDIR_REPOS -type d -depth 1`
do
echo "`basename $d` `ls $d`"
done
fi
}
#
# bist rehash
#
function bist_rehash() {
mkdir -p "$BISTDIR_BIN"
for d in `find $BISTDIR_REPOS -type d -depth 1`
do
file=`find $d -type f -depth 1 | head -1`
ln -Fs $file $BISTDIR_BIN/
done
}
#
# bist rm [num]
#
function bist_rm () {
if [ $# -ne 1 ]; then
usage
fi
gistdir=$BISTDIR_REPOS/$2
if [ -d $gistdir ]; then
echo $gistdir
rm -rf $gistdir
fi
}
function bist_init () {
mkdir -p $BISTDIR_REPOS
mkdir -p $BISTDIR_BIN
}
# -----------------------------------
# main
# -----------------------------------
if [ $# -lt 1 ]; then
usage
fi
command=$1
shift
case $command in
"list" | "l" ) bist_list $* ;;
"show" | "s" ) bist_show $* ;;
"rehash" | "r" ) bist_rehash ;;
"rm" ) bist_rm $* ;;
"install" | "i" ) bist_install $* ;;
"init" ) bist_init $* ;;
"update" | "u" ) bist_update $* ;;
esac
BISTCONTENT
chmod a+x $BISTPATH
sh $BISTPATH init
cat <<USAGE
please add PATH to your .zprofile or .bash_profile
# bist path
export PATH=\$PATH:$BISTDIR_BIN
USAGE