Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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