Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1115 - (hide annotations) (download) (as text)
Sat Jul 17 10:48:23 2010 UTC (13 years, 9 months ago) by niro
File MIME type: application/x-sh
File size: 25591 byte(s)
-dropped resolv_pkgs and start_depwanderer functions, using sqlwalker.sh now
1 niro 486 #!/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 niro 1114 fpver1=($(atoi ${pver1} | sed "s:\.:\ :g"))
133     fpver2=($(atoi ${pver2} | sed "s:\.:\ :g"))
134 niro 486
135 niro 1114 maxarrlen=$(max_len "${fpver1[*]}" "${fpver2[*]}")
136 niro 486
137     # now fill the rest with ZEROS
138 niro 1114 x=${#fpver1[*]}
139 niro 486 if [[ ${x} -lt ${maxarrlen} ]]
140     then
141     for (( i=x; i<maxarrlen;i++))
142     do
143 niro 1114 fpver1[${i}]="0"
144 niro 486 done
145     fi
146    
147     # now fill the rest with ZEROS
148 niro 1114 x=${#fpver2[*]}
149 niro 486 if [[ ${x} -lt ${maxarrlen} ]]
150     then
151     for (( i=x; i<maxarrlen;i++))
152     do
153 niro 1114 fpver2[${i}]="0"
154 niro 486 done
155     fi
156    
157     # now compare the to arrays
158     for ((i=0; i < ${maxarrlen}; i++))
159     do
160 niro 1114 if [[ ${fpver1[${i}]} -ne ${fpver2[${i}]} ]]
161 niro 486 then
162 niro 1114 if [[ ${fpver1[${i}]} -gt ${fpver2[${i}]} ]]
163 niro 486 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 niro 1114 highest_pkg()
178 niro 486 {
179     local pname="$1"
180     local state="$2"
181 niro 1114 local retval
182 niro 486
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 niro 1114 highest=$(highest_pkg "${pname}" "${state}")
210 niro 486 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 niro 1114 highest=$(highest_pkg "${pname}" "${state}")
238 niro 486 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 niro 1114 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 niro 486 # fetch_packages /path/to/mage/file1 /path/to/mage/file2
264     fetch_packages()
265     {
266     local list="$@"
267     local pkg
268     local mirr
269     local magefile
270     local md5file
271     local opt
272     local count_current
273     local count_total
274     local pname
275     local pver
276     local pbuild
277    
278     [ -z "${MIRRORS}" ] && die "You have no mirrors defined. Please edit your ${MAGERC}."
279    
280     # get count of total packages
281     declare -i count_current=0
282     declare -i count_total=0
283    
284     for i in ${list}; do (( count_total++ )); done
285    
286     for magefile in ${list}
287     do
288     #pkg="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}"
289    
290     pname="$(magename2pname ${magefile})"
291     pver="$(magename2pver ${magefile})"
292     pbuild="$(magename2pbuild ${magefile})"
293     pkg="${pname}-${pver}-${ARCH}-${pbuild}.${PKGSUFFIX}"
294    
295 niro 1114 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
296 niro 486
297     (( count_current++ ))
298     xtitle "[ (${count_current}/${count_total}) Fetching ${pkg} ]"
299    
300     # abort on virtual pkg
301     if [[ ${pkgtype} = virtual ]]
302     then
303     echo -ne " ${COLBLUE}---${COLDEFAULT}"
304     echo " !fetch virtual (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
305     continue
306     fi
307    
308     # abort on sources pkg
309     if [[ ${pkgtype} = sources ]]
310     then
311     echo -ne " ${COLBLUE}---${COLDEFAULT}"
312     echo " !fetch sources (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
313     continue
314     fi
315    
316     # abort if already exist
317     if [ -f ${PKGDIR}/${pkg} ]
318     then
319     echo -ne " ${COLBLUE}***${COLDEFAULT}"
320     echo " fetch complete (${count_current}/${count_total}): ${pkg} ... "
321     continue
322     fi
323    
324     for mirr in ${MIRRORS}
325     do
326     echo -ne " ${COLBLUE}***${COLDEFAULT}"
327     #echo -e " fetching (${count_current}/${count_total}): ${mirr}/${pkg} ... "
328     echo -e " fetching (${count_current}/${count_total}): ${pkg} ... "
329     [[ ${VERBOSE} = off ]] && opt="--quiet"
330     wget \
331     --passive-ftp \
332     --tries 3 \
333     --continue \
334     --progress bar \
335     --directory-prefix=${PKGDIR} \
336     ${opt} ${mirr}/${PACKAGES_SERVER_PATH}/${pkg}
337     if [[ $? = 0 ]]
338     then
339     break
340     else
341     continue
342     fi
343     done
344    
345     if [ ! -f ${PKGDIR}/${pkg} ]
346     then
347     die "Could not download ${pkg}"
348     fi
349     done
350    
351     # add a crlf for a better view
352     if [ ${count_total} -gt 1 ]; then echo; fi
353     }
354    
355     mage_install()
356     {
357     # local all possible vars of a mage file
358     # to prevent bad issues
359     local PKGNAME
360     local STATE
361     local DESCRIPTION
362     local HOMEPAGE
363     local DEPEND
364     local SDEPEND
365     local PROVIDE
366     local PKGTYPE
367     local preinstall
368     local postinstall
369     local preremove
370     local postremove
371    
372     local pcat
373     local pname
374     local pver
375     local pbuild
376     local count_total
377     local count_current
378     local magefile
379     local src_install
380    
381     # very basic getops
382     for i in $*
383     do
384     case $1 in
385     --pcat|-c) shift; pcat="$1" ;;
386     --pname|-n) shift; pname="$1" ;;
387     --pver|-v) shift; pver="$1" ;;
388     --pbuild|-b) shift; pbuild="$1" ;;
389     --count-total) shift; count_total="$1" ;;
390     --count-current) shift; count_current="$1" ;;
391     --src-install|-s) shift; src_install=true ;;
392     esac
393     shift
394     done
395    
396     # sanity checks; abort if not given
397     [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
398     [ -z "${pname}" ] && die "mage_install() \$pname not given."
399     [ -z "${pver}" ] && die "mage_install() \$pver not given."
400     [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
401    
402     # check needed global vars
403     [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
404     [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
405     [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
406    
407     xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
408     echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
409     echo -n "installing (${count_current}/${count_total}): "
410     echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
411     echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
412    
413     # magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
414     # source ${magefile}
415     PKGNAME="${pname}-${pver}-${ARCH}-${pbuild}"
416    
417     # abort on sources if no srcinstall
418     if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
419     then
420     echo
421     echo -e "This Package is a Source Package."
422     echo
423     echo -e "Only 'srcinstall' works with this type of packages"
424     echo -en "If you have done a srcinstall before, "
425     echo -e "you will find the files in /usr/src."
426     echo
427     exit 1
428     fi
429    
430     ## preinstall scripts
431     if [ -n "$(typeset -f preinstall)" ]
432     then
433     echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
434     preinstall
435     unset preinstall
436     fi
437    
438     if [[ ${src_install} = true ]]
439     then
440     local smage2file
441     # check needed global vars
442     [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
443     [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
444     [ -z "${BINDIR}" ] && die "\$BINDIR not set."
445    
446     # build the package first
447     if [[ ${MAGEDEBUG} = on ]]
448     then
449     echo M:${pname}
450     echo V:${pver}
451     echo B:${pbuild}
452     fi
453    
454     smage2file=${SMAGESCRIPTSDIR}/${pname}/${pname}-${pver}-${pbuild}.smage2
455     if [ -f "${smage2file}" ]
456     then
457     echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
458     smage2 ${smage2file} || die "compile failed"
459     else
460     echo
461     echo "$(basename ${SMAGEFILE}) not found."
462     echo "update your smage-tree and try it again."
463     echo
464     die
465     fi
466     fi
467    
468     if [[ ${PKGTYPE} != virtual ]] && \
469     [[ ${PKGTYPE} != sources ]]
470     then
471     echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
472     build_doinstall ${PKGNAME}
473     fi
474    
475     ## postinstall scripts
476     if [ -n "$(typeset -f postinstall)" ]
477     then
478     echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
479     postinstall
480     unset postinstall
481     fi
482    
483     # install a database entry
484     install_database_entry \
485     --pcat "${pcat}" \
486     --pname "${pname}" \
487     --pver "${pver}" \
488     --pbuild "${pbuild}" \
489     --pkgname "${PKGNAME}" \
490     --pkgtype "${PKGTYPE}" \
491     || die "error in mage_install() running install_database_entry()."
492    
493     # remove the package dir now
494     if [ -d ${BUILDDIR}/${PKGNAME} ]
495     then
496     rm -rf ${BUILDDIR}/${PKGNAME}
497     fi
498    
499     # rebuilds toplevel info node
500     if [[ ${MAGE_INFO_REBUILD} = true ]]
501     then
502     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
503     echo -n "rebuilding top-level info node ... "
504     ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
505     > ${MROOT}/usr/share/info/dir && \
506     echo "done." || echo "failure."
507     unset MAGE_INFO_REBUILD
508     fi
509    
510     # rebuilds the enviroment with the content of /etc/env.d
511     if [[ ${MAGE_ENV_REBUILD} = true ]]
512     then
513     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
514     echo -n "rebuilding environment ... "
515     ${MLIBDIR}/env-rebuild.sh > /dev/null && \
516     echo "done." || echo "failure."
517     unset MAGE_ENV_REBUILD
518     fi
519    
520     xtitleclean
521    
522     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
523     echo -n "package "
524     # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
525     # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
526     echo "successfully installed."
527    
528     # unset these functions
529     unset -f preinstall
530     unset -f postinstall
531     unset -f preremove
532     unset -f postremove
533     }
534    
535     md5sum_packages()
536     {
537     local list="$@"
538     local magefile
539     local pcat
540     local pname
541     local pkgname
542     local pkgfile
543     local pkgtype
544     local count_current
545     local count_total
546     local pver
547     local pbuild
548    
549     # get count of total packages
550     declare -i count_current=0
551     declare -i count_total=0
552    
553     for i in ${list}; do (( count_total++ )); done
554    
555     for magefile in ${list}
556     do
557     pcat=${magefile%%/*}
558     pname=$(magename2pname ${magefile})
559     pver=$(magename2pver ${magefile})
560     pbuild=$(magename2pbuild ${magefile})
561    
562     pkgname="${pname}-${pver}-${ARCH}-${pbuild}"
563     #md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
564     pkgfile="${pkgname}.${PKGSUFFIX}"
565 niro 1114 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
566     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}'")
567 niro 486 #echo "DEBUG: ${md5}"
568    
569     (( count_current++ ))
570     xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
571    
572     # abort on virtual pkg
573     if [[ ${pkgtype} = virtual ]]
574     then
575     echo -ne " ${COLBLUE}---${COLDEFAULT}"
576     echo " !md5sum virtual (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
577     continue
578     fi
579    
580     # abort on sources pkg
581     if [[ ${pkgtype} = sources ]]
582     then
583     echo -ne " ${COLBLUE}---${COLDEFAULT}"
584     echo " !md5sum sources (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
585     continue
586     fi
587    
588     # if [ -f "${md5file}" ]
589     # then
590     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
591     echo -ne "checking md5sum (${count_current}/${count_total}): "
592     #( cd ${PKGDIR}; md5sum --check ${md5file}) || die "md5 for ${pkgfile} failed"
593     ( cd ${PKGDIR}; md5sum --check << EOF ;)|| die "md5 for ${pkgfile} failed"
594     ${md5} ${pkgfile}
595     EOF
596    
597     # else
598     # echo -ne "${COLBLUE} --- ${COLDEFAULT}"
599     # echo -e "!! no md5sum file found for ${pkgfile} :("
600     # fi
601     done
602    
603     # add a crlf for a better view
604     if [ ${count_total} -gt 1 ]; then echo; fi
605     }
606    
607     unpack_packages()
608     {
609     local list="$@"
610     local magefile
611     local pkg
612     local pkgtype
613     local count_current
614     local count_total
615    
616     # get count of total packages
617     declare -i count_current=0
618     declare -i count_total=0
619    
620     for i in ${list}; do (( count_total++ )); done
621    
622     for magefile in ${list}
623     do
624     #pkg="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}"
625     #pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
626    
627     pname=$(magename2pname ${magefile})
628     pver=$(magename2pver ${magefile})
629     pbuild=$(magename2pbuild ${magefile})
630     pkg="${pname}-${pver}-${ARCH}-${pbuild}.${PKGSUFFIX}"
631 niro 1114 pkgtype=$(sql "select pkgtype from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
632 niro 486
633     (( count_current++ ))
634     xtitle "[ (${count_current}/${count_total}) Unpacking ${pkg} ]"
635    
636     # abort on virtual pkg
637     if [[ ${pkgtype} = virtual ]]
638     then
639     echo -ne " ${COLBLUE}---${COLDEFAULT}"
640     echo " !unpack virtual (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
641     continue
642     fi
643    
644     # abort on sources pkg
645     if [[ ${pkgtype} = sources ]]
646     then
647     echo -ne " ${COLBLUE}---${COLDEFAULT}"
648     echo " !unpack sources (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... "
649     continue
650     fi
651    
652     echo -e " ${COLBLUE}***${COLDEFAULT} unpacking (${count_current}/${count_total}): ${pkg} ... "
653     tar xjmf ${PKGDIR}/${pkg} -C ${BUILDDIR} || die "Unpacking package ${pkg}"
654     done
655    
656     # add a crlf for a better view
657     if [ ${count_total} -gt 1 ]; then echo; fi
658     }
659    
660     install_packages()
661     {
662     local list="$@"
663     local pkg
664     local pcat
665     local pname
666     local pver
667     local pbuild
668     local total_pkgs
669     local current_pkg
670     local src_install
671     local uninstall_list
672    
673     # check for --src-install
674     if [[ $1 = --src-install ]]
675     then
676     # remove --src-install from list
677     list=${list/--src-install/}
678     # enable src-install
679     src_install="--src-install"
680     fi
681    
682     # reset MAGE_PROTECT_COUNTER
683     declare -i MAGE_PROTECT_COUNTER=0
684     export MAGE_PROTECT_COUNTER
685    
686     # get count of total packages
687     declare -i total_pkgs=0
688     declare -i current_pkg=0
689     for i in ${list}; do (( total_pkgs++ )); done
690    
691     echo
692    
693     if [[ -n ${MROOT} ]]
694     then
695     echo -ne ${COLRED}
696     echo "!! installing in MROOT=${MROOT}"
697     echo -ne ${COLDEFAULT}
698     echo
699     fi
700    
701     for pkg in ${list}
702     do
703     (( current_pkg++ ))
704     #pcat=$(magename2pcat ${pkg})
705     pcat="${pkg%%/*}"
706     pname=$(magename2pname ${pkg})
707     pver=$(magename2pver ${pkg})
708     pbuild=$(magename2pbuild ${pkg})
709    
710     mage_install \
711     --pcat ${pcat} \
712     --pname ${pname} \
713     --pver ${pver} \
714     --pbuild ${pbuild} \
715     --count-total ${total_pkgs} \
716     --count-current ${current_pkg} \
717     ${src_install}
718    
719     # check for allready installed packages and remove them
720     # except the package we have installed
721     uninstall_list="$(get_uninstall_candidates \
722     --pcat "${pcat}" \
723     --pname "${pname}" \
724     --protected ${pcat}/${pname}-${pver}-${pbuild})"
725    
726     # uninstall all packges in uninstall_list if not empty
727     if [ -n "${uninstall_list}" ]
728     then
729     echo
730     uninstall_packages ${uninstall_list} \
731     || die "install_packges() uninstalling not-needed."
732     fi
733    
734     # crlf for better view in VERBOSE mode
735     #if [[ ${VERBOSE} = on ]]; then echo; fi
736     echo
737     done
738    
739     #echo "DEBUG MAGE_PROTECT_COUNTER=${MAGE_PROTECT_COUNTER}"
740     show_etc_update_mesg
741     }
742    
743     uninstall_packages()
744     {
745     local list="$@"
746     local pcat
747     local pname
748     local pver
749     local pbuild
750     local can_pcat
751     local can_pname
752     local can_ver_list
753    
754     if [[ -n ${MROOT} ]]
755     then
756     echo -ne ${COLRED}
757     echo "!! uninstalling from MROOT=${MROOT}"
758     echo -ne ${COLDEFAULT}
759     echo
760     fi
761    
762     # generate a candidates list
763     for pkg in ${list}
764     do
765     #pcat=$(dep2pcat ${pkg})
766     pcat="${pkg%%/*}"
767     pname=$(magename2pname ${pkg})
768     pver=$(magename2pver ${pkg})
769     pbuild=$(magename2pbuild ${pkg})
770     can_pcat="${pcat}"
771     can_pname="${pname}"
772    
773     if [ -z "${can_ver_list}" ]
774     then
775     can_ver_list=" ${pver}-${pbuild}"
776     else
777     can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
778     fi
779     done
780    
781     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
782     echo "following candidate(s) will be removed:"
783     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
784     echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
785     echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
786     echo
787     if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
788     then
789     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
790     echo "( Press [CTRL+C] to abort )"
791     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
792     echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
793     for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
794     do
795     echo -ne "${COLRED} ${i}${COLDEFAULT}"
796     sleep 1
797     done
798     echo
799     echo
800     fi
801    
802     for pkg in ${list}
803     do
804     pcat=$(dep2pcat ${pkg})
805     pname=$(magename2pname ${pkg})
806     pver=$(magename2pver ${pkg})
807     pbuild=$(magename2pbuild ${pkg})
808    
809     mage_uninstall \
810     --pcat ${pcat} \
811     --pname ${pname} \
812     --pver ${pver} \
813     --pbuild ${pbuild} \
814     --count-total ${total_pkgs} \
815     --count-current ${current_pkg} \
816     ${src_install}
817    
818     # crlf for better view in VERBOSE mode
819     #if [[ ${VERBOSE} = on ]]; then echo; fi
820     echo
821     done
822     }
823    
824     is_installed()
825     {
826     local DBFILE
827     local pkgid
828     local pcat
829     local pname
830     local pbuild
831     local pver
832    
833     # very basic getops
834     for i in $*
835     do
836     case $1 in
837     --pcat|-c) shift; pcat="$1" ;;
838     --pname|-n) shift; pname="$1" ;;
839     --pver|-v) shift; pver="$1" ;;
840     --pbuild|-b) shift; pbuild="$1" ;;
841     esac
842     shift
843     done
844    
845     DBFILE="${MAGE_INSTALL_DB}"
846     pkgid=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
847    
848     [[ ! -z ${pkgid} ]] && return 0
849     return 1
850     }
851    
852     reverse_depends()
853     {
854     local DBFILE
855     local pname="$1"
856    
857     DBFILE="${MAGE_INSTALL_DB}"
858     sql "select categories.pcat,
859     packages.pname,
860     packages.pver,
861     packages.pbuild
862     from depends
863     inner join packages on packages.id=depends.pkg_id
864     inner join categories on categories.id=packages.pcat_id
865     where depends.pname='${pname}'"
866     }
867    
868     reverse_sdepends()
869     {
870     local DBFILE
871     local pname="$1"
872    
873     DBFILE="${MAGE_INSTALL_DB}"
874     sql "select categories.pcat,
875     packages.pname,
876     packages.pver,
877     packages.pbuild
878     from sdepends
879     inner join packages on packages.id=sdepends.pkg_id
880     inner join categories on categories.id=packages.pcat_id
881     where sdepends.pname='${pname}'"
882     }
883    
884     create_database_layout()
885     {
886     sql "create table categories(id integer primary key, pcat text unique)"
887     sql "create table packages(id integer primary key,
888     pname text,
889     pver text,
890     pbuild text,
891     pcat_id numeric,
892     state text,
893     provide text,
894     pkgtype text
895     )"
896     sql "create table depends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
897     sql "create table sdepends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
898     sql "create table packages_info(id integer primary key, pkg_id numeric, arch text, md5 text, mtime numeric, homepage text, description text, size numeric)"
899     #sql "create table packages_content(id integer primary key, pkginfo_id numeric, char text, dirs text, files text, pipes text, symlinks text)"
900     sql "create table packages_content(id integer primary key, pkginfo_id numeric, char blob, dirs blob, files blob, pipes blob, symlinks blob)"
901     }
902    
903     install_database_entry()
904     {
905     local pcat
906     local pname
907     local pver
908     local pbuild
909     local pkgtype
910     local pkgname
911     local magefile
912     local dbrecorddir
913     local provide
914     local homepage
915     local description
916     local pkgid
917     local depends
918     local sdepends
919     local relation
920     local depcatid
921     local depname
922     local depver
923     local depbuild
924     local mage_pkg_id
925     local i
926     local install_pkg_id
927     local table
928    
929     # very basic getops
930     for i in $*
931     do
932     case $1 in
933     --pcat|-c) shift; pcat="$1" ;;
934     --pname|-n) shift; pname="$1" ;;
935     --pver|-v) shift; pver="$1" ;;
936     --pbuild|-b) shift; pbuild="$1" ;;
937     --pkgname|-a) shift; pkgname="$1" ;;
938     # --state|-s) shift; state="$1" ;;
939     --pkgtype|-t) shift; pkgtype="$1" ;;
940     esac
941     shift
942     done
943    
944     # sanity checks; abort if not given
945     [[ -z ${pcat} ]] && die "install_database_entry() \$pcat not given."
946     [[ -z ${pname} ]] && die "install_database_entry() \$pname not given."
947     [[ -z ${pver} ]] && die "install_database_entry() \$pver not given."
948     [[ -z ${pbuild} ]] && die "install_database_entry() \$pbuild not given."
949     [[ -z ${pkgname} ]] && die "install_database_entry() \$pkgname not given."
950     # [[ -z ${state} ]] && die "install_database_entry() \$state not given."
951    
952     # check needed global vars
953     [[ -z ${MAGEDIR} ]] && die "install_database_entry() \$MAGEDIR not set."
954     [[ -z ${INSTALLDB} ]] && die "install_database_entry() \$INSTALLDB not set."
955    
956     # first of all convert pbuild to database format (remove the r)
957 niro 1114 pbuild="${pbuild}"
958 niro 486
959     # check if
960     # copy all things over over
961     # mage.pkg_id
962     DBFILE="${MAGE_PACKAGES_DB}"
963     mage_pkg_id=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
964    
965     DBFILE="${MAGE_INSTALL_DB}"
966     # add pcat to installdb if missing
967     if [[ -z $(sql "select id from categories where pcat='${pcat}'") ]]
968     then
969     sql "insert into categories (pcat) values ('${pcat}')"
970     fi
971    
972     # attach install database
973     # install.categories.id != main.categories.id !!!
974     # so get pcat from pcat_id via inner join.
975     # then query install db which id pcat has and use this one
976     # final phase copies the whole crap to the install db
977     # use some more rows to copy the whole table
978     sqlite3 ${MAGE_PACKAGES_DB} \
979     "attach database '${MAGE_INSTALL_DB}' as install;
980     insert into install.packages
981     (pcat_id,
982     pname,
983     pver,
984     pbuild,
985     state,
986     provide,
987     pkgtype)
988     select mage_id,
989     main.packages.pname,
990     main.packages.pver,
991     main.packages.pbuild,
992     main.packages.state,
993     main.packages.provide,
994     main.packages.pkgtype
995     from main.packages,
996     (select id as mage_id
997     from install.categories where pcat=(
998     select pcat from main.packages
999     inner join main.categories
1000     on main.categories.id=main.packages.pcat_id
1001     where main.packages.id='${mage_pkg_id}')
1002     )
1003     where main.packages.id=${mage_pkg_id};"
1004    
1005     # get install db pkg_id
1006     install_pkg_id=$(sql "select max(id) from packages")
1007    
1008     # now copy dependencies
1009     # replace pkg with the one from insatll.db
1010     for table in depends sdepends
1011     do
1012     sqlite3 ${MAGE_PACKAGES_DB} \
1013     "attach database '${MAGE_INSTALL_DB}' as install;
1014     insert into install.${table}
1015     (pkg_id,
1016     relation,
1017     pcat_id,
1018     pname,
1019     pver,
1020     pbuild)
1021     select '${install_pkg_id}',
1022     relation,
1023     pcat_id,
1024     pname,
1025     pver,
1026     pbuild
1027     from main.${table} where pkg_id='${mage_pkg_id}'"
1028     done
1029    
1030     # and now the package infos
1031     sqlite3 ${MAGE_PACKAGES_DB} \
1032     "attach database '${MAGE_INSTALL_DB}' as install;
1033     insert into install.packages_info
1034     (pkg_id,
1035     arch,
1036     md5,
1037     mtime,
1038     homepage,
1039     description,
1040     size)
1041     select '${install_pkg_id}',
1042     arch,
1043     md5,
1044     mtime,
1045     homepage,
1046     description,
1047     size
1048     from main.packages_info
1049     where pkg_id='${mage_pkg_id}' and arch='${ARCH}'"
1050    
1051     # now install PKGTYPE specific files
1052     case ${pkgtype} in
1053     virtual) echo ;;
1054     sources) echo ;;
1055     *)
1056     # and finally the package_content
1057     # but we first need to know the pkg_info id
1058     # because of the arch dependency
1059     pkg_info_id=$(sql "select max(id) from packages_info")
1060    
1061     sql "insert into packages_content
1062     (pkginfo_id,
1063     char,
1064     dirs,
1065     files,
1066     pipes,
1067     symlinks)
1068     values('${pkg_info_id}',
1069     'records/${pkg_info_id}/char.bz2',
1070     'records/${pkg_info_id}/dirs.bz2',
1071     'records/${pkg_info_id}/files.bz2',
1072     'records/${pkg_info_id}/pipes.bz2',
1073     'records/${pkg_info_id}/symlinks.bz2')"
1074     # create compressed content files
1075 niro 1114 entrydir=${BUILDDIR}/${pname}-${pver}-${ARCH}-${pbuild}
1076 niro 486 local entryfile
1077     for entryfile in char dirs files pipes symlinks
1078     do
1079     install -d $(dirname ${MAGE_INSTALL_DB})/records/${pkg_info_id}
1080     cat ${entrydir}/.${entryfile} | bzip2 -9 >> $(dirname ${MAGE_INSTALL_DB})/records/${pkg_info_id}/${entryfile}.bz2
1081     done
1082     ;;
1083     esac
1084     }