sup view sup @ rev 49

Tiny edits
author Paul Issott <paul@slitaz.org>
date Fri Mar 10 18:44:59 2017 +0000 (2017-03-10)
parents 9ff1a3400fb1
children 46a8dc676569
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} ${downloads}
23 #
24 # Functions
25 #
27 help() {
28 cat << EOT
30 $(boldify $(gettext "Usage:")) $(basename $0) [command|package] [package|--option]
32 $(boldify $(gettext "Commands:"))
33 -i install $(gettext "Install a new package")
34 -u update $(gettext "Update installed package")
35 -r remove $(gettext "Remove an installed package")
36 -c cook $(gettext "Cook a package from the wok")
37 run $(gettext "Run sup_install without installing")
38 -e extract $(gettext "Extract a package to current dir")
39 -n new $(gettext "Create a new SUP package :-)")
40 -l list $(gettext "List packages: installed/wok/db")
41 -s search $(gettext "Search for packages in the database")
42 -d debug $(gettext "Debug info and sup environment")
44 $(boldify $(gettext "Options:"))
45 --install update $(gettext "Install all updates from mirror")
46 --force update $(gettext "Force recharging packages database")
47 --db list $(gettext "List all packages in database")
48 --init cook $(gettext "Initialize sup cook environment")
49 --list info $(gettext "List files installed by a package")
50 --verbose all $(gettext "Display more output messages")
52 $(colorize 33 Manual) man sup
53 $(colorize 33 Documentation) http://scn.slitaz.org/?d=en/sup
55 EOT
56 exit 0
57 }
59 cook_init() {
60 mkdir -p ${wok} ${cooked}
61 if [ ! -d "$supcook/wok" ]; then
62 ln -s ${wok} ${supcook}/wok
63 fi
64 }
66 # check_pkg_arg "command"
67 check_pkg_arg() {
68 if [ ! "$pkg" ]; then
69 gettext "Missing package name or file path:"
70 boldify " sup $1 [package|file.sup]" && exit 1
71 fi
72 }
74 # new_receip "package"
75 new_receip() {
76 cat > ${wok}/${1}/receip << EOT
77 # SliTaz User Package receip
79 PACKAGE="$1"
80 VERSION=""
81 SHORT_DESC=""
82 MAINTAINER=""
83 LICENSE=""
84 WEB_SITE=""
86 # Sup dependencies will be automatically downloaded but system wide
87 # deps must be manually installed by root.
88 SUP_DEPS=""
89 DEPENDS=""
91 # Here are the optional install rules (download, configure, etc)
92 sup_install() {
93 echo ""
94 }
96 EOT
97 }
99 #
100 # Commands
101 #
103 case "$1" in
105 -c|cook)
106 # Cook in $cache/cook and mv pkg to a visible dir
107 pkg="$2"
108 receip="$wok/$pkg/receip"
109 cache="$cache/cook"
111 # Handle --init
112 if [ "$init" ]; then
113 gettext "Initializing sup cook environment..."
114 cook_init; status; exit 0
115 fi
117 # Sanity check
118 check_pkg_arg "$1"
119 if [ ! -f "$receip" ]; then
120 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
121 fi
123 newline
124 echo -n "$(colorize 33 $(gettext 'Building package:'))"
125 colorize 35 " $pkg"
126 separator
127 rm -rf ${cache} && mkdir -p ${cache} ${cooked}
129 # Check receip
130 [ "$verbose" ] && gettext "Checking receip variables..."
131 . ${receip}
132 for var in PACKAGE VERSION SHORT_DESC MAINTAINER LICENSE WEB_SITE
133 do
134 value="$(. $receip; eval echo \$$var)"
135 case "$var" in
136 PACKAGE|VERSION|SHORT_DESC|MAINTAINER|LICENSE)
137 if [ ! "$value" ]; then
138 gettext "Empty variable:"; colorize 31 " $var=\"\""
139 exit 1
140 fi ;;
141 WEB_SITE)
142 if [ ! "$value" ]; then
143 gettext "Empty variable:"; colorize 31 " $var=\"\""
144 gettext "You can use:"
145 boldify " http://scn.slitaz.org/?sup&pkg=$PACKAGE"
146 exit 1
147 fi ;;
148 esac
149 done
150 [ "$verbose" ] && status
152 # Set $PACKAGE-$VERSION.sup and copy pkg to cache
153 supfile="$PACKAGE-$VERSION.sup"
154 cp -rf ${wok}/${pkg} ${cache}/${supfile%.sup}
155 cd ${cache}/${supfile%.sup}
157 # Move to hidden dir: ~/.local ~/.config ~./icons
158 for dir in local config icons; do
159 [ -d "files/${dir}" ] && mv files/${dir} files/.${dir}
160 done
162 # Add $cook_date
163 sed -i "/^cook_date=/"d receip
164 echo "cook_date=\"$(date "+%Y-%m-%d %H:%M")\"" >> receip
166 gettext "Creating compressed archive..."
167 find files | cpio -o -H newc --quiet | lzma e files.lzma -si
168 rm -rf files
169 find . -print | cpio -o -H newc --quiet > ${cooked}/${supfile}
170 status
172 size="$(du -sh $cooked/$supfile | cut -d " " -f 1)"
173 echo "$(gettext 'Packages:') $(colorize 035 $supfile) ($size)"
174 rm -rf ${cache} && newline ;;
176 run)
177 # Run sup_install() without any installation to help package creation
178 pkg="$2"
179 receip="$wok/$pkg/receip"
180 workdir="$supcook/sup_install"
182 # Sanity check
183 check_pkg_arg "$1"
184 if [ ! -f "$receip" ]; then
185 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
186 fi
188 newline
189 echo "$(colorize 33 $(gettext 'Running') sup_install\(\):) $(colorize 35 $pkg)"
190 separator
191 [ "$clean" ] && rm -rf ${workdir}
192 mkdir -p ${workdir} && cd ${workdir}
193 . ${wok}/${pkg}/receip
194 gettext "Checking package dependencies"
195 deps="$(echo $DEPENDS | wc -w)"
196 in=$((8 + ${deps}))
197 indent $(($(tty_size) - ${in})) "[ $(colorize 033 $deps) ]"
198 check_sys_deps || exit 1
199 sup_install
200 separator
201 gettext "Working folder:"; echo " $workdir"
202 newline ;;
204 -n|new)
205 pkg="$2"
206 if [ ! "$pkg" ]; then
207 newline
208 gettext "New package name:"; echo -n " "; read pkg
209 fi
210 newline
211 [ ! "$pkg" ] && exit 0
212 [ ! -d "$wok" ] && cook_init
213 if [ -d "$wok/$pkg" ]; then
214 gettext "Package already exists in wok:"; colorize 35 " $pkg"
215 newline && exit 0
216 fi
217 gettext "Creating new package files and tree..."
218 mkdir -p ${wok}/${pkg}/files
219 new_receip "$pkg"; status
220 gettext "Executing your favorite editor..."
221 editor ${wok}/${pkg}/receip; status
222 gettext "To cook your package:"; colorize 33 " sup cook $pkg"
223 newline ;;
225 -e|extract)
226 # Extract package.sup to current dir
227 pkg="$2"
229 # Sanity check
230 check_pkg_arg "$1"
231 if [ ! -f "$pkg" ]; then
232 gettext "Can't find package file:"; colorize 31 " $pkg" && exit 1
233 fi
235 newline
236 echo -n "$(colorize 33 $(gettext 'Extract package:'))"
237 colorize 36 " $(basename $pkg)"
238 separator
240 extract_sup "$pkg"
242 echo "$(gettext 'Packages:') $(colorize 036 $pkg) ($size)"
243 newline ;;
245 -i|install)
246 # Download/extract run install() in $cache/install
247 pkg="$2"
248 check_pkg_arg "$1"
249 newline
251 # Sup pkg in current dir
252 if [ -f "$pkg" ]; then
253 install_sup "$pkg" && exit 0
254 fi
256 # Sup cooked from wok
257 if [ -f "$wok/$pkg/receip" ]; then
258 . $wok/$pkg/receip
259 if [ -f "$cooked/$PACKAGE-$VERSION.sup" ]; then
260 install_sup "$cooked/$PACKAGE-$VERSION.sup" && exit 0
261 fi
262 fi
264 # Sup on mirror
265 [ ! -f "${pkgsdb}" ] && sup -u
267 if sqlite3 ${pkgsdb} "SELECT name FROM pkgs WHERE name = '$pkg'" >/dev/null
268 then
269 vers=$(sqlite3 ${pkgsdb} "SELECT version FROM pkgs WHERE name = '$pkg'")
270 mkdir -p ${cache}/wget && cd ${cache}/wget
271 # Download
272 download "$pkg" "${mirror}/${pkg}-${vers}.sup"
273 install_sup "${pkg}-${vers}.sup"
274 rm -rf ${cache}/wget && exit 0
275 fi
276 gettext "Can't find sup package:"; colorize 31 " $pkg" ;;
278 -u|update)
279 # Update packages
280 online=""
281 newline
282 colorize 33 $(gettext "SUP Update")
283 separator
285 # Skip HTTP/1.1 404 Not Found: 2>/dev/null
286 if ! wget -q -T 5 --spider ${mirror}/packages.sql 2>/dev/null; then
287 gettext "Mirror is unreachable"; newline
288 else
289 online=0
290 fi
292 # Newer packages.sql ?
293 gettext "Checking packages.sql..."
294 db_sum=$(wget -q "${server}?sup=dbsum" -O -)
295 if echo "$db_sum $pkgsdb" | md5sum -s -c -; then
296 info 036 "synced"
297 else
298 [ "$online" ] && rm -f ${pkgsdb}
299 info 035 "update"
300 fi
302 # --force
303 [ "$force" ] && rm -f ${pkgsdb}
305 # No packages.sql: get latest!
306 if [ ! -f "$pkgsdb" ] && [ "$online" ]; then
308 download "db" "${mirror}/packages.sql" "${supdb}"
310 timestamp=$(sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
311 | awk '{printf $1 " " $2}')
312 gettext "Database timestamp:"
313 indent $(($(tty_size) - 23)) "[ $(colorize 30 $timestamp) ]"
314 log "$(gettext 'Updated packages database:') $timestamp"
315 fi
317 # Exit now if no packages.sql
318 if [ ! -f "$pkgsdb" ]; then
319 gettext "Please try later. Missing packages database"; echo; exit 1
320 fi
322 # Scan installed packages
323 count="$(ls $installed | wc -l)"
324 char="$(echo $count | wc -L)"
325 in=$((7 + ${char}))
327 gettext "Installed packages:"
328 info 033 "$count"; newline
329 echo -n $(boldify $(gettext "Package") $(indent 30 $(gettext "Version")))
330 info 40 $(gettext "Status")
331 separator "-"
333 for pkg in $(ls $installed)
334 do
335 . ${installed}/${pkg}/receip
337 name=$(sqlite3 ${pkgsdb} \
338 "SELECT name FROM pkgs WHERE name = '$pkg'")
339 if [ ! "$name" ]; then
340 #echo -n "$pkg $(indent 30 $VERSION)"
341 #info 036 "$(gettext "local")"
342 continue
343 else
344 db_sum=$(sqlite3 ${pkgsdb} \
345 "SELECT md5sum FROM pkgs WHERE name = '$pkg'")
347 # MD5sum not matching: new version our new build ?
348 if [ "$md5_sum" != "$db_sum" ]; then
349 echo -n "$pkg $(indent 30 $VERSION)"
350 vers=$(sqlite3 ${pkgsdb} \
351 "SELECT version FROM pkgs WHERE name = '$pkg'")
353 if [ "$VERSION" != "$vers" ]; then
354 info 032 "$(gettext "$vers")"
355 else
356 info 035 "$(gettext "rebuilt")"
357 fi
358 echo "$pkg" >> ${cache}/updates
359 fi
360 fi
361 unset name db_sum vers
362 done
364 if [ -f "$cache/updates" ]; then
365 count=$(cat $cache/updates | wc -l)
366 separator "-"
367 gettext "Available updates:"; colorize 32 " $count"
368 newline
369 # --install
370 if [ "$install" ]; then
371 for pkg in $(cat $cache/updates); do
372 sup -i ${pkg}
373 done
374 fi
375 else
376 gettext "Packages are up-to-date"; echo
377 newline
378 fi
379 rm -f ${cache}/updates ;;
381 -r|remove)
382 # Remove a package
383 pkg="$2"
384 check_pkg_arg "$1"
385 if [ -d "$installed/$pkg" ]; then
386 remove_sup "$pkg"
387 else
388 gettext "Can't find installed package:"; colorize 31 " $pkg"
389 exit 1
390 fi ;;
392 -s|search)
393 # TODO: better :-)
394 query="$2"
395 newline
396 echo -n "$(colorize 33 $(gettext 'Searching for:'))"
397 boldify " $query"
398 separator
399 sqlite3 ${pkgsdb} << EOT
400 .headers on
401 .mode column
402 SELECT name, version, short_desc FROM pkgs WHERE name = '$2';
403 EOT
404 newline ;;
406 -l|list)
407 # only --db
408 if [ "$db" ]; then
409 newline
410 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
411 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
412 separator
413 sqlite3 ${pkgsdb} << EOT
414 .mode column
415 SELECT name, short_desc FROM pkgs;
416 EOT
417 newline; exit 0
418 fi
420 installed_nb=$(ls ${installed} | wc -l)
421 newline
422 echo -n "$(colorize 36 $(gettext 'Installed SUP packages:'))"
423 boldify " $installed_nb"
424 separator
425 for pkg in $(ls ${installed}); do
426 . ${installed}/${pkg}/receip
427 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
428 done
429 if [ "$installed_nb" == 0 ]; then
430 gettext "No sup packages installed"; echo
431 fi
432 newline
433 if [ -d "$wok" ]; then
434 echo -n "$(colorize 35 $(gettext 'SUP packages in your wok:'))"
435 boldify " $(ls ${wok} | wc -l)"
436 separator
437 for pkg in $(ls ${wok}); do
438 . ${wok}/${pkg}/receip
439 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
440 done
441 newline
442 fi
443 if [ -d "$cooked" ]; then
444 echo -n "$(colorize 33 $(gettext 'Cooked packages:'))"
445 boldify " $(ls $cooked | wc -l)"
446 fi
447 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
448 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
449 newline ;;
451 -d|debug)
452 # Packaging environment and debugger
453 newline
454 echo "$(colorize 35 $(gettext "Cooked packages:")) $cooked"
455 separator "-"
456 ls -1 ${cooked}; newline
457 echo "$(colorize 35 $(gettext "Downloads::")) $downloads"
458 separator "-"
459 ls -1 ${downloads}; newline ;;
461 ""|-h|help) help ;;
463 *|info)
464 # Display pkg info if installed
465 [ "$1" == "info" ] && shift
466 pkg="$1"
467 if [ -f "$installed/$pkg/receip" ]; then
468 . ${installed}/${pkg}/receip
469 newline
470 gettext "Installed package:"; colorize 36 " $PACKAGE $VERSION"
471 separator
472 echo "$SHORT_DESC"
473 newline
474 if [ "$list" ]; then
475 colorize 036 "$(gettext 'Files list:')"
476 cat ${installed}/${pkg}/files.list
477 separator "-"
478 fi
479 echo -n $(colorize 036 "$(gettext 'Installed size:')")
480 info 033 "$sup_size"
481 echo -n $(colorize 036 "$(gettext 'Installed files:')")
482 info 033 $(cat ${installed}/${pkg}/files.list | wc -l)
483 newline
484 else
485 gettext "Can't find installed package:"; colorize 31 " $pkg"
486 fi ;;
488 esac && exit 0