sup view sup @ rev 42

Improvment and fixes after some usage/testing
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 08 00:15:04 2017 +0100 (2017-03-08)
parents d58ceb6b8fe9
children d86e4e894a97
line source
1 #!/bin/sh
2 #
3 # SliTaz Users Package - Minimal cmdline tools - README for devel info
4 #
5 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
6 # Author: Christophe Lincoln <pankso@slitaz.org>
7 #
9 # Get system wide libsup.sh or development version if working in Hg folder
10 if [ -f "/usr/lib/slitaz/libsup.sh" ]; then
11 . /usr/lib/slitaz/libsup.sh
12 fi
13 if [ -f "./libsup.sh" ]; then
14 . ./libsup.sh
15 fi
17 # Sanity check
18 if ! [ -d "/home/$(id -un)" ]; then
19 gettext "Not a valid user:"; colorize 31 " $(id -un)" && exit 1
20 fi
21 mkdir -p ${cache} ${installed}
23 #
24 # Functions
25 #
27 help() {
28 cat << EOT
30 $(boldify $(gettext "Usage:")) $(basename $0) [command|package] [package]
32 $(boldify $(gettext "Commands:"))
33 -c cook $(gettext "Cook a package from the wok")
34 -r run $(gettext "Run sup_install without installing")
35 -e extract $(gettext "Extract a package to current dir")
36 -i install $(gettext "Install a new package")
37 -u update $(gettext "Update installed package")
38 -r remove $(gettext "Remove package files")
39 -n new $(gettext "Create a new SUP package :-)")
40 -l list $(gettext "List installed packages in your wok")
42 $(boldify $(gettext "Options:"))
43 --install update $(gettext "Install all updates from mirror")
44 --force update $(gettext "Force recharging packages database")
45 --db list $(gettext "List all packages in database")
46 --init cook $(gettext "Initialize sup cook environment")
47 --verbose all $(gettext "Display more output messages")
49 $(colorize 33 Manual) man sup
50 $(colorize 33 Documentation) http://scn.slitaz.org/?d=en/sup
52 EOT
53 exit 0
54 }
56 cook_init() {
57 mkdir -p ${wok} ${cooked}
58 if [ ! -d "$supcook/wok" ]; then
59 ln -s ${wok} ${supcook}/wok
60 fi
61 }
63 # check_pkg_arg "command"
64 check_pkg_arg() {
65 if [ ! "$pkg" ]; then
66 gettext "Missing package name or file path:"
67 boldify " sup $1 [package|file.sup]" && exit 1
68 fi
69 }
71 # new_receip "package"
72 new_receip() {
73 cat > ${wok}/${1}/receip << EOT
74 # SliTaz User Package receip
76 PACKAGE="$1"
77 VERSION=""
78 SHORT_DESC=""
79 MAINTAINER=""
80 LICENSE=""
81 WEB_SITE=""
83 # Sup dependencies will be automatically downloaded but system wide
84 # deps must be manually installed by root.
85 SUP_DEPS=""
86 DEPENDS=""
88 # Here are the optional install rules (download, configure, etc)
89 sup_install() {
90 echo ""
91 }
93 EOT
94 }
96 #
97 # Commands
98 #
100 case "$1" in
102 -c|cook)
103 # Cook in $cache/cook and mv pkg to a visible dir
104 pkg="$2"
105 receip="$wok/$pkg/receip"
106 cache="$cache/cook"
108 # Handle --init
109 if [ "$init" ]; then
110 gettext "Initializing sup cook environment..."
111 cook_init; status; exit 0
112 fi
114 # Sanity check
115 check_pkg_arg "$1"
116 if [ ! -f "$receip" ]; then
117 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
118 fi
120 newline
121 echo -n "$(colorize 33 $(gettext 'Building package:'))"
122 colorize 35 " $pkg"
123 separator
124 rm -rf ${cache} && mkdir -p ${cache} ${cooked}
126 # Check receip
127 [ "$verbose" ] && gettext "Checking receip variables..."
128 . ${receip}
129 for var in PACKAGE VERSION SHORT_DESC MAINTAINER LICENSE WEB_SITE
130 do
131 value="$(. $receip; eval echo \$$var)"
132 case "$var" in
133 PACKAGE|VERSION|SHORT_DESC|MAINTAINER|LICENSE)
134 if [ ! "$value" ]; then
135 gettext "Empty variable:"; colorize 31 " $var=\"\""
136 exit 1
137 fi ;;
138 WEB_SITE)
139 if [ ! "$value" ]; then
140 gettext "Empty variable:"; colorize 31 " $var=\"\""
141 gettext "You can use:"
142 boldify " http://scn.slitaz.org/?sup&pkg=$PACKAGE"
143 exit 1
144 fi ;;
145 esac
146 done
147 [ "$verbose" ] && status
149 # Set $PACKAGE-$VERSION.sup and copy pkg to cache
150 supfile="$PACKAGE-$VERSION.sup"
151 cp -rf ${wok}/${pkg} ${cache}/${supfile%.sup}
152 cd ${cache}/${supfile%.sup}
154 # Move to hidden dir: ~/.local ~/.config ~./icons
155 for dir in local config icons; do
156 [ -d "files/${dir}" ] && mv files/${dir} files/.${dir}
157 done
159 # Add $cook_date
160 sed -i "/^cook_date=/"d receip
161 echo "cook_date=\"$(date "+%Y-%m-%d %H:%M")\"" >> receip
163 gettext "Creating compressed archive..."
164 find files | cpio -o -H newc --quiet | lzma e files.lzma -si
165 rm -rf files
166 find . -print | cpio -o -H newc --quiet > ${cooked}/${supfile}
167 status
169 size="$(du -sh $cooked/$supfile | cut -d " " -f 1)"
170 echo "$(gettext 'Packages:') $(colorize 035 $supfile) ($size)"
171 rm -rf ${cache} && newline ;;
173 -r|run)
174 # Run sup_install() without any installation to help package creation
175 pkg="$2"
176 receip="$wok/$pkg/receip"
177 workdir="$supcook/sup_install"
179 # Sanity check
180 check_pkg_arg "$1"
181 if [ ! -f "$receip" ]; then
182 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
183 fi
185 newline
186 echo "$(colorize 33 $(gettext 'Running') sup_install\(\):) $(colorize 35 $pkg)"
187 separator
188 [ "$clean" ] && rm -rf ${workdir}
189 mkdir -p ${workdir} && cd ${workdir}
190 . ${wok}/${pkg}/receip
191 gettext "Checking package dependencies"
192 deps="$(echo $DEPENDS | wc -w)"
193 in=$((8 + ${deps}))
194 indent $(($(tty_size) - ${in})) "[ $(colorize 033 $deps) ]"
195 check_sys_deps || exit 1
196 sup_install
197 separator
198 gettext "Working folder:"; echo " $workdir"
199 newline ;;
201 -n|new)
202 pkg="$2"
203 if [ ! "$pkg" ]; then
204 newline
205 gettext "New package name:"; echo -n " "; read pkg
206 fi
207 newline
208 [ ! "$pkg" ] && exit 0
209 [ ! -d "$wok" ] && cook_init
210 if [ -d "$wok/$pkg" ]; then
211 gettext "Package already exists in wok:"; colorize 35 " $pkg"
212 newline && exit 0
213 fi
214 gettext "Creating new package files and tree..."
215 mkdir -p ${wok}/${pkg}/files
216 new_receip "$pkg"; status
217 gettext "Executing your favorite editor..."
218 editor ${wok}/${pkg}/receip; status
219 gettext "To cook your package:"; colorize 33 " sup cook $pkg"
220 newline ;;
222 -e|extract)
223 # Extract package.sup to current dir
224 pkg="$2"
226 # Sanity check
227 check_pkg_arg "$1"
228 if [ ! -f "$pkg" ]; then
229 gettext "Can't find package file:"; colorize 31 " $pkg" && exit 1
230 fi
232 newline
233 echo -n "$(colorize 33 $(gettext 'Extract package:'))"
234 colorize 36 " $(basename $pkg)"
235 separator
237 extract_sup "$pkg"
239 echo "$(gettext 'Packages:') $(colorize 036 $pkg) ($size)"
240 newline ;;
242 -i|install)
243 # Download/extract run install() in $cache/install
244 pkg="$2"
245 check_pkg_arg "$1"
246 newline
248 # Sup pkg in current dir
249 if [ -f "$pkg" ]; then
250 install_sup "$pkg" && exit 0
251 fi
253 # Sup cooked from wok
254 if [ -f "$wok/$pkg/receip" ]; then
255 . $wok/$pkg/receip
256 if [ -f "$cooked/$PACKAGE-$VERSION.sup" ]; then
257 install_sup "$cooked/$PACKAGE-$VERSION.sup" && exit 0
258 fi
259 fi
261 # Sup on mirror
262 [ ! -f "${pkgsdb}" ] && sup -u
264 if sqlite3 ${pkgsdb} "SELECT name FROM pkgs WHERE name = '$pkg'" >/dev/null
265 then
266 vers=$(sqlite3 ${pkgsdb} "SELECT version FROM pkgs WHERE name = '$pkg'")
267 mkdir -p ${cache}/wget && cd ${cache}/wget
268 # Download
269 download "$pkg" "${mirror}/${pkg}-${vers}.sup"
270 install_sup "${pkg}-${vers}.sup"
271 rm -rf ${cache}/wget && exit 0
272 fi
273 gettext "Can't find sup package:"; colorize 31 " $pkg" ;;
275 -u|update)
276 # Update packages
277 online=""
278 newline
279 colorize 33 $(gettext "SUP Update")
280 separator
282 # Skip HTTP/1.1 404 Not Found: 2>/dev/null
283 if ! wget -q -T 5 --spider ${mirror}/packages.sql 2>/dev/null; then
284 gettext "Mirror is unreachable"; newline
285 else
286 online=0
287 fi
289 # Newer packages.sql ?
290 gettext "Checking packages.sql..."
291 db_sum=$(wget -q "${server}?sup=dbsum" -O -)
292 if echo "$db_sum $pkgsdb" | md5sum -s -c -; then
293 indent $(($(tty_size) - 13)) "[ $(colorize 036 synced) ]"
294 else
295 [ "$online" ] && rm -f ${pkgsdb}
296 indent $(($(tty_size) - 13)) "[ $(colorize 035 update) ]"
297 fi
299 # --force
300 [ "$force" ] && rm -f ${pkgsdb}
302 # No packages.sql: get latest!
303 if [ ! -f "$pkgsdb" ] && [ "$online" ]; then
305 download "db" "${mirror}/packages.sql" "${supdb}"
307 timestamp=$(sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
308 | awk '{printf $1 " " $2}')
309 gettext "Database timestamp:"
310 indent $(($(tty_size) - 23)) "[ $(colorize 30 $timestamp) ]"
311 log "$(gettext 'Updated packages database: $timestamp') "
312 fi
314 # Scan installed packages
315 count="$(ls $installed | wc -l)"
316 char="$(echo $count | wc -L)"
317 in=$((7 + ${char}))
319 gettext "Installed packages:"
320 indent $(($(tty_size) - ${in})) "[ $(colorize 033 $count) ]"
321 cat << EOT
323 $(boldify $(gettext "Package") $(indent 30 $(gettext "Version")) \
324 $(indent 60 $(gettext "Status")))
325 $(separator "-")
326 EOT
327 for pkg in $(ls $installed)
328 do
329 . ${installed}/${pkg}/receip
330 db_sum=$(sqlite3 ${pkgsdb} \
331 "SELECT md5sum FROM pkgs WHERE name = '$pkg'")
333 # MD5sum not matching: new version our new build ?
334 if [ "$md5_sum" != "$db_sum" ]; then
336 vers=$(sqlite3 ${pkgsdb} \
337 "SELECT version FROM pkgs WHERE name = '$pkg'")
339 if [ "$VERSION" != "$vers" ]; then
340 status="$(colorize 032 $(gettext "update") )"
341 else
342 status="$(colorize 035 $(gettext "rebuilt"))"
343 fi
344 echo "$pkg $(indent 30 $VERSION) $(indent 60 $status)"
345 echo "$pkg" >> ${cache}/updates
346 fi
348 done
350 if [ -f "$cache/updates" ]; then
351 count=$(cat $cache/updates | wc -l)
352 separator "-"
353 gettext "Available updates:"; colorize 32 " $count"
354 newline
355 # --install
356 if [ "$install" ]; then
357 for pkg in $(cat $cache/updates); do
358 sup -i ${pkg}
359 done
360 fi
361 else
362 gettext "Packages are up-to-date"; echo
363 newline
364 fi
365 rm -f ${cache}/updates ;;
367 -r|remove)
368 # Remove a package
369 pkg="$2"
370 check_pkg_arg "$1"
371 if [ -d "$installed/$pkg" ]; then
372 remove_sup "$pkg"
373 else
374 gettext "Can't find installed package:"; colorize 31 " $pkg"
375 exit 1
376 fi ;;
378 -s|search)
379 # TODO: better :-)
380 query="$2"
381 newline
382 echo -n "$(colorize 33 $(gettext 'Searching for:'))"
383 boldify " $query"
384 separator
385 sqlite3 ${pkgsdb} << EOT
386 .headers on
387 .mode column
388 SELECT name, version, short_desc FROM pkgs WHERE name = '$2';
389 EOT
390 newline ;;
392 -l|list)
393 # only --db
394 if [ "$db" ]; then
395 newline
396 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
397 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
398 separator
399 sqlite3 ${pkgsdb} << EOT
400 .mode column
401 SELECT name, short_desc FROM pkgs;
402 EOT
403 newline; exit 0
404 fi
406 installed_nb=$(ls ${installed} | wc -l)
407 newline
408 echo -n "$(colorize 36 $(gettext 'Installed SUP packages:'))"
409 boldify " $installed_nb"
410 separator
411 for pkg in $(ls ${installed}); do
412 . ${installed}/${pkg}/receip
413 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
414 done
415 if [ "$installed_nb" == 0 ]; then
416 gettext "No sup packages installed"; echo
417 fi
418 newline
419 if [ -d "$wok" ]; then
420 echo -n "$(colorize 35 $(gettext 'SUP packages in your wok:'))"
421 boldify " $(ls ${wok} | wc -l)"
422 separator
423 for pkg in $(ls ${wok}); do
424 . ${wok}/${pkg}/receip
425 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
426 done
427 newline
428 fi
429 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
430 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
431 newline ;;
433 ""|-h|help) help ;;
435 *)
436 # Display pkg info if installed
437 pkg="$1"
438 if [ -f "$installed/$pkg/receip" ]; then
439 . ${installed}/${pkg}/receip
440 newline
441 gettext "Installed package:"; colorize 36 " $PACKAGE $VERSION"
442 separator
443 echo "$SHORT_DESC"
444 newline
445 colorize 036 "$(gettext 'Installed files:') ($sup_size)"
446 cat ${installed}/${pkg}/files.list
447 newline
448 else
449 gettext "Can't find installed package:"; colorize 31 " $pkg"
450 fi ;;
452 esac && exit 0