1- #! /bin/bash
1+ #! /usr/ bin/env bash
22#
33# # #
44# #mmm mmmm # m mmmm
99# " ""
1010# bash package manager
1111
12+ VERSION=" 0.3.1"
13+ TAG=${TAG:- $VERSION }
14+ BRANCH=${BRANCH:- $TAG }
1215REMOTE=${REMOTE:- https:// github.com/ bpkg/ bpkg.git}
1316TMPDIR=${TMPDIR:-/ tmp}
14- DEST=${DEST:- ${ TMPDIR} / bpkg-master }
17+ DEST=${DEST:- $TMPDIR / bpkg-$BRANCH }
1518
1619# # test if command exists
1720ftest () {
18- echo " info: Checking for ${1} ..."
19- if ! type -f " ${1} " > /dev/null 2>&1 ; then
21+ echo " info: Checking for $1 ..."
22+ if ! type -f " $1 " > /dev/null 2>&1 ; then
2023 return 1
2124 else
2225 return 0
@@ -26,8 +29,8 @@ ftest () {
2629# # feature tests
2730features () {
2831 for f in " ${@ } " ; do
29- ftest " ${f} " || {
30- echo >&2 " error: Missing \` ${f} '! Make sure it exists and try again."
32+ ftest " $f " || {
33+ echo >&2 " error: Missing \` $f '! Make sure it exists and try again."
3134 return 1
3235 }
3336 done
@@ -43,13 +46,15 @@ setup () {
4346 # # build
4447 {
4548 echo
46- cd " ${TMPDIR} "
4749 echo " info: Creating temporary files..."
48- test -d " ${DEST} " && { echo " warn: Already exists: '${DEST} '" ; }
49- rm -rf " ${DEST} "
50- echo " info: Fetching latest 'bpkg'..."
51- git clone --depth=1 " ${REMOTE} " " ${DEST} " > /dev/null 2>&1
52- cd " ${DEST} "
50+ cd " $TMPDIR " || exit
51+ test -d " $DEST " && { echo " warn: Already exists: '$DEST '" ; }
52+ rm -rf " $DEST "
53+
54+ echo " info: Fetching 'bpkg@$BRANCH '..."
55+ git clone --depth=1 --branch " $BRANCH " " $REMOTE " " $DEST " > /dev/null 2>&1
56+ cd " $DEST " || exit
57+
5358 echo " info: Installing..."
5459 echo
5560 make_install
@@ -60,39 +65,86 @@ setup () {
6065
6166# # make targets
6267BIN=" bpkg"
63- [ -z " $PREFIX " ] && PREFIX=" /usr/local"
68+ if [ -z " $PREFIX " ]; then
69+ if [ " $( whoami) " == " root" ]; then
70+ PREFIX=" /usr/local"
71+ else
72+ PREFIX=" $HOME /.local"
73+ fi
74+ fi
6475
6576# All 'bpkg' supported commands
66- CMDS=" json install package term suggest init utils update list show getdeps"
77+ declare -a CMDS=()
78+ CMDS+=(" env" )
79+ CMDS+=(" getdeps" )
80+ CMDS+=(" init" )
81+ CMDS+=(" install" )
82+ CMDS+=(" json" )
83+ CMDS+=(" list" )
84+ CMDS+=(" package" )
85+ CMDS+=(" run" )
86+ CMDS+=(" show" )
87+ CMDS+=(" source" )
88+ CMDS+=(" suggest" )
89+ CMDS+=(" term" )
90+ CMDS+=(" update" )
91+ CMDS+=(" utils" )
92+ CMDS+=(" realpath" )
6793
6894make_install () {
95+ local source
96+
97+ # # do 'make uninstall'
6998 make_uninstall
99+
70100 echo " info: Installing $PREFIX /bin/$BIN ..."
71101 install -d " $PREFIX /bin"
72- local source=$( < $BIN )
73- [ -f " $source " ] && install " $source " " $PREFIX /bin/$BIN " || install " $BIN " " $PREFIX /bin"
74- for cmd in $CMDS ; do
75- source=$( < $BIN -$cmd )
76- [ -f " $source " ] && install " $source " " $PREFIX /bin/$BIN -$cmd " || install " $BIN -$cmd " " $PREFIX /bin"
102+ source=$( < $BIN )
103+
104+ if [ -f " $source " ]; then
105+ install " $source " " $PREFIX /bin/$BIN "
106+ else
107+ install " $BIN " " $PREFIX /bin"
108+ fi
109+
110+ for cmd in " ${CMDS[@]} " ; do
111+ if test -f " $BIN -$cmd " ; then
112+ source=$( < " $BIN -$cmd " )
113+
114+ if [ -f " $source " ]; then
115+ install " $source " " $PREFIX /bin/$BIN -$cmd "
116+ else
117+ install " $BIN -$cmd " " $PREFIX /bin"
118+ fi
119+ fi
120+
77121 done
78122 return $?
79123}
80124
81125make_uninstall () {
82- echo " info: Uninstalling $PREFIX /bin/$BIN ..."
126+ echo " info: Uninstalling $PREFIX /bin/$BIN *"
127+ echo " rm: $PREFIX /bin/$BIN '"
83128 rm -f " $PREFIX /bin/$BIN "
84- for cmd in $CMDS ; do
85- rm -f " $PREFIX /bin/$BIN -$cmd "
129+ for cmd in " ${CMDS[@]} " ; do
130+ if test -f " $PREFIX /bin/$BIN -$cmd " ; then
131+ echo " rm: $PREFIX /bin/$BIN -$cmd '"
132+ rm -f " $PREFIX /bin/$BIN -$cmd "
133+ fi
86134 done
87135 return $?
88136}
89137
90138make_link () {
91139 make_uninstall
92- echo " info: Linking $PREFIX /bin/$BIN ..."
140+ echo " info: Linking $PREFIX /bin/$BIN *"
141+ echo " link: '$PWD /$BIN ' -> '$PREFIX /bin/$BIN '"
93142 ln -s " $PWD /$BIN " " $PREFIX /bin/$BIN "
94- for cmd in $CMDS ; do
95- ln -s " $PWD /$BIN -$cmd " " $PREFIX /bin"
143+ for cmd in " ${CMDS[@]} " ; do
144+ if test -f " $PWD /$BIN -$cmd " ; then
145+ echo " link: '$PWD /$BIN -$cmd ' -> '$PREFIX /bin/$BIN -$cmd '"
146+ ln -s " $PWD /$BIN -$cmd " " $PREFIX /bin/$BIN -$cmd "
147+ fi
96148 done
97149 return $?
98150}
@@ -101,6 +153,11 @@ make_unlink () {
101153 make_uninstall
102154}
103155
104- # # go
105- [ $# -eq 0 ] && setup || make_$1
156+ # # do setup or `make_{install|uninstall|link|unlink}` command
157+ if [ $# -eq 0 ]; then
158+ setup
159+ else
160+ " make_$1 "
161+ fi
162+
106163exit $?
0 commit comments