-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.sh
executable file
·226 lines (201 loc) · 5.81 KB
/
pkg.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Dummy package manager using my dropbox
# ----------------------------------------------------------------------------
set -e
SCRIPT_DIR=`dirname "$BASH_SOURCE{0}"`
SCRIPT_HOME=`readlink -f "$SCRIPT_DIR"`
TOPDIR=`pwd`
. "$SCRIPT_HOME/resx/utils.sh"
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function wfetch {
NM=`echo $1 | tr '/' '\n' | tail -n 1`
echo '-> Download '$NM" at https://www.dropbox.com/s/$1"
if [ -f $NM ]; then
mv "$NM" "$NM.bak"
wget $WGET_ARGS https://www.dropbox.com/s/$1 -O "$NM" --no-check-certificate || (mv "$NM.bak" "$NM" && false)
rm "$NM.bak"
else
wget $WGET_ARGS https://www.dropbox.com/s/$1 -O "$NM" --no-check-certificate
fi
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function pack_identify {
IFS=':' read -ra TPKG <<< "$1"
export PNAME=${TPKG[0]}
export PACK=`echo ${TPKG[0]} | tr '-' '_'`
export VERS=${TPKG[1]}
if [ -z "$VERS" ]; then
K=kpkg_${PACK}_stable
VERS=${!K}
fi
if [ -z "$VERS" ]; then
VERS='latest'
fi
export VERS=`echo $VERS | tr '.' '_'`
export K=kpkg_${PACK}_${SYSOS}_${ARCH}_${VERS}
export PACK_KEY=${!K}
export PACK_NAME=`echo "$PACK_KEY" | cut -d '/' -f 2`
if [ -z "$PAGE_NAME" ] && [ -n "$LOCAL" ]; then
export PACK_NAME="${TPKG[0]}-${VERS}.tar.xz"
fi
K2=kpkg_${PACK}_${SYSOS}_${ARCH}_${VERS}_sha2
export PACK_SHA2=${!K2}
}
function pack_download {
echo_info "Package uniq key : $K"
if [ -z "$PACK_NAME" ]; then
PACK_NAME="${PNAME}-${VERS}.tar.xz"
if [ ! -f "$REPODIR/$PACK_NAME" ]; then
echo_warning "Unable to find package meta-data"
fi
return 0
fi
mkdir -p "$REPODIR"
cd "$REPODIR"
HASH='--'
if [ -f "$PACK_NAME" ]; then
HASH=`sha256sum "$PACK_NAME" | cut -f 1 -d ' '`
fi
# echo_info "Compare hash $HASH / $PACK_SHA2"
if [ "$HASH" != "$PACK_SHA2" ]; then
wfetch "$PACK_KEY"
else
true
# echo_info "Already up to date"
fi
}
function pack_install {
cd "$SCRIPT_HOME/packages/$TARGET"
NAME=`readlink -f "$PACK_NAME"`
if [ ! -d "$PREFIX" ]; then
echo_error "Prefix is not a directory $PREFIX"
fi
cd "$PREFIX"
if [ ! -f "$NAME" ]; then
echo_error "Unable to locate the package at $NAME"
fi
tar xf "$NAME"
}
function pack_uninstall {
cd "$SCRIPT_HOME/packages/$TARGET"
NAME=`readlink -f "$PACK_NAME"`
if [ ! -d "$PREFIX" ]; then
echo_error "Prefix is not a directory $PREFIX"
fi
cd "$PREFIX"
if [ ! -f "$NAME" ]; then
echo_error "Unable to locate the package at $NAME"
fi
tar tf "$NAME" | xargs rm
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function read_store {
echo_important "Package(s) for $SYSOS $ARCH"
NM="$SCRIPT_HOME/packages/index.yml"
parse_yaml "$NM" | grep "$SYSOS"_"$ARCH" | grep -v '_sha2' | sed "s/_"$SYSOS"_"$ARCH".*=.*//"
}
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
if [ -z "$PREFIX" ]; then
export PREFIX=`readlink -f $TOPDIR`
fi
TARGET=''
COMMAND=''
PACKAGE=''
UPDATE=''
LOCAL=''
# Analyze scripts parameters
while (( $# > 0)); do
case "$1" in
-u|--update)
UPDATE='true'
;;
--prefix=*)
export PREFIX=`readlink -f ${1:9}`
;;
--local)
LOCAL='true'
;;
--target=*)
export TARGET="${1:9}"
;;
-*)
echo_error "Unknown parameter $1"
;;
*)
if [ -n "$COMMAND" ]; then
if [ -n "$PACKAGE" ]; then
echo_error "Unexpected parameter $1"
fi
PACKAGE=$1
else
COMMAND=$1
fi
;;
esac
shift
done
# Initialize tools
TARGET=`$SCRIPT_DIR/make/host.sh $TARGET`
echo_info "Initialize package script"
echo_important "Select host architecture $TARGET"
IFS='-' read -ra THST <<< "$TARGET"
ARCH=${THST[0]}
SYSOS=`tr ' ' '-' <<< "${THST[@]:2:${#THST[@]}}" | tr '-' '_'`
REPODIR="$SCRIPT_HOME/packages/$TARGET"
if [ ! -f "$SCRIPT_HOME/packages/index.yml" ]; then
UPDATE='true'
fi
if [ -n "$UPDATE" ]; then
mkdir -p "$SCRIPT_HOME/packages"
cd "$SCRIPT_HOME/packages"
wfetch "gdz3pssw9kxfm1w/index.yml"
fi
. <(parse_yaml $SCRIPT_HOME/packages/index.yml 'kpkg_')
pack_identify "$PACKAGE"
echo_info " ----"
# Run the command
case "$COMMAND" in
'download')
pack_download
;;
'install')
if [ -z "$LOCAL" ]; then
pack_download
fi
pack_install
;;
'uninstall')
pack_uninstall
;;
'update')
pack_uninstall
pack_download
pack_install
;;
'list')
read_store
;;
'search')
read_store "$@"
;;
'help'|'')
echo "Dummy package manager served by Dropbox"
echo ""
echo "download <pkg> Downlaod a package"
echo "install <pkg> Downlaod and install a package"
echo "uninstall <pkg> Remove all files of a package"
echo "update <pkg> Re-install a package with its last version"
echo "list List all known package"
# echo "search <text> Look for a package or utility nammed like text"
# echo "upgrade Upgrade the store (no-use here, automatic)"
echo ""
echo "Current settings:"
echo "prefix='$PREFIX' arch='$ARCH' os='$SYSOS'"
# echo "publish <name, version, url>"
;;
*)
echo "Unknown command: $1" 1>&2
echo "Use '$0 help' to get the list of available commands." 1>&2
exit -1
;;
esac