Magellan Linux

Contents of /branches/mage-sql/usr/lib/mage/sql_functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1121 - (show annotations) (download) (as text)
Thu Jul 22 07:47:49 2010 UTC (13 years, 9 months ago) by niro
File MIME type: application/x-sh
File size: 25910 byte(s)
-added fullpname2p{cat,name,ver,build}() functions as sql compat equivalent of magename2*() functions
-fixed is_higher logic -> some packages uses a zero in front of a subpver and this is not handled as a integer (1.08 -> 08)
-fixed all_{s,}depends() functions, using the ones from sqlwalker globally and deleted them in sqlwalker
-fixed fetch_packages(), compat md5sum_packages() and unpack_packages() functions to work with sqlwalker output
-added repository support to mage_install()

1 #!/bin/bash
2
3 die()
4 {
5 echo -e "Exited ${BASH_SOURCE} at line no ${BASH_LINENO}."
6 echo -e "$@"
7 exit 1
8 }
9
10 sql()
11 {
12 local sqlcmd="$*"
13 [[ -z ${sqlcmd} ]] && die "no sqlcmd given."
14
15 sqlite3 -nullvalue 'NULL' -list -separator '|' "${DBFILE}" << EOF || die "error running '$@'"
16 ${sqlcmd};
17 EOF
18 }
19
20 mage_setup()
21 {
22 [ ! -d ${MROOT}${INSTALLDB} ] && install -d ${MROOT}${INSTALLDB}
23 [ ! -d ${MROOT}${INSTALLDB}/records ] && install -d ${MROOT}${INSTALLDB}/records
24 # [ ! -f ${MROOT}${VIRTUALDB_FILE} ] && touch ${MROOT}${VIRTUALDB_FILE}
25 [ ! -d ${PKGDIR} ] && install -d ${PKGDIR}
26 [ ! -d ${BUILDDIR} ] && install -d ${BUILDDIR}
27 [ ! -d ${MAGEDIR} ] && install -d ${MAGEDIR}
28
29 return 0
30 }
31
32 dep2pver()
33 {
34 local pver
35
36 pver="${1##*/}"
37
38 # cut version only if not virtual or it will cut the name
39 if [[ $(dep2pcat $1) != virtual ]] && \
40 [[ $2 != virtual ]]
41 then
42 echo "${pver##*-}"
43 fi
44 }
45
46 # returns pname from fullname
47 # fullname2pver [fullname] -> PCAT/PNAME-PVER-PBUILD
48 fullname2pname()
49 {
50 local i pname
51
52 i="${1#*/}"
53 pname="${i%-*-*}"
54 echo "${pname}"
55 }
56
57 # returns pver from fullname
58 # fullname2pver [fullname] -> PCAT/PNAME-PVER-PBUILD
59 fullname2pver()
60 {
61 local i pver
62
63 i="${1#*/}"
64 i="${i/$(fullname2pname $1)-/}"
65 pver="${i%-*}"
66 echo "${pver}"
67 }
68
69 # returns pbuild from fullname
70 # fullname2pbuild [fullname] -> PCAT/PNAME-PVER-PBUILD
71 fullname2pbuild()
72 {
73 local i pbuild
74
75 i="$1"
76 pbuild="${i##*-}"
77 echo "${pbuild}"
78 }
79
80 # returns pcat from magename
81 # fullname2pcat [fullname] -> PCAT/PNAME-PVER-PBUILD
82 fullname2pcat()
83 {
84 local i pcat
85
86 i="${1%/*}"
87 # get basename
88 pcat="${i##*/}"
89 echo "${pcat}"
90 }
91
92
93 ## atoi string
94 atoi()
95 {
96 local pver="$1"
97 local len
98 local value
99 local finalpver
100
101 # first run a basic char to int on argv
102 len=${#pver}
103 for ((x=0; x < len; x++))
104 do
105 value="${pver:${x}:1}"
106 case ${value} in
107 a) value=".1.";;
108 b) value=".2.";;
109 c) value=".3.";;
110 d) value=".4.";;
111 e) value=".5.";;
112 f) value=".6.";;
113 g) value=".7.";;
114 h) value=".8.";;
115 i) value=".9.";;
116 j) value=".10.";;
117 k) value=".11.";;
118 l) value=".12.";;
119 m) value=".13.";;
120 n) value=".14.";;
121 o) value=".15.";;
122 p) value=".16.";;
123 q) value=".17.";;
124 r) value=".18.";;
125 s) value=".19.";;
126 t) value=".20.";;
127 u) value=".21.";;
128 v) value=".22.";;
129 w) value=".23.";;
130 x) value=".24.";;
131 y) value=".25.";;
132 z) value=".26.";;
133 _|-|\|) value=".0.";;
134 esac
135
136 # save new var
137 finalpver="${finalpver}${value}"
138 done
139 echo ${finalpver}
140 }
141
142 ## max_len string1 string2
143 max_len()
144 {
145 local str1="$1"
146 local str2="$2"
147 local len1
148 local len2
149
150 # substitute spaces
151 str1=$(echo "${str1}" | sed "s:\ ::g")
152 str2=$(echo "${str2}" | sed "s:\ ::g")
153
154 # bash returns lenghts always +1
155 len1=$(( ${#str1} -1 ))
156 len2=$(( ${#str2} -1 ))
157
158 if [[ ${len1} -ge ${len2} ]]
159 then
160 echo "${len1}"
161 else
162 echo "${len2}"
163 fi
164 }
165
166 is_higher()
167 {
168 local pver1="$1"
169 local pver2="$2"
170 local fpver
171 local fpver
172 local arr1
173 local arr2
174 local maxarrlen
175 local i
176 local x
177
178 # build to arrays with fixed pvers
179 fpver1=($(atoi ${pver1} | sed "s:\.:\ :g"))
180 fpver2=($(atoi ${pver2} | sed "s:\.:\ :g"))
181
182 maxarrlen=$(max_len "${fpver1[*]}" "${fpver2[*]}")
183
184 # now fill the rest with ZEROS
185 x=${#fpver1[*]}
186 if [[ ${x} -lt ${maxarrlen} ]]
187 then
188 for (( i=x; i<maxarrlen;i++))
189 do
190 fpver1[${i}]="0"
191 done
192 fi
193
194 # now fill the rest with ZEROS
195 x=${#fpver2[*]}
196 if [[ ${x} -lt ${maxarrlen} ]]
197 then
198 for (( i=x; i<maxarrlen;i++))
199 do
200 fpver2[${i}]="0"
201 done
202 fi
203
204 # now compare the to arrays
205 for ((i=0; i < ${maxarrlen}; i++))
206 do
207 #if [[ ${fpver1[${i}]} -ne ${fpver2[${i}]} ]]
208 if [[ ${fpver1[${i}]} != ${fpver2[${i}]} ]]
209 then
210 #if [[ ${fpver1[${i}]} -gt ${fpver2[${i}]} ]]
211 if [[ ${fpver1[${i}]} > ${fpver2[${i}]} ]]
212 then
213 echo "${pver1}"
214 return
215 else
216 echo "${pver2}"
217 return
218 fi
219 fi
220 done
221
222 # both values are the same, so print the first
223 echo "${pver1}"
224 }
225
226 highest_pkg()
227 {
228 local pname="$1"
229 local state="$2"
230 local retval
231
232 case ${state} in
233 stable) search="state='stable'" ;;
234 testing) search="state='stable' or state='testing'" ;;
235 unstable) search="state='stable' or state='testing' or state='unstable'" ;;
236 esac
237
238 for current in $(sql "select pver,pbuild from packages where pname='${pname}' and (${search})")
239 do
240 [[ x -eq 0 ]] && max="${current}"
241 max="$(is_higher ${current} ${max})"
242 ((x++))
243 done
244
245 echo "${max}"
246 }
247
248 all_depends()
249 {
250 local pname="$1"
251 local pver="$2"
252 local pbuild="$3"
253
254 sql "select categories.pcat,
255 depends.pname,
256 depends.pver,
257 depends.pbuild
258 from depends
259 inner join packages
260 on depends.pkg_id=packages.id
261 and packages.pname='${pname}'
262 and packages.pver='${pver}'
263 and packages.pbuild='${pbuild}'
264 inner join categories
265 on depends.pcat_id=categories.id"
266 }
267
268 all_sdepends()
269 {
270 local pname="$1"
271 local pver="$2"
272 local pbuild="$3"
273
274 sql "select categories.pcat,
275 sdepends.pname,
276 sdepends.pver,
277 sdepends.pbuild
278 from sdepends
279 inner join packages
280 on sdepends.pkg_id=packages.id
281 and packages.pname='${pname}'
282 and packages.pver='${pver}'
283 and packages.pbuild='${pbuild}'
284 inner join categories
285 on sdepends.pcat_id=categories.id"
286 }
287
288 default_virtualname_to_pkgname()
289 {
290 local vcat="$1"
291 local vname="$2"
292
293 sql "select pcat,pname from virtual_defaults where vcat='${vcat}' and vname='${vname}'"
294 }
295
296 # fetch_packages /path/to/mage/file1 /path/to/mage/file2
297 fetch_packages()
298 {
299 local list=( $@ )
300 local pkg
301 local mirr
302 local item
303 local md5file
304 local opt
305 local count_current
306 local count_total
307 local pname
308 local pver
309 local pbuild
310
311 [[ -z ${MIRRORS} ]] && die "You have no mirrors defined. Please edit your ${MAGERC}."
312
313 # get count of total packages
314 declare -i count_current=0
315 declare -i count_total=0
316
317 count_total="${#list[*]}"
318
319 for item in ${list[*]}
320 do
321 pname="$(fullname2pname ${item})"
322 pver="$(fullname2pver ${item})"
323 pbuild="$(fullname2pbuild ${item})"
324 pkg="${pname}-${pver}-${ARCH}-${pbuild}.${PKGSUFFIX}"
325
326 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
327
328 (( count_current++ ))
329 xtitle "[ (${count_current}/${count_total}) Fetching ${pkg} ]"
330
331 # abort on virtual pkg
332 if [[ ${pkgtype} = virtual ]]
333 then
334 echo -ne " ${COLBLUE}---${COLDEFAULT}"
335 echo " !fetch virtual (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
336 continue
337 fi
338
339 # abort on sources pkg
340 if [[ ${pkgtype} = sources ]]
341 then
342 echo -ne " ${COLBLUE}---${COLDEFAULT}"
343 echo " !fetch sources (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
344 continue
345 fi
346
347 # abort if already exist
348 if [ -f ${PKGDIR}/${pkg} ]
349 then
350 echo -ne " ${COLBLUE}***${COLDEFAULT}"
351 echo " fetch complete (${count_current}/${count_total}): ${pkg} ... "
352 continue
353 fi
354
355 for mirr in ${MIRRORS}
356 do
357 echo -ne " ${COLBLUE}***${COLDEFAULT}"
358 #echo -e " fetching (${count_current}/${count_total}): ${mirr}/${pkg} ... "
359 echo -e " fetching (${count_current}/${count_total}): ${pkg} ... "
360 [[ ${VERBOSE} = off ]] && opt="--quiet"
361 wget \
362 --passive-ftp \
363 --tries 3 \
364 --continue \
365 --progress bar \
366 --directory-prefix=${PKGDIR} \
367 ${opt} ${mirr}/${PACKAGES_SERVER_PATH}/${pkg}
368 if [[ $? = 0 ]]
369 then
370 break
371 else
372 continue
373 fi
374 done
375
376 if [ ! -f ${PKGDIR}/${pkg} ]
377 then
378 die "Could not download ${pkg}"
379 fi
380 done
381
382 # add a crlf for a better view
383 if [ ${count_total} -gt 1 ]; then echo; fi
384 }
385
386 md5sum_packages()
387 {
388 local list=( $@ )
389 local item
390 local pcat
391 local pname
392 local pkgname
393 local pkgfile
394 local pkgtype
395 local count_current
396 local count_total
397 local pver
398 local pbuild
399
400 # get count of total packages
401 declare -i count_current=0
402 declare -i count_total=0
403
404 count_total="${#list[*]}"
405
406 for item in ${list[*]}
407 do
408 pcat=${item%%/*}
409 pname=$(fullname2pname ${item})
410 pver=$(fullname2pver ${item})
411 pbuild=$(fullname2pbuild ${item})
412
413 pkgname="${pname}-${pver}-${ARCH}-${pbuild}"
414 pkgfile="${pkgname}.${PKGSUFFIX}"
415 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
416 md5=$(sql "select packages_info.md5 from packages_info inner join packages on packages_info.pkg_id=packages.id where packages.pname='${pname}' and packages.pver='${pver}' and packages.pbuild='${pbuild}' and arch='${ARCH}'")
417
418 (( count_current++ ))
419 xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
420
421 # abort on virtual pkg
422 if [[ ${pkgtype} = virtual ]]
423 then
424 echo -ne " ${COLBLUE}---${COLDEFAULT}"
425 echo " !md5sum virtual (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
426 continue
427 fi
428
429 # abort on sources pkg
430 if [[ ${pkgtype} = sources ]]
431 then
432 echo -ne " ${COLBLUE}---${COLDEFAULT}"
433 echo " !md5sum sources (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
434 continue
435 fi
436
437 if [[ ! -z ${md5} ]]
438 then
439 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
440 echo -ne "checking md5sum (${count_current}/${count_total}): "
441 ( cd ${PKGDIR}; md5sum --check << EOF ;)|| die "md5 for ${pkgfile} failed"
442 ${md5} ${pkgfile}
443 EOF
444 else
445 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
446 echo -e "!! no md5sum file found for ${pkgfile} :("
447 fi
448 done
449
450 # add a crlf for a better view
451 if [ ${count_total} -gt 1 ]; then echo; fi
452 }
453
454 unpack_packages()
455 {
456 local list=( $@ )
457 local item
458 local pkg
459 local pkgtype
460 local count_current
461 local count_total
462
463 # get count of total packages
464 declare -i count_current=0
465 declare -i count_total=0
466
467 count_total="${#list[*]}"
468
469 for item in ${list[*]}
470 do
471 pname=$(fullname2pname ${item})
472 pver=$(fullname2pver ${item})
473 pbuild=$(fullname2pbuild ${item})
474 pkg="${pname}-${pver}-${ARCH}-${pbuild}.${PKGSUFFIX}"
475 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
476
477 (( count_current++ ))
478 xtitle "[ (${count_current}/${count_total}) Unpacking ${pkg} ]"
479
480 # abort on virtual pkg
481 if [[ ${pkgtype} = virtual ]]
482 then
483 echo -ne " ${COLBLUE}---${COLDEFAULT}"
484 echo " !unpack virtual (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
485 continue
486 fi
487
488 # abort on sources pkg
489 if [[ ${pkgtype} = sources ]]
490 then
491 echo -ne " ${COLBLUE}---${COLDEFAULT}"
492 echo " !unpack sources (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
493 continue
494 fi
495
496 echo -e " ${COLBLUE}***${COLDEFAULT} unpacking (${count_current}/${count_total}): ${pkg} ... "
497 tar xjmf ${PKGDIR}/${pkg} -C ${BUILDDIR} || die "Unpacking package ${pkg}"
498 done
499
500 # add a crlf for a better view
501 if [ ${count_total} -gt 1 ]; then echo; fi
502 }
503
504 mage_install()
505 {
506 # local all possible vars of a mage file
507 # to prevent bad issues
508 local PKGNAME
509 local STATE
510 local DESCRIPTION
511 local HOMEPAGE
512 local DEPEND
513 local SDEPEND
514 local PROVIDE
515 local PKGTYPE
516 local preinstall
517 local postinstall
518 local preremove
519 local postremove
520
521 local pcat
522 local pname
523 local pver
524 local pbuild
525 local count_total
526 local count_current
527 local magefile
528 local src_install
529
530 # very basic getops
531 for i in $*
532 do
533 case $1 in
534 --pcat|-c) shift; pcat="$1" ;;
535 --pname|-n) shift; pname="$1" ;;
536 --pver|-v) shift; pver="$1" ;;
537 --pbuild|-b) shift; pbuild="$1" ;;
538 --count-total) shift; count_total="$1" ;;
539 --count-current) shift; count_current="$1" ;;
540 --src-install|-s) shift; src_install=true ;;
541 esac
542 shift
543 done
544
545 # sanity checks; abort if not given
546 [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
547 [ -z "${pname}" ] && die "mage_install() \$pname not given."
548 [ -z "${pver}" ] && die "mage_install() \$pver not given."
549 [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
550
551 # check needed global vars
552 [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
553 [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
554 [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
555
556 xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
557 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
558 echo -n "installing (${count_current}/${count_total}): "
559 echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
560 echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
561
562 # magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
563 # source ${magefile}
564 PKGNAME="${pname}-${pver}-${ARCH}-${pbuild}"
565
566 # abort on sources if no srcinstall
567 if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
568 then
569 echo
570 echo -e "This Package is a Source Package."
571 echo
572 echo -e "Only 'srcinstall' works with this type of packages"
573 echo -en "If you have done a srcinstall before, "
574 echo -e "you will find the files in /usr/src."
575 echo
576 exit 1
577 fi
578
579 ## preinstall scripts
580 if [ -n "$(typeset -f preinstall)" ]
581 then
582 echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
583 preinstall
584 unset preinstall
585 fi
586
587 if [[ ${src_install} = true ]]
588 then
589 local smage2file
590 local repository
591 # check needed global vars
592 [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
593 [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
594 [ -z "${BINDIR}" ] && die "\$BINDIR not set."
595
596 # build the package first
597 if [[ ${MAGEDEBUG} = on ]]
598 then
599 echo M:${pname}
600 echo V:${pver}
601 echo B:${pbuild}
602 fi
603 repository=$(sql "select repositories.name from repositories
604 inner join packages
605 on packages.repo_id=repositories.id
606 where packages.pname='${pname}'
607 and packages.pver='${pver}'
608 and packages.pbuild='${pbuild}';")
609 smage2file=${SMAGESCRIPTSDIR}/${repository}/${pname}/${pname}-${pver}-${pbuild}.smage2
610 if [ -f "${smage2file}" ]
611 then
612 echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
613 smage2 ${smage2file} || die "compile failed"
614 else
615 echo
616 echo "$(basename ${SMAGEFILE}) not found."
617 echo "update your smage-tree and try it again."
618 echo
619 die
620 fi
621 fi
622
623 if [[ ${PKGTYPE} != virtual ]] && \
624 [[ ${PKGTYPE} != sources ]]
625 then
626 echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
627 build_doinstall ${PKGNAME}
628 fi
629
630 ## postinstall scripts
631 if [ -n "$(typeset -f postinstall)" ]
632 then
633 echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
634 postinstall
635 unset postinstall
636 fi
637
638 # install a database entry
639 install_database_entry \
640 --pcat "${pcat}" \
641 --pname "${pname}" \
642 --pver "${pver}" \
643 --pbuild "${pbuild}" \
644 --pkgname "${PKGNAME}" \
645 --pkgtype "${PKGTYPE}" \
646 || die "error in mage_install() running install_database_entry()."
647
648 # remove the package dir now
649 if [ -d ${BUILDDIR}/${PKGNAME} ]
650 then
651 rm -rf ${BUILDDIR}/${PKGNAME}
652 fi
653
654 # rebuilds toplevel info node
655 if [[ ${MAGE_INFO_REBUILD} = true ]]
656 then
657 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
658 echo -n "rebuilding top-level info node ... "
659 ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
660 > ${MROOT}/usr/share/info/dir && \
661 echo "done." || echo "failure."
662 unset MAGE_INFO_REBUILD
663 fi
664
665 # rebuilds the enviroment with the content of /etc/env.d
666 if [[ ${MAGE_ENV_REBUILD} = true ]]
667 then
668 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
669 echo -n "rebuilding environment ... "
670 ${MLIBDIR}/env-rebuild.sh > /dev/null && \
671 echo "done." || echo "failure."
672 unset MAGE_ENV_REBUILD
673 fi
674
675 xtitleclean
676
677 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
678 echo -n "package "
679 # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
680 # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
681 echo "successfully installed."
682
683 # unset these functions
684 unset -f preinstall
685 unset -f postinstall
686 unset -f preremove
687 unset -f postremove
688 }
689
690 install_packages()
691 {
692 local list="$@"
693 local pkg
694 local pcat
695 local pname
696 local pver
697 local pbuild
698 local total_pkgs
699 local current_pkg
700 local src_install
701 local uninstall_list
702
703 # check for --src-install
704 if [[ $1 = --src-install ]]
705 then
706 # remove --src-install from list
707 list=${list/--src-install/}
708 # enable src-install
709 src_install="--src-install"
710 fi
711
712 # reset MAGE_PROTECT_COUNTER
713 declare -i MAGE_PROTECT_COUNTER=0
714 export MAGE_PROTECT_COUNTER
715
716 # get count of total packages
717 declare -i total_pkgs=0
718 declare -i current_pkg=0
719 for i in ${list}; do (( total_pkgs++ )); done
720
721 echo
722
723 if [[ -n ${MROOT} ]]
724 then
725 echo -ne ${COLRED}
726 echo "!! installing in MROOT=${MROOT}"
727 echo -ne ${COLDEFAULT}
728 echo
729 fi
730
731 for pkg in ${list}
732 do
733 (( current_pkg++ ))
734 #pcat=$(magename2pcat ${pkg})
735 pcat="${pkg%%/*}"
736 pname=$(magename2pname ${pkg})
737 pver=$(magename2pver ${pkg})
738 pbuild=$(magename2pbuild ${pkg})
739
740 mage_install \
741 --pcat ${pcat} \
742 --pname ${pname} \
743 --pver ${pver} \
744 --pbuild ${pbuild} \
745 --count-total ${total_pkgs} \
746 --count-current ${current_pkg} \
747 ${src_install}
748
749 # check for allready installed packages and remove them
750 # except the package we have installed
751 uninstall_list="$(get_uninstall_candidates \
752 --pcat "${pcat}" \
753 --pname "${pname}" \
754 --protected ${pcat}/${pname}-${pver}-${pbuild})"
755
756 # uninstall all packges in uninstall_list if not empty
757 if [ -n "${uninstall_list}" ]
758 then
759 echo
760 uninstall_packages ${uninstall_list} \
761 || die "install_packges() uninstalling not-needed."
762 fi
763
764 # crlf for better view in VERBOSE mode
765 #if [[ ${VERBOSE} = on ]]; then echo; fi
766 echo
767 done
768
769 #echo "DEBUG MAGE_PROTECT_COUNTER=${MAGE_PROTECT_COUNTER}"
770 show_etc_update_mesg
771 }
772
773 uninstall_packages()
774 {
775 local list="$@"
776 local pcat
777 local pname
778 local pver
779 local pbuild
780 local can_pcat
781 local can_pname
782 local can_ver_list
783
784 if [[ -n ${MROOT} ]]
785 then
786 echo -ne ${COLRED}
787 echo "!! uninstalling from MROOT=${MROOT}"
788 echo -ne ${COLDEFAULT}
789 echo
790 fi
791
792 # generate a candidates list
793 for pkg in ${list}
794 do
795 #pcat=$(dep2pcat ${pkg})
796 pcat="${pkg%%/*}"
797 pname=$(magename2pname ${pkg})
798 pver=$(magename2pver ${pkg})
799 pbuild=$(magename2pbuild ${pkg})
800 can_pcat="${pcat}"
801 can_pname="${pname}"
802
803 if [ -z "${can_ver_list}" ]
804 then
805 can_ver_list=" ${pver}-${pbuild}"
806 else
807 can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
808 fi
809 done
810
811 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
812 echo "following candidate(s) will be removed:"
813 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
814 echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
815 echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
816 echo
817 if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
818 then
819 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
820 echo "( Press [CTRL+C] to abort )"
821 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
822 echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
823 for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
824 do
825 echo -ne "${COLRED} ${i}${COLDEFAULT}"
826 sleep 1
827 done
828 echo
829 echo
830 fi
831
832 for pkg in ${list}
833 do
834 pcat=$(dep2pcat ${pkg})
835 pname=$(magename2pname ${pkg})
836 pver=$(magename2pver ${pkg})
837 pbuild=$(magename2pbuild ${pkg})
838
839 mage_uninstall \
840 --pcat ${pcat} \
841 --pname ${pname} \
842 --pver ${pver} \
843 --pbuild ${pbuild} \
844 --count-total ${total_pkgs} \
845 --count-current ${current_pkg} \
846 ${src_install}
847
848 # crlf for better view in VERBOSE mode
849 #if [[ ${VERBOSE} = on ]]; then echo; fi
850 echo
851 done
852 }
853
854 is_installed()
855 {
856 local DBFILE
857 local pkgid
858 local pcat
859 local pname
860 local pbuild
861 local pver
862
863 # very basic getops
864 for i in $*
865 do
866 case $1 in
867 --pcat|-c) shift; pcat="$1" ;;
868 --pname|-n) shift; pname="$1" ;;
869 --pver|-v) shift; pver="$1" ;;
870 --pbuild|-b) shift; pbuild="$1" ;;
871 esac
872 shift
873 done
874
875 DBFILE="${MAGE_INSTALL_DB}"
876 pkgid=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
877
878 [[ ! -z ${pkgid} ]] && return 0
879 return 1
880 }
881
882 reverse_depends()
883 {
884 local DBFILE
885 local pname="$1"
886
887 DBFILE="${MAGE_INSTALL_DB}"
888 sql "select categories.pcat,
889 packages.pname,
890 packages.pver,
891 packages.pbuild
892 from depends
893 inner join packages on packages.id=depends.pkg_id
894 inner join categories on categories.id=packages.pcat_id
895 where depends.pname='${pname}'"
896 }
897
898 reverse_sdepends()
899 {
900 local DBFILE
901 local pname="$1"
902
903 DBFILE="${MAGE_INSTALL_DB}"
904 sql "select categories.pcat,
905 packages.pname,
906 packages.pver,
907 packages.pbuild
908 from sdepends
909 inner join packages on packages.id=sdepends.pkg_id
910 inner join categories on categories.id=packages.pcat_id
911 where sdepends.pname='${pname}'"
912 }
913
914 create_database_layout()
915 {
916 sql "create table categories(id integer primary key, pcat text unique)"
917 sql "create table packages(id integer primary key,
918 pname text,
919 pver text,
920 pbuild text,
921 pcat_id numeric,
922 state text,
923 provide text,
924 pkgtype text
925 )"
926 sql "create table depends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
927 sql "create table sdepends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
928 sql "create table packages_info(id integer primary key, pkg_id numeric, arch text, md5 text, mtime numeric, homepage text, description text, size numeric)"
929 #sql "create table packages_content(id integer primary key, pkginfo_id numeric, char text, dirs text, files text, pipes text, symlinks text)"
930 sql "create table packages_content(id integer primary key, pkginfo_id numeric, char blob, dirs blob, files blob, pipes blob, symlinks blob)"
931 }
932
933 install_database_entry()
934 {
935 local pcat
936 local pname
937 local pver
938 local pbuild
939 local pkgtype
940 local pkgname
941 local magefile
942 local dbrecorddir
943 local provide
944 local homepage
945 local description
946 local pkgid
947 local depends
948 local sdepends
949 local relation
950 local depcatid
951 local depname
952 local depver
953 local depbuild
954 local mage_pkg_id
955 local i
956 local install_pkg_id
957 local table
958
959 # very basic getops
960 for i in $*
961 do
962 case $1 in
963 --pcat|-c) shift; pcat="$1" ;;
964 --pname|-n) shift; pname="$1" ;;
965 --pver|-v) shift; pver="$1" ;;
966 --pbuild|-b) shift; pbuild="$1" ;;
967 --pkgname|-a) shift; pkgname="$1" ;;
968 # --state|-s) shift; state="$1" ;;
969 --pkgtype|-t) shift; pkgtype="$1" ;;
970 esac
971 shift
972 done
973
974 # sanity checks; abort if not given
975 [[ -z ${pcat} ]] && die "install_database_entry() \$pcat not given."
976 [[ -z ${pname} ]] && die "install_database_entry() \$pname not given."
977 [[ -z ${pver} ]] && die "install_database_entry() \$pver not given."
978 [[ -z ${pbuild} ]] && die "install_database_entry() \$pbuild not given."
979 [[ -z ${pkgname} ]] && die "install_database_entry() \$pkgname not given."
980 # [[ -z ${state} ]] && die "install_database_entry() \$state not given."
981
982 # check needed global vars
983 [[ -z ${MAGEDIR} ]] && die "install_database_entry() \$MAGEDIR not set."
984 [[ -z ${INSTALLDB} ]] && die "install_database_entry() \$INSTALLDB not set."
985
986 # first of all convert pbuild to database format (remove the r)
987 pbuild="${pbuild}"
988
989 # check if
990 # copy all things over over
991 # mage.pkg_id
992 DBFILE="${MAGE_PACKAGES_DB}"
993 mage_pkg_id=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
994
995 DBFILE="${MAGE_INSTALL_DB}"
996 # add pcat to installdb if missing
997 if [[ -z $(sql "select id from categories where pcat='${pcat}'") ]]
998 then
999 sql "insert into categories (pcat) values ('${pcat}')"
1000 fi
1001
1002 # attach install database
1003 # install.categories.id != main.categories.id !!!
1004 # so get pcat from pcat_id via inner join.
1005 # then query install db which id pcat has and use this one
1006 # final phase copies the whole crap to the install db
1007 # use some more rows to copy the whole table
1008 sqlite3 ${MAGE_PACKAGES_DB} \
1009 "attach database '${MAGE_INSTALL_DB}' as install;
1010 insert into install.packages
1011 (pcat_id,
1012 pname,
1013 pver,
1014 pbuild,
1015 state,
1016 provide,
1017 pkgtype)
1018 select mage_id,
1019 main.packages.pname,
1020 main.packages.pver,
1021 main.packages.pbuild,
1022 main.packages.state,
1023 main.packages.provide,
1024 main.packages.pkgtype
1025 from main.packages,
1026 (select id as mage_id
1027 from install.categories where pcat=(
1028 select pcat from main.packages
1029 inner join main.categories
1030 on main.categories.id=main.packages.pcat_id
1031 where main.packages.id='${mage_pkg_id}')
1032 )
1033 where main.packages.id=${mage_pkg_id};"
1034
1035 # get install db pkg_id
1036 install_pkg_id=$(sql "select max(id) from packages")
1037
1038 # now copy dependencies
1039 # replace pkg with the one from insatll.db
1040 for table in depends sdepends
1041 do
1042 sqlite3 ${MAGE_PACKAGES_DB} \
1043 "attach database '${MAGE_INSTALL_DB}' as install;
1044 insert into install.${table}
1045 (pkg_id,
1046 relation,
1047 pcat_id,
1048 pname,
1049 pver,
1050 pbuild)
1051 select '${install_pkg_id}',
1052 relation,
1053 pcat_id,
1054 pname,
1055 pver,
1056 pbuild
1057 from main.${table} where pkg_id='${mage_pkg_id}'"
1058 done
1059
1060 # and now the package infos
1061 sqlite3 ${MAGE_PACKAGES_DB} \
1062 "attach database '${MAGE_INSTALL_DB}' as install;
1063 insert into install.packages_info
1064 (pkg_id,
1065 arch,
1066 md5,
1067 mtime,
1068 homepage,
1069 description,
1070 size)
1071 select '${install_pkg_id}',
1072 arch,
1073 md5,
1074 mtime,
1075 homepage,
1076 description,
1077 size
1078 from main.packages_info
1079 where pkg_id='${mage_pkg_id}' and arch='${ARCH}'"
1080
1081 # now install PKGTYPE specific files
1082 case ${pkgtype} in
1083 virtual) echo ;;
1084 sources) echo ;;
1085 *)
1086 # and finally the package_content
1087 # but we first need to know the pkg_info id
1088 # because of the arch dependency
1089 pkg_info_id=$(sql "select max(id) from packages_info")
1090
1091 sql "insert into packages_content
1092 (pkginfo_id,
1093 char,
1094 dirs,
1095 files,
1096 pipes,
1097 symlinks)
1098 values('${pkg_info_id}',
1099 'records/${pkg_info_id}/char.bz2',
1100 'records/${pkg_info_id}/dirs.bz2',
1101 'records/${pkg_info_id}/files.bz2',
1102 'records/${pkg_info_id}/pipes.bz2',
1103 'records/${pkg_info_id}/symlinks.bz2')"
1104 # create compressed content files
1105 entrydir=${BUILDDIR}/${pname}-${pver}-${ARCH}-${pbuild}
1106 local entryfile
1107 for entryfile in char dirs files pipes symlinks
1108 do
1109 install -d $(dirname ${MAGE_INSTALL_DB})/records/${pkg_info_id}
1110 cat ${entrydir}/.${entryfile} | bzip2 -9 >> $(dirname ${MAGE_INSTALL_DB})/records/${pkg_info_id}/${entryfile}.bz2
1111 done
1112 ;;
1113 esac
1114 }