Magellan Linux

Annotation of /branches/mage-next/src/mage4.functions.sh.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2753 - (hide annotations) (download)
Thu Aug 14 14:29:11 2014 UTC (9 years, 8 months ago) by niro
File size: 81427 byte(s)
-fixed broken count_installed_pkgs() function
1 niro 226 #!/bin/bash
2     # Magellan Linux Installer Functions (mage.functions.sh)
3 niro 2268 # $Id$
4 niro 226
5     mage_setup()
6     {
7     [ ! -d ${MROOT}${INSTALLDB} ] && \
8     install -d ${MROOT}${INSTALLDB}
9     [ ! -f ${MROOT}${VIRTUALDB_FILE} ] && \
10     touch ${MROOT}${VIRTUALDB_FILE}
11     [ ! -d ${PKGDIR} ] && install -d ${PKGDIR}
12     [ ! -d ${BUILDDIR} ] && install -d ${BUILDDIR}
13     [ ! -d ${MAGEDIR} ] && install -d ${MAGEDIR}
14    
15     return 0
16     }
17    
18 niro 1549 mchecksum()
19     {
20     local i
21     local rundir
22     local file
23     local method
24     local cmd
25     local retval
26 niro 2225 local sum
27     local dest
28 niro 1549
29     # very basic getops
30     for i in $*
31     do
32     case $1 in
33     --rundir|-r) shift; rundir="$1" ;;
34     --file|-f) shift; file="$1" ;;
35     --method|-m) shift; method="$1" ;;
36     esac
37     shift
38     done
39    
40     # sanity checks
41     [[ -z ${rundir} ]] && die "mchecksum(): rundir missing"
42     [[ -z ${file} ]] && die "mchecksum(): file missing"
43     [[ -z ${method} ]] && die "mchecksum(): method missing"
44    
45     case ${method} in
46     md5) cmd="md5sum" ;;
47     sha256) cmd="sha256sum" ;;
48 niro 1625 *) die "mchecksum(): unknown method '${method}'" ;;
49 niro 1549 esac
50    
51     if [[ -d ${rundir} ]]
52     then
53     pushd ${rundir} &> /dev/null
54 niro 2225
55     # all file must be non-zero
56     retval=0
57     while read sum dest
58     do
59 niro 2232 if [ ! -s ${dest} ]
60 niro 2225 then
61     echo "${dest}: file is empty ;("
62     retval=127
63     fi
64     done < ${file}
65     if [[ ${retval} != 127 ]]
66     then
67     # be verbose here
68     ${cmd} -c ${file} #&> /dev/null
69     retval="$?"
70     fi
71    
72 niro 1549 popd &> /dev/null
73     else
74     retval=1
75     fi
76    
77     return "${retval}"
78     }
79    
80 niro 1653 mcheckemptydir()
81     {
82     local dir="$1"
83     local retval=1
84    
85     if [[ ! -d ${dir} ]]
86     then
87     echo "mcheckemptydir(): '${dir}' is not a directory!"
88     retval=3
89     else
90     shopt -s nullglob dotglob
91     files=( ${dir}/* )
92     (( ${#files[*]} )) || retval=0
93     shopt -u nullglob dotglob
94     fi
95    
96     return ${retval}
97     }
98    
99 niro 2156 unpack_package()
100     {
101     local magefile="$1"
102 niro 2271 local pkgname
103     local pkgfile
104 niro 2156 local pkgtype
105     local tar_opts
106    
107 niro 2271 pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
108     pkgfile="${pkgname}.${PKGSUFFIX}"
109 niro 2156 pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
110    
111     xtitle "[ Unpacking ${pkg} ]"
112    
113     # abort on virtual pkg
114     if [[ ${pkgtype} = virtual ]]
115     then
116     echo -ne " ${COLBLUE}---${COLDEFAULT}"
117 niro 2271 echo " !unpack virtual ${pkgname} ... "
118 niro 2156 continue
119     fi
120    
121     # abort on sources pkg
122     if [[ ${pkgtype} = sources ]]
123     then
124     echo -ne " ${COLBLUE}---${COLDEFAULT}"
125 niro 2271 echo " !unpack sources ${pkgname} ... "
126 niro 2156 continue
127     fi
128    
129     # busybox?
130     if need_busybox_support tar
131     then
132     tar_opts="xjf"
133     else
134     tar_opts="xjmf"
135     fi
136    
137 niro 2271 echo -e " ${COLBLUE}***${COLDEFAULT} unpacking ${pkgfile} ... "
138     tar ${tar_opts} ${PKGDIR}/${pkgfile} -C ${BUILDDIR} || die "Unpacking package ${pkgfile}"
139 niro 2156 }
140    
141 niro 226 unpack_packages()
142     {
143     local list="$@"
144     local magefile
145     local count_current
146     local count_total
147 niro 1273 local tar_opts
148 niro 226
149     # get count of total packages
150     declare -i count_current=0
151     declare -i count_total=0
152    
153     for i in ${list}; do (( count_total++ )); done
154    
155     for magefile in ${list}
156     do
157 niro 2156 unpack_package "${magefile}"
158 niro 226 (( count_current++ ))
159     done
160    
161     # add a crlf for a better view
162     if [ ${count_total} -gt 1 ]; then echo; fi
163     }
164    
165     # fix_mtime path/to/$mtime/reffile $pathto/file
166     # creates a given reference file and fixes given file
167     # returns new mtime
168     fix_mtime()
169     {
170     local reference="$1"
171     local pathto="$2"
172     local mtime
173    
174     mtime=$(stat -c %Y "${reference}")
175     touch \
176     --no-create \
177 niro 1690 --no-dereference \
178 niro 226 --time=mtime \
179 niro 1690 --reference="${reference}" \
180 niro 226 "${pathto}"
181    
182     echo "${mtime}"
183     }
184    
185     # fix_descriptor pkgname/.dir "foo1" "foo2"
186     fix_descriptor()
187     {
188     local descriptor="$1"
189     local output
190     local i
191     shift
192    
193     for i in $@
194     do
195     if [[ -z ${output} ]]
196     then
197     output="${i}"
198     else
199     output="${output}§${i}"
200     fi
201     done
202    
203     echo "${output}" >> ${BUILDDIR}/${descriptor}_fixed
204     }
205    
206     ###################################################
207     # function install_direcories #
208     # install_direcories $PKGNAME #
209     ###################################################
210     install_directories()
211     {
212     local pkgname="$1"
213     local pathto
214     local posix
215     local user
216     local group
217     local IFS
218    
219     # sanity checks; abort if not given
220     [ -z "${pkgname}" ] && die "install_directories() \$pkgname not given."
221    
222     # check needed global vars
223     [ -z "${BUILDDIR}" ] && die "install_directories() \$BUILDDIR not set."
224    
225     [ ! -f ${BUILDDIR}/${pkgname}/.dirs ] && die "install_directories() .dirs not found"
226    
227     # sets fieldseperator to "§" instead of " "
228     IFS=§
229    
230     while read pathto posix user group
231     do
232     [ -z "${pathto}" ] && continue
233 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> DIR: ${MROOT}${pathto}"
234 niro 226
235     # monitors /etc/env.d -> env-rebuild
236     [[ ${pathto} = /etc/env.d ]] && export MAGE_ENV_REBUILD=true
237    
238     # monitors /usr/share/info -> info-rebuild
239     [[ ${pathto} = /usr/share/info ]] && export MAGE_INFO_REBUILD=true
240    
241     install -m "${posix}" -o "${user}" -g "${group}" -d "${MROOT}${pathto}"
242     done < ${BUILDDIR}/${pkgname}/.dirs
243    
244     # very important: unsetting the '§' fieldseperator
245     IFS=$'\n'
246     }
247    
248    
249     ###################################################
250     # function install_files #
251     # install_files $PKGNAME #
252     ###################################################
253     install_files()
254     {
255    
256     local pkgname="$1"
257     local pathto
258     local posix
259     local user
260     local group
261     local mtime
262     local md5sum
263    
264     local retval
265     local counter
266     local filename
267     local dest_dirname
268     local dest_protected
269     local IFS
270    
271     # sanity checks; abort if not given
272     [ -z "${pkgname}" ] && die "install_files() \$pkgname not given."
273    
274     # check needed global vars
275     [ -z "${BUILDDIR}" ] && die "install_files() \$BUILDDIR not set."
276    
277     [ ! -f ${BUILDDIR}/${pkgname}/.files ] && die "install_files() .files not found"
278    
279     # delete old files
280     [ -f ${BUILDDIR}/${pkgname}/.files_fixed ] && rm ${BUILDDIR}/${pkgname}/.files_fixed
281    
282     # sets fieldseperator to "§" instead of " "
283     IFS=§
284    
285     while read pathto posix user group mtime md5sum
286     do
287     [ -z "${pathto}" ] && continue
288    
289     # final destination dir of file ${pathto}
290     dest_dirname="$(dirname "${MROOT}${pathto}")"
291    
292     ### small hotfix fix ###
293     [ ! -d "${dest_dirname}" ] && install -d "${dest_dirname}"
294    
295     # check if the file is config_protected
296     # ${MROOT} will automatically added if set !!
297     is_config_protected "${pathto}"
298     retval="$?"
299    
300 niro 942 # 0 - not protected #
301     # 1 - error #
302     # 2 - protected #
303     # 3 - protected but masked #
304     # 4 - protected but ignored #
305 niro 226
306     case ${retval} in
307     # file is not protected - (over)write it
308     0|3)
309 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> FILE: ${MROOT}${pathto}"
310 niro 226 install -m "${posix}" -o "${user}" -g "${group}" \
311     ${BUILDDIR}/${pkgname}/binfiles/"${pathto}" \
312     "${MROOT}${pathto}"
313    
314     # fix mtime and db
315     fix_descriptor ${pkgname}/.files \
316     "${pathto}" \
317     "${posix}" \
318     "${user}" \
319     "${group}" \
320     "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \
321 niro 1289 "${MROOT}${pathto}")" \
322 niro 226 "${md5sum}"
323     ;;
324    
325     # file is protected, write backup file
326     2)
327 niro 1584 if mqueryfeature "verbose"
328 niro 226 then
329     echo -en "${COLRED}"
330     echo -n "! prot "
331     echo -en "${COLDEFAULT}"
332     echo " === FILE: ${MROOT}${pathto}"
333     fi
334     filename="$(basename "${pathto}")"
335     counter=$(count_protected_files "${pathto}")
336     dest_protected="${dest_dirname}/._cfg${counter}_${filename}"
337     install -m "${posix}" -o "${user}" -g "${group}" \
338     ${BUILDDIR}/${pkgname}/binfiles/"${pathto}" \
339     "${dest_protected}"
340    
341     # fix mtime and db
342     fix_descriptor ${pkgname}/.files \
343     "${pathto}" \
344     "${posix}" \
345     "${user}" \
346     "${group}" \
347     "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \
348 niro 1289 "${dest_protected}")" \
349 niro 226 "${md5sum}"
350    
351     # update global MAGE_PROTECT_COUNTER
352     (( MAGE_PROTECT_COUNTER++ ))
353     export MAGE_PROTECT_COUNTER
354     ;;
355 niro 942
356     # file is protected but ignored, delete the update/do nothing
357     4)
358 niro 1584 if mqueryfeature "verbose"
359 niro 942 then
360     echo -en "${COLRED}"
361     echo -n "! ignr "
362     echo -en "${COLDEFAULT}"
363     echo " === FILE: ${MROOT}${pathto}"
364     fi
365 niro 1289 # simply do nothing here - only fix mtime
366     fix_descriptor ${pkgname}/.files \
367     "${pathto}" \
368     "${posix}" \
369     "${user}" \
370     "${group}" \
371     "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \
372     "${MROOT}${pathto}")" \
373     "${md5sum}"
374 niro 942 ;;
375 niro 226 esac
376     done < ${BUILDDIR}/${pkgname}/.files
377    
378     # now copy the fixed file over the old one
379     [ -f ${BUILDDIR}/${pkgname}/.files_fixed ] && \
380     cp ${BUILDDIR}/${pkgname}/.files{_fixed,}
381    
382     # very important: unsetting the '§' fieldseperator
383     IFS=$'\n'
384     }
385    
386    
387     ###################################################
388     # function install_symlinks #
389     # install_symlinks $PKGNAME #
390     ###################################################
391     install_symlinks()
392     {
393     local pkgname="$1"
394     local pathto
395     local posix
396     local link
397     local mtime
398     local IFS
399    
400     # sanity checks; abort if not given
401     [ -z "${pkgname}" ] && die "install_symlinks() \$pkgname not given."
402    
403     # check needed global vars
404     [ -z "${BUILDDIR}" ] && die "install_symlinks() \$BUILDDIR not set."
405    
406     [ ! -f ${BUILDDIR}/${pkgname}/.symlinks ] && die "install_symlinks() .symlinks not found"
407    
408     # delete old files
409     [ -f ${BUILDDIR}/${pkgname}/.symlinks_fixed ] && rm ${BUILDDIR}/${pkgname}/.symlinks_fixed
410    
411     # sets fieldseperator to "§" instead of " "
412     IFS=§
413    
414     while read pathto posix link mtime
415     do
416     [ -z "${pathto}" ] && continue
417 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> LINK: ${MROOT}${pathto}"
418 niro 226
419     ln -snf "${link}" "${MROOT}${pathto}"
420    
421 niro 1690 # fix mtime and db
422     fix_descriptor ${pkgname}/.symlinks \
423     "${pathto}" \
424     "${posix}" \
425     "${link}" \
426     "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \
427     "${MROOT}${pathto}")"
428 niro 226
429     done < ${BUILDDIR}/${pkgname}/.symlinks
430    
431 niro 858 # # now copy the fixed file over the old one
432     # [ -f ${BUILDDIR}/${pkgname}/.symlinks_fixed ] && \
433     # cp -f ${BUILDDIR}/${pkgname}/.symlinks{_fixed,}
434 niro 226
435     # very important: unsetting the '§' fieldseperator
436     IFS=$'\n'
437     }
438    
439    
440     ###################################################
441     # function install_blockdevices #
442     # install_blockdevices $PKGNAME #
443     ###################################################
444     install_blockdevices()
445     {
446     local pkgname="$1"
447     local pathto
448     local posix
449 niro 1209 local user
450     local group
451 niro 226 local IFS
452    
453     # sanity checks; abort if not given
454     [ -z "${pkgname}" ] && die "install_blockdevices() \$pkgname not given."
455    
456     # check needed global vars
457     [ -z "${BUILDDIR}" ] && die "install_blockdevices() \$BUILDDIR not set."
458    
459     [ ! -f ${BUILDDIR}/${pkgname}/.pipes ] && die "install_blockdevices() .pipes not found"
460    
461     # sets fieldseperator to "§" instead of " "
462     IFS=§
463    
464 niro 1271 while read pathto posix major minor user group
465 niro 226 do
466     [ -z "${pathto}" ] && continue
467 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> PIPE: ${MROOT}${pathto}"
468 niro 226
469 niro 1271 mknod -m "${posix}" "${MROOT}${pathto}"
470 niro 1211 # make it optional atm !!
471     if [[ ! -z ${user} ]] && [[ ! -z ${group} ]]
472     then
473 niro 1271 chown "${user}:${group}" "${MROOT}${pathto}" b "${major}" "${minor}"
474 niro 1211 fi
475 niro 226 done < ${BUILDDIR}/${pkgname}/.pipes
476    
477     # very important: unsetting the '§' fieldseperator
478     IFS=$'\n'
479     }
480    
481    
482     ###################################################
483     # function install_characterdevices #
484     # install_characterdevices $PKGNAME #
485     ###################################################
486     install_characterdevices()
487     {
488     local pkgname="$1"
489     local pathto
490     local posix
491 niro 312 local major
492     local minor
493 niro 1209 local user
494     local group
495 niro 226 local IFS
496    
497     # sanity checks; abort if not given
498     [ -z "${pkgname}" ] && die "install_characterdevices() \$pkgname not given."
499    
500     # check needed global vars
501     [ -z "${BUILDDIR}" ] && die "install_characterdevices() \$BUILDDIR not set."
502    
503     [ ! -f ${BUILDDIR}/${pkgname}/.char ] && die "install_characterdevices() .char not found"
504    
505     # sets fieldseperator to "§" instead of " "
506     IFS=§
507    
508 niro 1209 while read pathto posix major minor user group
509 niro 226 do
510     [ -z "${pathto}" ] && continue
511 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> CHAR: ${MROOT}${pathto}"
512 niro 226
513 niro 1271 mknod -m ${posix} "${MROOT}${pathto}" b "${major}" "${minor}"
514 niro 1211
515     # make it optional atm !!
516     if [[ ! -z ${user} ]] && [[ ! -z ${group} ]]
517     then
518     chown "${user}:${group}" "${MROOT}${pathto}"
519     fi
520 niro 226 done < ${BUILDDIR}/${pkgname}/.char
521    
522     # very important: unsetting the '§' fieldseperator
523     IFS=$'\n'
524     }
525    
526 niro 1209 ###################################################
527     # function install_fifos #
528     # install_fifos $PKGNAME #
529     ###################################################
530     install_fifos()
531     {
532     local pkgname="$1"
533     local pathto
534     local posix
535     local user
536     local group
537     local IFS
538 niro 226
539 niro 1209 # sanity checks; abort if not given
540     [ -z "${pkgname}" ] && die "install_fifos() \$pkgname not given."
541    
542     # check needed global vars
543     [ -z "${BUILDDIR}" ] && die "install_fifos() \$BUILDDIR not set."
544    
545 niro 1211 # make it optional atm !!
546     #[ ! -f ${BUILDDIR}/${pkgname}/.fifo ] && die "install_fifos() .fifo not found"
547     [ ! -f ${BUILDDIR}/${pkgname}/.fifo ] && return
548 niro 1209
549     # sets fieldseperator to "§" instead of " "
550     IFS=§
551    
552     while read pathto posix user group
553     do
554     [ -z "${pathto}" ] && continue
555 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> FIFO: ${MROOT}${pathto}"
556 niro 1209
557     mkfifo -m "${posix}" "${MROOT}${pathto}"
558     chown "${user}:${group}" "${MROOT}${pathto}"
559     done < ${BUILDDIR}/${pkgname}/.fifo
560    
561     # very important: unsetting the '§' fieldseperator
562     IFS=$'\n'
563     }
564    
565    
566 niro 226 ###################################################
567     # function build_doinstall #
568     # build_doinstall $PKGNAME #
569 niro 1209 # NOTE: this is an wrapper to install packages #
570 niro 226 ###################################################
571     build_doinstall()
572     {
573     local pkgname="$1"
574    
575     # sanity checks; abort if not given
576     [ -z "${pkgname}" ] && die "build_doinstall() \$pkgname not given."
577 niro 1289
578 niro 226 # this is only a wrapper
579    
580     # NOTE:
581     # !! we use § as field seperator !!
582     # doing so prevent us to get errors by filenames with spaces
583    
584     # create a new mtime reference file
585     touch ${BUILDDIR}/${pkgname}/.mtime
586    
587     install_directories ${pkgname} || die "install directories ${pkgname}"
588     install_files ${pkgname} || die "install files ${pkgname}"
589     install_symlinks ${pkgname} || die "install symlinks ${pkgname}"
590     install_blockdevices ${pkgname} || die "install blockdevices ${pkgname}"
591     install_characterdevices ${pkgname} || die "install chardevices ${pkgname}"
592 niro 1209 install_fifos ${pkgname} || die "install fifos ${pkgname}"
593 niro 226 }
594    
595    
596     ###################################################
597     # function install_database_entry #
598     # install_database_entry $PKGNAME $PKGTYPE #
599     # PKGTYPE can be "virtual", "sources" or nothing #
600     ###################################################
601     install_database_entry()
602     {
603     local pcat
604     local pname
605     local pver
606     local pbuild
607     local pkgtype
608     local pkgname
609     local magefile
610     local dbrecorddir
611     local provide
612 niro 273 local i
613 niro 226
614     # very basic getops
615     for i in $*
616     do
617     case $1 in
618     --pcat|-c) shift; pcat="$1" ;;
619     --pname|-n) shift; pname="$1" ;;
620     --pver|-v) shift; pver="$1" ;;
621     --pbuild|-b) shift; pbuild="$1" ;;
622     --pkgname|-a) shift; pkgname="$1" ;;
623     --pkgtype|-t) shift; pkgtype="$1" ;;
624     esac
625     shift
626     done
627    
628     # sanity checks; abort if not given
629     [ -z "${pcat}" ] && die "install_database_entry() \$pcat not given."
630     [ -z "${pname}" ] && die "install_database_entry() \$pname not given."
631     [ -z "${pver}" ] && die "install_database_entry() \$pver not given."
632     [ -z "${pbuild}" ] && die "install_database_entry() \$pbuild not given."
633     [ -z "${pkgname}" ] && die "install_database_entry() \$pkgname not given."
634    
635     # check needed global vars
636     [ -z "${MAGEDIR}" ] && die "install_database_entry() \$MAGEDIR not set."
637     [ -z "${INSTALLDB}" ] && die "install_database_entry() \$INSTALLDB not set."
638    
639     # set needed vars
640     magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
641     dbrecorddir="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}"
642    
643     # abort if mage file not exists
644     [ ! -f ${magefile} ] && die "install_database_entry() ${magefile} not exist."
645    
646     # add package to database
647     install -d ${dbrecorddir}
648    
649     # install mage-file to database
650     install -m 0644 -o root -g root ${magefile} ${dbrecorddir}
651    
652     # create fake file descriptors
653     # used by virtual and source packages
654 niro 1209 for i in .dirs .symlinks .files .pipes .char .fifo
655 niro 226 do
656     touch ${dbrecorddir}/${i}
657     done
658    
659     # put the category to database
660     echo ${pcat} > ${dbrecorddir}/.categorie
661    
662     # now install PKGTYPE specific files
663     case ${pkgtype} in
664     virtual) touch ${dbrecorddir}/.virtual ;;
665     sources) touch ${dbrecorddir}/.sources ;;
666     *)
667     # !move! .mtime to database (only mv modifies not the mtime!!)
668     mv ${BUILDDIR}/${pkgname}/.mtime ${dbrecorddir}/.mtime
669    
670     # normal packages needs these files
671     local i
672 niro 1209 for i in .char .dirs .files .pipes .symlinks .fifo
673 niro 226 do
674 niro 1214 # make .fifo optional atm
675     if [[ -f ${BUILDDIR}/${pkgname}/${i} ]]
676     then
677     install -m 0644 ${BUILDDIR}/${pkgname}/${i} ${dbrecorddir}/${i}
678     fi
679 niro 226 done
680     ;;
681     esac
682    
683     # last but not least register virtuals
684     provide="$(get_value_from_magefile PROVIDE ${magefile})"
685     if [ -n "${provide}" ]
686     then
687 niro 273 for i in ${provide}
688     do
689     virtuals_add "${i}" "${pcat}/${pname}"
690     done
691 niro 226 fi
692     }
693    
694    
695     ###################################################
696     # function remove_database_entry #
697     # remove_database_entry $PKGNAME $PKGTYPE #
698     # PKGTYPE can be "virtual", "sources" or nothing #
699     ###################################################
700     remove_database_entry()
701     {
702     local pcat
703     local pname
704     local pver
705     local pbuild
706     local magefile
707     local dbrecorddir
708     local provide
709 niro 273 local i
710 niro 226
711     # very basic getops
712     for i in $*
713     do
714     case $1 in
715     --pcat|-c) shift; pcat="$1" ;;
716     --pname|-n) shift; pname="$1" ;;
717     --pver|-v) shift; pver="$1" ;;
718     --pbuild|-b) shift; pbuild="$1" ;;
719     esac
720     shift
721     done
722    
723     # sanity checks; abort if not given
724     [ -z "${pcat}" ] && die "remove_database_entry() \$pcat not given."
725     [ -z "${pname}" ] && die "remove_database_entry() \$pname not given."
726     [ -z "${pver}" ] && die "remove_database_entry() \$pver not given."
727     [ -z "${pbuild}" ] && die "remove_database_entry() \$pbuild not given."
728    
729     # check needed global vars
730     [ -z "${INSTALLDB}" ] && die "remove_database_entry() \$INSTALLDB not set."
731    
732     # set needed vars
733     magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
734     dbrecorddir="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}"
735    
736     # abort if mage file not exists
737     [ ! -f ${magefile} ] && die "remove_database_entry() ${magefile} not exist."
738    
739 niro 294 # remove virtuals only if no other exist
740 niro 2753 if [[ $(count_installed_pkgs --pcat=${pcat} --pname=${pname}) -le 1 ]]
741 niro 226 then
742 niro 294 # first unregister virtuals
743     provide="$(get_value_from_magefile PROVIDE ${magefile})"
744     if [ -n "${provide}" ]
745     then
746     for i in ${provide}
747     do
748     virtuals_del "${i}" "${pcat}/${pname}"
749     done
750     fi
751 niro 226 fi
752    
753     # removes database entry
754     if [ -d ${dbrecorddir} ]
755     then
756     rm -rf ${dbrecorddir}
757     fi
758     }
759    
760 niro 294 # get the number of installed packages
761     count_installed_pkgs()
762     {
763     local pcat
764     local pname
765     local pkg
766     local i
767 niro 226
768 niro 294 # very basic getops
769 niro 2753 for i in $@
770 niro 294 do
771 niro 2753 case ${i} in
772     --pcat*) pcat="${i#*=}" ;;
773     --pname*) pname="${i#*=}" ;;
774     esac
775 niro 294 done
776    
777     # sanity checks; abort if not given
778     [ -z "${pcat}" ] && die "pkg_count() \$pcat not given."
779     [ -z "${pname}" ] && die "pkg_count() \$pname not given."
780    
781     declare -i i=0
782     for pkg in $(get_uninstall_candidates --pcat ${pcat} --pname ${pname})
783     do
784     (( i++ ))
785     #echo "$i ${pkg}"
786     done
787    
788     # return the value
789     echo "${i}"
790     }
791    
792    
793 niro 226 ###################################################
794     # function compare_mtime #
795     # compare_mtime $pcat/$PKGNAME /path/to/file #
796     # #
797     # returns: #
798     # 0=delete me #
799     # 1=keep me #
800     # #
801     # compares mtime of given files in packages #
802     ###################################################
803     compare_mtime()
804     {
805     local pfull="$1"
806     local pathto="$2"
807     local mtime
808     local pcat
809     local x
810    
811     mtime="$(stat -c %Y ${MROOT}${INSTALLDB}/${pfull}/.mtime)"
812    
813 niro 1690 # no extra handlink for symlinks anymore as fix_mtime
814     # uses --no-dereference, compare directly
815     x=$(stat -c %Y "${MROOT}${pathto}")
816 niro 226
817     [[ ${mtime} = ${x} ]] && return 0
818    
819     # echo "keep me : ${pathto}"
820     return 1
821     }
822    
823    
824     ###################################################
825     # function remove_symlinks #
826     # remove_symlinks $PKGNAME #
827     ###################################################
828     remove_symlinks()
829     {
830     local pathto
831     local posix
832     local link
833     local mtime
834     local IFS
835     local retval
836     local pcat
837     local pname
838     local pver
839     local pbuild
840     local i
841     local pfull
842    
843     IFS=$'\n'
844    
845     # very basic getops
846     for i in $*
847     do
848     case $1 in
849     --pcat|-c) shift; pcat="$1" ;;
850     --pname|-n) shift; pname="$1" ;;
851     --pver|-v) shift; pver="$1" ;;
852     --pbuild|-b) shift; pbuild="$1" ;;
853     esac
854     shift
855     done
856    
857     # sanity checks; abort if not given
858     [ -z "${pcat}" ] && die "remove_symlinks() \$pcat not given."
859     [ -z "${pname}" ] && die "remove_symlinks() \$pname not given."
860     [ -z "${pver}" ] && die "remove_symlinks() \$pver not given."
861     [ -z "${pbuild}" ] && die "remove_symlinks() \$pbuild not given."
862     pfull="${pcat}/${pname}-${pver}-${pbuild}"
863    
864     # check needed global vars
865     [ -z "${BUILDDIR}" ] && die "remove_symlinks() \$BUILDDIR not set."
866    
867     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.symlinks ] && die "remove_symlinks() .symlinks not found"
868    
869     # sets fieldseperator to "§" instead of " "
870     IFS=§
871    
872     while read pathto posix link mtime
873     do
874     [ -z "${pathto}" ] && continue
875     if [ ! -L "${MROOT}${pathto}" ]
876     then
877 niro 1584 mqueryfeature "verbose" && \
878 niro 226 echo -e "${COLRED}! exist${COLDEFAULT} === LINK: ${MROOT}${pathto}"
879     continue
880     fi
881    
882     # *no* ${MROOT}, will be set internal
883     compare_mtime "${pfull}" "${pathto}"
884     retval=$?
885     # 0=delete me #
886     # 1=keep me #
887     case ${retval} in
888     0)
889 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< LINK: ${MROOT}${pathto}"
890 niro 226 rm "${MROOT}${pathto}"
891     ;;
892    
893     1)
894 niro 1584 mqueryfeature "verbose" && \
895 niro 226 echo -e "${COLRED}! mtime${COLDEFAULT} === LINK: ${MROOT}${pathto}"
896     ;;
897     esac
898     done < ${MROOT}${INSTALLDB}/${pfull}/.symlinks
899    
900     # very important: unsetting the '§' fieldseperator
901     IFS=$'\n'
902     }
903    
904    
905     ###################################################
906     # function remove_files #
907     # remove_files $PKGNAME #
908     ###################################################
909     remove_files()
910     {
911     local pathto
912     local posix
913     local user
914     local group
915     local mtime
916     local md5sum
917     local IFS
918     local retval
919     local pcat
920     local pname
921     local pver
922     local pbuild
923     local i
924     local pfull
925    
926     IFS=$'\n'
927    
928     # very basic getops
929     for i in $*
930     do
931     case $1 in
932     --pcat|-c) shift; pcat="$1" ;;
933     --pname|-n) shift; pname="$1" ;;
934     --pver|-v) shift; pver="$1" ;;
935     --pbuild|-b) shift; pbuild="$1" ;;
936     esac
937     shift
938     done
939    
940     # sanity checks; abort if not given
941 niro 1209 [ -z "${pcat}" ] && die "remove_files() \$pcat not given."
942     [ -z "${pname}" ] && die "remove_files() \$pname not given."
943     [ -z "${pver}" ] && die "remove_files() \$pver not given."
944     [ -z "${pbuild}" ] && die "remove_files() \$pbuild not given."
945 niro 226 pfull="${pcat}/${pname}-${pver}-${pbuild}"
946    
947     # check needed global vars
948     [ -z "${BUILDDIR}" ] && die "remove_files() \$BUILDDIR not set."
949    
950     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.files ] && die "remove_files() .files not found"
951    
952     # sets fieldseperator to "§" instead of " "
953     IFS=§
954    
955     while read pathto posix user group mtime md5sum
956     do
957     [ -z "${pathto}" ] && continue
958    
959 niro 240 if [ ! -e "${MROOT}${pathto}" ]
960 niro 226 then
961 niro 1584 mqueryfeature "verbose" && \
962 niro 226 echo -e "${COLRED}! exist${COLDEFAULT} === FILE: ${MROOT}${pathto}"
963     continue
964     fi
965    
966     # *no* ${MROOT}, will be set internal
967     compare_mtime "${pfull}" "${pathto}"
968     retval=$?
969     # 0=delete me #
970     # 1=keep me #
971     case ${retval} in
972     0)
973 niro 280 # check if the file is config_protected
974     # ${MROOT} will automatically added if set !!
975     is_config_protected "${pathto}"
976     retval="$?"
977    
978 niro 942 # 0 - not protected #
979     # 1 - error #
980     # 2 - protected #
981     # 3 - protected but masked #
982     # 4 - protected but ignored #
983 niro 280
984     case ${retval} in
985     # file is not protected - delete it
986     0|3)
987 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< FILE: ${MROOT}${pathto}"
988 niro 280 rm "${MROOT}${pathto}"
989     ;;
990    
991     # file is protected, do not delete
992     2)
993 niro 1584 if mqueryfeature "verbose"
994 niro 280 then
995     echo -en "${COLRED}"
996     echo -n "! prot "
997     echo -en "${COLDEFAULT}"
998     echo " === FILE: ${MROOT}${pathto}"
999     fi
1000     ;;
1001 niro 942
1002     # file is protected but ignored, delete the update/do nothing
1003     4)
1004 niro 1584 if mqueryfeature "verbose"
1005 niro 942 then
1006     echo -en "${COLRED}"
1007     echo -n "! ignr "
1008     echo -en "${COLDEFAULT}"
1009     echo " === FILE: ${MROOT}${pathto}"
1010     fi
1011     # simply do nothing here
1012     ;;
1013 niro 280 esac
1014 niro 226 ;;
1015     1)
1016 niro 1584 mqueryfeature "verbose" && \
1017 niro 226 echo -e "${COLRED}! mtime${COLDEFAULT} === FILE: ${MROOT}${pathto}"
1018     ;;
1019     esac
1020     done < ${MROOT}${INSTALLDB}/${pfull}/.files
1021    
1022     # very important: unsetting the '§' fieldseperator
1023     IFS=$'\n'
1024     }
1025    
1026    
1027     ###################################################
1028     # function remove_blockdevices #
1029     # remove_blockdevices $PKGNAME #
1030     ###################################################
1031     remove_blockdevices()
1032     {
1033     local pathto
1034     local posix
1035 niro 1209 local user
1036     local group
1037 niro 226 local IFS
1038     local pcat
1039     local pname
1040     local pver
1041     local pbuild
1042     local i
1043     local pfull
1044    
1045     IFS=$'\n'
1046    
1047     # very basic getops
1048     for i in $*
1049     do
1050     case $1 in
1051     --pcat|-c) shift; pcat="$1" ;;
1052     --pname|-n) shift; pname="$1" ;;
1053     --pver|-v) shift; pver="$1" ;;
1054     --pbuild|-b) shift; pbuild="$1" ;;
1055     esac
1056     shift
1057     done
1058    
1059     # sanity checks; abort if not given
1060 niro 1209 [ -z "${pcat}" ] && die "remove_blockdevices() \$pcat not given."
1061     [ -z "${pname}" ] && die "remove_blockdevices() \$pname not given."
1062     [ -z "${pver}" ] && die "remove_blockdevices() \$pver not given."
1063     [ -z "${pbuild}" ] && die "remove_blockdevices() \$pbuild not given."
1064 niro 226 pfull="${pcat}/${pname}-${pver}-${pbuild}"
1065    
1066     # check needed global vars
1067     [ -z "${BUILDDIR}" ] && die "remove_blockdevices() \$BUILDDIR not set."
1068    
1069     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.pipes ] && die "remove_blockdevices() .pipes not found"
1070    
1071     # sets fieldseperator to "§" instead of " "
1072     IFS=§
1073    
1074 niro 1209 while read pathto posix user group
1075 niro 226 do
1076     [ -z "${pathto}" ] && continue
1077    
1078 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< PIPE: ${MROOT}${pathto}"
1079 niro 226 rm "${MROOT}${pathto}"
1080     done < ${MROOT}${INSTALLDB}/${pfull}/.pipes
1081    
1082     # very important: unsetting the '§' fieldseperator
1083     IFS=$'\n'
1084     }
1085    
1086    
1087     ###################################################
1088     # function remove_characterdevices #
1089     # remove_characterdevices $PKGNAME #
1090     ###################################################
1091     remove_characterdevices()
1092     {
1093     local pathto
1094     local posix
1095 niro 1209 local user
1096     local group
1097 niro 226 local IFS
1098     local pcat
1099     local pname
1100     local pver
1101     local pbuild
1102     local i
1103     local pfull
1104    
1105     IFS=$'\n'
1106    
1107     # very basic getops
1108     for i in $*
1109     do
1110     case $1 in
1111     --pcat|-c) shift; pcat="$1" ;;
1112     --pname|-n) shift; pname="$1" ;;
1113     --pver|-v) shift; pver="$1" ;;
1114     --pbuild|-b) shift; pbuild="$1" ;;
1115     esac
1116     shift
1117     done
1118    
1119     # sanity checks; abort if not given
1120 niro 1209 [ -z "${pcat}" ] && die "remove_characterdevices() \$pcat not given."
1121     [ -z "${pname}" ] && die "remove_characterdevices() \$pname not given."
1122     [ -z "${pver}" ] && die "remove_characterdevices() \$pver not given."
1123     [ -z "${pbuild}" ] && die "remove_characterdevices() \$pbuild not given."
1124 niro 226 pfull="${pcat}/${pname}-${pver}-${pbuild}"
1125    
1126     # check needed global vars
1127     [ -z "${BUILDDIR}" ] && die "remove_characterdevices() \$BUILDDIR not set."
1128    
1129     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.char ] && die "remove_characterdevices() .char not found"
1130    
1131     # sets fieldseperator to "§" instead of " "
1132     IFS=§
1133    
1134 niro 1209 while read pathto posix user group
1135 niro 226 do
1136     [ -z "${pathto}" ] && continue
1137    
1138 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< CHAR: ${MROOT}${pathto}"
1139 niro 226 rm "${MROOT}${pathto}"
1140     done < ${MROOT}${INSTALLDB}/${pfull}/.char
1141    
1142     # very important: unsetting the '§' fieldseperator
1143     IFS=$'\n'
1144     }
1145    
1146    
1147     ###################################################
1148 niro 1209 # function remove_fifos #
1149     # remove_fifos $PKGNAME #
1150     ###################################################
1151     remove_fifos()
1152     {
1153     local pathto
1154     local posix
1155     local user
1156     local group
1157     local IFS
1158     local pcat
1159     local pname
1160     local pver
1161     local pbuild
1162     local i
1163     local pfull
1164    
1165     IFS=$'\n'
1166    
1167     # very basic getops
1168     for i in $*
1169     do
1170     case $1 in
1171     --pcat|-c) shift; pcat="$1" ;;
1172     --pname|-n) shift; pname="$1" ;;
1173     --pver|-v) shift; pver="$1" ;;
1174     --pbuild|-b) shift; pbuild="$1" ;;
1175     esac
1176     shift
1177     done
1178    
1179     # sanity checks; abort if not given
1180     [ -z "${pcat}" ] && die "remove_fifos() \$pcat not given."
1181     [ -z "${pname}" ] && die "remove_fifos() \$pname not given."
1182     [ -z "${pver}" ] && die "remove_fifos() \$pver not given."
1183     [ -z "${pbuild}" ] && die "remove_fifos() \$pbuild not given."
1184     pfull="${pcat}/${pname}-${pver}-${pbuild}"
1185    
1186     # check needed global vars
1187     [ -z "${BUILDDIR}" ] && die "remove_fifos() \$BUILDDIR not set."
1188    
1189 niro 1211 # make it optional atm !!
1190     #[ ! -f ${MROOT}${INSTALLDB}/${pfull}/.fifo ] && die "remove_fifos() .fifo not found"
1191     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.fifo ] && return
1192 niro 1209
1193     # sets fieldseperator to "§" instead of " "
1194     IFS=§
1195    
1196     while read pathto posix user group
1197     do
1198     [ -z "${pathto}" ] && continue
1199    
1200 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< FIFO: ${MROOT}${pathto}"
1201 niro 1209 rm "${MROOT}${pathto}"
1202     done < ${MROOT}${INSTALLDB}/${pfull}/.fifo
1203    
1204     # very important: unsetting the '§' fieldseperator
1205     IFS=$'\n'
1206     }
1207    
1208    
1209     ###################################################
1210 niro 226 # function remove_direcories #
1211     # remove_direcories $PKGNAME #
1212     ###################################################
1213     remove_directories()
1214     {
1215     local pathto
1216     local posix
1217     local IFS
1218     local pcat
1219     local pname
1220     local pver
1221     local pbuild
1222     local i
1223     local pfull
1224    
1225     IFS=$'\n'
1226    
1227     # very basic getops
1228     for i in $*
1229     do
1230     case $1 in
1231     --pcat|-c) shift; pcat="$1" ;;
1232     --pname|-n) shift; pname="$1" ;;
1233     --pver|-v) shift; pver="$1" ;;
1234     --pbuild|-b) shift; pbuild="$1" ;;
1235     esac
1236     shift
1237     done
1238    
1239     # sanity checks; abort if not given
1240 niro 1209 [ -z "${pcat}" ] && die "remove_directories() \$pcat not given."
1241     [ -z "${pname}" ] && die "remove_directories() \$pname not given."
1242     [ -z "${pver}" ] && die "remove_directories() \$pver not given."
1243     [ -z "${pbuild}" ] && die "remove_directories() \$pbuild not given."
1244 niro 226 pfull="${pcat}/${pname}-${pver}-${pbuild}"
1245    
1246     # check needed global vars
1247     [ -z "${BUILDDIR}" ] && die "remove_directories() \$BUILDDIR not set."
1248    
1249     [ ! -f ${MROOT}${INSTALLDB}/${pfull}/.char ] && die "remove_directories() .dirs not found"
1250    
1251     # sets fieldseperator to "§" instead of " "
1252     IFS=§
1253    
1254 niro 240 # reversed order is mandatory !
1255     tac ${MROOT}${INSTALLDB}/${pfull}/.dirs | while read pathto posix
1256 niro 226 do
1257     [ -z "${pathto}" ] && continue
1258    
1259     if [ ! -d "${MROOT}${pathto}" ]
1260     then
1261 niro 1584 mqueryfeature "verbose" && \
1262 niro 226 echo -e "${COLRED}! exist${COLDEFAULT} === DIR: ${MROOT}${pathto}"
1263     continue
1264     fi
1265    
1266     # exclude .keep directories
1267     if [ -f "${MROOT}${pathto}/.keep" ]
1268     then
1269 niro 1584 mqueryfeature "verbose" && \
1270 niro 240 echo -e "${COLRED}! .keep${COLDEFAULT} === DIR: ${MROOT}${pathto}"
1271 niro 226 continue
1272     fi
1273    
1274     # monitors /etc/env.d -> env-rebuild
1275     [[ ${pathto} = /etc/env.d ]] && export MAGE_ENV_REBUILD=true
1276    
1277     # monitors /usr/share/info -> info-rebuild
1278     [[ ${pathto} = /usr/share/info ]] && export MAGE_INFO_REBUILD=true
1279    
1280     if rmdir "${MROOT}${pathto}" &> /dev/null
1281     then
1282 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< DIR: ${MROOT}${pathto}"
1283 niro 226 else
1284 niro 1584 mqueryfeature "verbose" && \
1285 niro 226 echo -e "${COLRED}! empty${COLDEFAULT} === DIR: ${MROOT}${pathto}"
1286     fi
1287 niro 240 done
1288 niro 226
1289     # very important: unsetting the '§' fieldseperator
1290     IFS=$'\n'
1291     }
1292    
1293    
1294     ###################################################
1295     # function build_douninstall #
1296     # build_douninstall $PKGNAME #
1297 niro 1209 # NOTE: this is an wrapper to remove packages #
1298 niro 226 ###################################################
1299     build_douninstall()
1300     {
1301     local pcat
1302     local pname
1303     local pver
1304     local pbuild
1305     local i
1306    
1307     # very basic getops
1308     for i in $*
1309     do
1310     case $1 in
1311     --pcat|-c) shift; pcat="$1" ;;
1312     --pname|-n) shift; pname="$1" ;;
1313     --pver|-v) shift; pver="$1" ;;
1314     --pbuild|-b) shift; pbuild="$1" ;;
1315     esac
1316     shift
1317     done
1318    
1319     # sanity checks; abort if not given
1320     [ -z "${pcat}" ] && die "build_douninstall() \$pcat not given."
1321     [ -z "${pname}" ] && die "build_douninstall() \$pname not given."
1322     [ -z "${pver}" ] && die "build_douninstall() \$pver not given."
1323     [ -z "${pbuild}" ] && die "build_douninstall() \$pbuild not given."
1324    
1325     # this is only a wrapper
1326    
1327     # NOTE:
1328     # !! we use § as field seperator !!
1329     # doing so prevent us to get errors by filenames with spaces
1330    
1331 niro 1209 for i in symlinks files blockdevices characterdevices directories fifos
1332 niro 226 do
1333     remove_${i} \
1334     --pcat "${pcat}" \
1335     --pname "${pname}" \
1336     --pver "${pver}" \
1337     --pbuild "${pbuild}" \
1338     || die "remove ${i} ${pcat}/${pname}-${pver}-${pbuild}"
1339     done
1340     }
1341    
1342 niro 1549 # convertmirrors [uri]
1343     convertmirrors()
1344     {
1345     local uri="$1"
1346     local scheme
1347     local mirror
1348     local mirrors
1349     local addon
1350     local real_uri
1351     local output
1352    
1353     # needs
1354     [[ -z ${MIRRORS} ]] && die "convertmirrors(): no mirrors defined!"
1355     [[ -z ${SOURCEFORGE_MIRRORS} ]] && die "convertmirrors(): no sourceforge mirrors defined!"
1356     [[ -z ${GNU_MIRRORS} ]] && die "convertmirrors(): no gnu mirrors defined!"
1357     [[ -z ${GNOME_MIRRORS} ]] && die "convertmirrors(): no gnome mirrors defined!"
1358     [[ -z ${KDE_MIRRORS} ]] && die "convertmirrors(): no kde mirrors defined!"
1359    
1360     # check known uri schemes
1361     case ${uri} in
1362     http://*|https://*|ftp://*|ftps://*) mirrors="" ;;
1363     mirror://*) mirrors="${MIRRORS}"; scheme="mirror://"; addon="/sources" ;;
1364     package://*) mirrors="${MIRRORS}"; scheme="package://"; addon="/${PACKAGES_SERVER_PATH}" ;;
1365     gnu://*) mirrors="${GNU_MIRRORS}"; scheme="gnu://" ;;
1366     sourceforge://*) mirrors="${SOURCEFORGE_MIRRORS}"; scheme="sourceforge://" ;;
1367     gnome://*) mirrors="${GNOME_MIRRORS}"; scheme="gnome://" ;;
1368     kde://*) mirrors="${KDE_MIRRORS}"; scheme="kde://" ;;
1369     *) die "convertmirror(): unsupported uri scheme in '${uri}'!" ;;
1370     esac
1371    
1372     if [[ ! -z ${mirrors} ]]
1373     then
1374     for mirror in ${mirrors}
1375     do
1376     # add a whitespace to the output
1377     [[ -z ${output} ]] || output+=" "
1378     output+="${mirror}${addon}/${uri/${scheme}/}"
1379     done
1380     else
1381 niro 2270 output="${uri}"
1382 niro 1549 fi
1383    
1384     echo "${output}"
1385     }
1386    
1387     mdownload()
1388     {
1389     local i
1390     local uri
1391     local real_uris
1392     local mirror
1393     local outputfile
1394     local outputdir
1395     local retval
1396     local wget_opts
1397    
1398     # very basic getops
1399     for i in $*
1400     do
1401     case $1 in
1402     --uri|-u) shift; uri="$1" ;;
1403     --dir|-d) shift; outputdir="$1" ;;
1404     esac
1405     shift
1406     done
1407    
1408     # sanity checks; abort if not given
1409     [[ -z ${uri} ]] && die "mdownload(): no uri given!"
1410     [[ -z ${outputdir} ]] && die "mdownload(): no dir given!"
1411    
1412     # convert mirrored uris to the real ones
1413     real_uris="$(convertmirrors ${uri})"
1414    
1415     # verbose or not
1416 niro 1626 mqueryfeature "!verbose" && wget_opts+=" --quiet"
1417 niro 1549
1418     # filter wget options if busybox was found
1419 niro 1626 wget_opts+=" $(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})"
1420 niro 1549
1421     # create outputdir
1422     [[ ! -d ${outputdir} ]] && install -d "${outputdir}"
1423    
1424     for mirror in ${real_uris}
1425     do
1426     # get the name of the output file
1427     outputfile="${mirror##*/}"
1428    
1429     wget ${wget_opts} --output-document="${outputdir}/${outputfile}" "${mirror}"
1430     retval="$?"
1431     if [[ ${retval} = 0 ]]
1432     then
1433     break
1434     else
1435     continue
1436     fi
1437     done
1438    
1439     # return wget retval
1440     return "${retval}"
1441     }
1442    
1443 niro 226 # fetch_packages /path/to/mage/file1 /path/to/mage/file2
1444     fetch_packages()
1445     {
1446 niro 1549 local i
1447 niro 226 local list="$@"
1448 niro 2271 local pkgname
1449     local pkgfile
1450 niro 2272 local pcat
1451     local pname
1452 niro 226 local mirr
1453     local magefile
1454     local md5file
1455     local opt
1456     local count_current
1457     local count_total
1458 niro 1273 local wget_opts
1459 niro 2272 local fetching
1460 niro 226
1461 niro 419 [ -z "${MIRRORS}" ] && die "You have no mirrors defined. Please edit your ${MAGERC}."
1462 niro 226
1463 niro 1273 # filter wget command if busybox was found
1464     wget_opts="$(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})"
1465    
1466 niro 226 # get count of total packages
1467     declare -i count_current=0
1468     declare -i count_total=0
1469    
1470     for i in ${list}; do (( count_total++ )); done
1471    
1472     for magefile in ${list}
1473     do
1474 niro 2271 pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
1475     pkgfile="${pkgname}.${PKGSUFFIX}"
1476 niro 226 pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
1477    
1478 niro 2272 pcat=$(magename2pcat ${magefile})
1479     pname=$(magename2pname ${magefile})
1480     md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
1481    
1482 niro 226 (( count_current++ ))
1483 niro 2271 xtitle "[ (${count_current}/${count_total}) Fetching ${pkgfile} ]"
1484 niro 226
1485     # abort on virtual pkg
1486     if [[ ${pkgtype} = virtual ]]
1487     then
1488     echo -ne " ${COLBLUE}---${COLDEFAULT}"
1489 niro 2271 echo " !fetch virtual (${count_current}/${count_total}): ${pkgname} ... "
1490 niro 226 continue
1491     fi
1492    
1493     # abort on sources pkg
1494     if [[ ${pkgtype} = sources ]]
1495     then
1496     echo -ne " ${COLBLUE}---${COLDEFAULT}"
1497 niro 2271 echo " !fetch sources (${count_current}/${count_total}): ${pkgname} ... "
1498 niro 226 continue
1499     fi
1500    
1501 niro 2272 # check if FETCHING is required
1502     if [ ! -f "${md5file}" ]
1503 niro 226 then
1504 niro 2272 fetching=true
1505     else
1506     if mchecksum --rundir "${PKGDIR}" --file "${md5file}" --method md5 &> /dev/null
1507     then
1508     # md5's ok, no fetching required
1509     fetching=false
1510     else
1511     fetching=true
1512     fi
1513     fi
1514    
1515     if [[ ${fetching} = false ]]
1516     then
1517 niro 226 echo -ne " ${COLBLUE}***${COLDEFAULT}"
1518 niro 2271 echo " fetch complete (${count_current}/${count_total}): ${pkgfile} ... "
1519 niro 226 continue
1520 niro 2272 else
1521     echo -ne " ${COLBLUE}***${COLDEFAULT}"
1522     echo -e " fetching (${count_current}/${count_total}): ${pkgfile} ... "
1523     mdownload --uri "package://${pkgfile}" --dir "${PKGDIR}" || die "Could not download ${pkgfile}"
1524 niro 226 fi
1525    
1526 niro 2272 # sanity check, not really needed but to be sure
1527 niro 2271 if [ ! -f ${PKGDIR}/${pkgfile} ]
1528 niro 226 then
1529 niro 2271 die "Package '${pkgfile}' after download not found in '${PKGDIR}'"
1530 niro 226 fi
1531     done
1532    
1533     # add a crlf for a better view
1534     if [ ${count_total} -gt 1 ]; then echo; fi
1535     }
1536    
1537     syncmage()
1538     {
1539     if [ -z "${RSYNC}" ]
1540     then
1541 niro 419 die "You have no rsync-mirrors defined. Please edit your ${MAGERC}."
1542 niro 226 fi
1543    
1544     local i
1545     for i in ${RSYNC}
1546     do
1547 niro 386 rsync ${RSYNC_FETCH_OPTIONS} ${i} ${MAGEDIR}
1548 niro 226 if [[ $? = 0 ]]
1549     then
1550     break
1551     else
1552     continue
1553     fi
1554     done
1555    
1556     # clean up backup files (foo~)
1557 niro 1438 find ${MAGEDIR} -name \*~ -exec rm '{}' ';'
1558 niro 226
1559 niro 966 # check if a newer mage version is available
1560 niro 226 is_newer_mage_version_available
1561     }
1562    
1563 niro 739 syncmage_tarball()
1564     {
1565     local latest_tarball
1566 niro 1083 local latest_md5
1567 niro 739 local temp="$(mktemp -d)"
1568     local mirr mymirr
1569 niro 1087 local opt
1570 niro 1273 local tar_opts
1571 niro 1293 local wget_opts
1572 niro 739
1573 niro 1083 # try to get the md5 marked as latest on the server
1574     latest_md5="mage-latest.md5"
1575    
1576 niro 739 # try to get the tarball marked as latest on the server
1577     latest_tarball="mage-latest.tar.bz2"
1578    
1579 niro 1293 # filter wget command if busybox was found
1580     wget_opts="$(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})"
1581    
1582 niro 739 for mirr in ${MIRRORS}
1583     do
1584 niro 1675 # path without distribution
1585     # (only for stable|testing|unstable and not DISTROTAG)
1586     case ${mirr##*/} in
1587     stable|testing|unstable) mymirr="${mirr%/*}";;
1588     *) mymirr="${mirr}";;
1589     esac
1590 niro 739
1591     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1592 niro 1083 echo "fetching latest md5 from ${mymirr} ..."
1593 niro 1584 mqueryfeature "!verbose" && opt="--quiet"
1594 niro 1083 wget \
1595 niro 1293 ${wget_opts} \
1596 niro 1083 --directory-prefix=${temp} \
1597 niro 1087 ${opt} ${mymirr}/rsync/tarballs/${latest_md5}
1598 niro 1083
1599     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1600 niro 739 echo "fetching latest tarball from ${mymirr} ..."
1601     wget \
1602 niro 1293 ${wget_opts} \
1603 niro 739 --directory-prefix=${temp} \
1604 niro 1087 ${opt} ${mymirr}/rsync/tarballs/${latest_tarball}
1605 niro 739 if [[ $? = 0 ]]
1606     then
1607     break
1608     else
1609     continue
1610     fi
1611     done
1612    
1613     if [[ -f ${temp}/${latest_tarball} ]]
1614     then
1615 niro 1083 # check md5
1616     if [[ ! -f ${temp}/${latest_md5} ]]
1617     then
1618     die "md5 is missing ... aborting"
1619     else
1620 niro 1087 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1621     echo -n "checking md5sum... "
1622 niro 1652 mchecksum --rundir "${temp}" --file "${latest_md5}" --method md5 || die "md5 for ${latest_tarball} failed"
1623 niro 1083 fi
1624    
1625 niro 739 if [[ -d ${MAGEDIR} ]]
1626     then
1627     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1628     echo "cleaning old mage-tree ${MAGEDIR}..."
1629 niro 1654 # honor mountpoints and empty dirs
1630     if mountpoint -q ${MAGEDIR}
1631     then
1632     if ! mcheckemptydir ${MAGEDIR}
1633     then
1634 niro 1963 find ${MAGEDIR} -mindepth 1 -maxdepth 1 | xargs --no-run-if-empty rm -r
1635 niro 1654 fi
1636     else
1637     rm -rf ${MAGEDIR}
1638     fi
1639 niro 739 fi
1640    
1641 niro 1273 if need_busybox_support tar
1642     then
1643     tar_opts="xjf"
1644     else
1645     tar_opts="xjmf"
1646     fi
1647    
1648 niro 739 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1649     echo "updating mage-tree from tarball ..."
1650     # unpack in dirname of MAGEDIR, as the tarball has already the mage
1651 niro 1273 tar ${tar_opts} ${temp}/${latest_tarball} -C ${MAGEDIR%/*} || die "Unpacking tarball"
1652 niro 739
1653     if [[ -d ${temp} ]]
1654     then
1655     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1656 niro 972 echo "cleaning temp-files ..."
1657 niro 739 rm -rf ${temp}
1658     fi
1659 niro 966
1660     # check if a newer mage version is available
1661     is_newer_mage_version_available
1662 niro 739 else
1663     die "Could not fetch the latest tarball ... aborting"
1664     fi
1665     }
1666    
1667 niro 226 cleanpkg()
1668     {
1669     if [ -d "${PKGDIR}" ]
1670     then
1671     echo -n "Removing downloaded packages... "
1672     rm -rf ${PKGDIR}/*
1673     echo "done."
1674     fi
1675     }
1676    
1677 niro 1650 # unused?
1678     #
1679     # # cuts full pathnames or versionized names down to basename
1680     # choppkgname()
1681     # {
1682     # #we want this only if full name was used
1683     # if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ]
1684     # then
1685     # #cuts ARCH and PBUILD
1686     # #ARCH comes from ${MAGERC}
1687     # MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}$(print_distrotag)-r*.::g")
1688     #
1689     # #cuts version number
1690     # MAGENAME=$(basename ${MAGENAME%-*} .mage)
1691     # fi
1692     # }
1693 niro 226
1694    
1695     # get_categorie $PNAME, returns CATEGORIE
1696     # $1=pname
1697     # ret 0=ok, 1=not_found
1698     pname2pcat()
1699     {
1700     local pname="$1"
1701     local repo="$2"
1702     local pcat
1703     local categorie
1704    
1705     for pcat in ${MAGEDIR}/*
1706     do
1707     if [ -d ${pcat}/${pname} ]
1708     then
1709     categorie=$(basename ${pcat})
1710     fi
1711     done
1712    
1713     echo "${categorie}"
1714     }
1715    
1716     # check_stable_package /path/to/foo.mage
1717     # returns 0=stable 1=unstable
1718     check_stable_package()
1719     {
1720 niro 370 # first check if this magefile is not blacklisted
1721     blacklisted "$1" || return 1
1722    
1723 niro 226 local STATE
1724     STATE="$(get_value_from_magefile STATE "$1")"
1725    
1726     # state testing
1727 niro 286 if [[ ${USE_TESTING} = true ]] || [[ ${MAGE_DISTRIBUTION} = testing ]]
1728 niro 226 then
1729     case ${STATE} in
1730     testing|stable) return 0 ;;
1731     *) return 1 ;;
1732     esac
1733     fi
1734    
1735     # state unstable
1736 niro 286 if [[ ${USE_UNSTABLE} = true ]] || [[ ${MAGE_DISTRIBUTION} = unstable ]]
1737 niro 226 then
1738     case ${STATE} in
1739     unstable|testing|stable) return 0 ;;
1740     *) return 1 ;;
1741     esac
1742     fi
1743    
1744     # no use_state given = stable
1745     case ${STATE} in
1746     stable) return 0 ;;
1747     *) return 1 ;;
1748     esac
1749     }
1750    
1751    
1752     # get_highest_magefile ${PCAT} ${PNAME}
1753     # fake at moment returns only stable pkgs (must set to be one)
1754     # return $HIGHEST_MAGEFILE
1755     get_highest_magefile()
1756     {
1757     local HIGHEST_MAGEFILE
1758     local PCAT="$1"
1759     local PNAME="$2"
1760     local magefile
1761    
1762 niro 676 # do not list the content of a directory, only the name (-d)
1763 niro 1438 for magefile in $(ls --format=single-column -v -d ${MAGEDIR}/${PCAT}/${PNAME}/* 2> /dev/null)
1764 niro 226 do
1765 niro 676 [[ -z ${magefile} ]] && continue
1766 niro 226 # we exclude subdirs (for stuff like a md5sum dir)
1767 niro 676 [[ -d ${magefile} ]] && continue
1768 niro 226 if check_stable_package ${magefile}
1769     then
1770     HIGHEST_MAGEFILE=${magefile}
1771     #for debug only
1772 niro 2224 mqueryfeature "debug" && echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}" >&2
1773 niro 226 fi
1774     done
1775    
1776     echo "${HIGHEST_MAGEFILE}"
1777     return 0
1778     }
1779    
1780    
1781     ###################################################
1782     # function is_config_protected #
1783     # is_config_protected /path/to/file #
1784     # #
1785     # returns: #
1786     # 0 - not protected #
1787     # 1 - error #
1788     # 2 - protected #
1789     # 3 - protected but masked #
1790 niro 942 # 4 - protected but ignored #
1791 niro 226 # #
1792     ###################################################
1793     is_config_protected()
1794     {
1795     local EXPFILE
1796     local TEST
1797     local PROTECTED
1798     local IFS
1799 niro 942 local i
1800     local x
1801 niro 226
1802     EXPFILE="${MROOT}$1"
1803    
1804     # file does not exist; it can be written
1805 niro 451 [[ ! -e ${EXPFILE} ]] && return 0
1806 niro 226
1807     # to be safe; it may be '§'
1808     IFS=' '
1809    
1810 niro 942 # check if config protected
1811 niro 226 for i in ${CONFIG_PROTECT}
1812     do
1813 niro 942 # only replace $i in the beginning of the variable
1814 niro 226 TEST="${EXPFILE/#${MROOT}${i}/Protected}"
1815 niro 451 if [[ ${TEST} != ${EXPFILE} ]]
1816 niro 226 then
1817 niro 942 # file is config proteced
1818 niro 226 PROTECTED=TRUE
1819    
1820 niro 942 # check if not masked
1821 niro 226 for x in ${CONFIG_PROTECT_MASK}
1822     do
1823     TEST="${EXPFILE/#${MROOT}${x}/Protect_Masked}"
1824 niro 451 if [[ ${TEST} != ${EXPFILE} ]]
1825 niro 226 then
1826     PROTECTED=MASKED
1827     fi
1828     done
1829 niro 942
1830     # check if not ignored
1831     for x in ${CONFIG_PROTECT_IGNORE}
1832     do
1833     TEST="${EXPFILE/#${MROOT}${x}/Protect_Ignored}"
1834     if [[ ${TEST} != ${EXPFILE} ]]
1835     then
1836     PROTECTED=IGNORED
1837     fi
1838     done
1839 niro 226 fi
1840     done
1841    
1842     unset IFS
1843    
1844     case ${PROTECTED} in
1845     TRUE)
1846     #echo "I'm protected"
1847     return 2
1848     ;;
1849     MASKED)
1850     #echo "I'm protected, but masked - delete me"
1851     return 3
1852     ;;
1853 niro 942 IGNORED)
1854     #echo "I'm protected, but ignored - keep me, del update"
1855     return 4
1856     ;;
1857 niro 226 *)
1858     #echo "delete me"
1859     return 0
1860     ;;
1861     esac
1862     }
1863    
1864    
1865     ###################################################
1866     # function count_protected_files #
1867     # count_protected_files /path/to/file #
1868     # #
1869     # note: prints number of protected files #
1870     # exp: 0012 #
1871     ###################################################
1872     count_protected_files()
1873     {
1874 niro 603 local file="$1"
1875     local dirname="${file%/*}"
1876     local filename="${file##*/}"
1877     local count
1878     local output
1879 niro 1758 local oldprotected
1880 niro 603 local i
1881 niro 1758 local x
1882 niro 603
1883 niro 1758 # hack; do not honor a global set IFS like '§'
1884     local IFS
1885 niro 603
1886 niro 1758 count=0
1887    
1888 niro 603 # check if there are already protected files
1889 niro 1758 for oldprotected in $(find ${dirname} -iname "._cfg????_${filename}" |
1890 niro 603 sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" |
1891     sort -t'%' -k3 -k2 | cut -f1 -d'%')
1892     do
1893 niro 1758 count="$(echo ${oldprotected} | sed 's:.*\/._cfg\(.*\)_.*:\1:')"
1894 niro 603 done
1895    
1896 niro 1962 # convert 0001 -> 1; 0120 -> 120 etc
1897     # use bash internal base functions to this task
1898     x="$((10#${count}))"
1899 niro 1758 for (( i=0; i<x; i++ ))
1900     do
1901     if [[ ${count:${i}:1} != 0 ]]
1902     then
1903     count="${count:${i}}"
1904     break
1905     fi
1906     done
1907    
1908 niro 1762 count="$(( ${count}+1 ))"
1909 niro 1758
1910 niro 603 # fill output up with zeros
1911     for (( i=${#count}; i < 4; i++ )); do output="${output}0"; done
1912     output="${output}${count}"
1913    
1914     echo "${output}"
1915 niro 226 }
1916    
1917     # call with
1918     # 'get_uninstall_candidates (--pcat cat --protected pcat/pfull) --pname PNAME'
1919     # returns /path/to/magefile(s)
1920     get_uninstall_candidates()
1921     {
1922     local search_pname
1923     local pkg
1924     local pcat
1925     local pname
1926     local pver
1927     local pbuild
1928     local list
1929     local pcatdir
1930     local protected
1931 niro 449 local i
1932 niro 226
1933     # very basic getops
1934     for i in $*
1935     do
1936     case $1 in
1937     --pcat|-c) shift; pcatdir="$1" ;;
1938     --pname|-n) shift; search_pname="$1" ;;
1939     --protected|-p) shift; protected="$1" ;;
1940     esac
1941     shift
1942     done
1943    
1944 niro 329 # it's not good to complain here about empty pnames; better to continue later anyway
1945     # # sanity checks; abort if not given
1946     # [ -z "${search_pname}" ] && die "get_uninstall_candidates() \$search_pname not given."
1947 niro 226
1948    
1949     # check needed global vars
1950     [ -z "${INSTALLDB}" ] && die "get_uninstall_candidates() \$INSTALLDB not set."
1951    
1952     # set pcatdir to '*' if empty
1953 niro 329 [ -z "${pcatdir}" ] && pcatdir='*'
1954 niro 226
1955     for pkg in ${MROOT}${INSTALLDB}/${pcatdir}/*
1956     do
1957     # abort if not a dir
1958     [ ! -d ${pkg} ] && continue
1959    
1960     pname="$(magename2pname ${pkg})"
1961    
1962     if [[ ${search_pname} = ${pname} ]]
1963     then
1964     pcat="$(magename2pcat ${pkg} installdb)"
1965     pver="$(magename2pver ${pkg})"
1966     pbuild="$(magename2pbuild ${pkg})"
1967    
1968     # exclude proteced
1969     [[ ${protected} = ${pcat}/${pname}-${pver}-${pbuild} ]] && continue
1970    
1971     list="${list} ${pcat}/${pname}-${pver}-${pbuild}"
1972     fi
1973     done
1974    
1975     echo "${list}"
1976     }
1977    
1978     # reads virtualdb file
1979     #$1 = virtualname; $2 commands: showpkgs, showline
1980     #return 0 == installed -> shows installed pkg as well
1981     #return 1 == not installed
1982     virtuals_read()
1983     {
1984     local virtualname="$1"
1985     local command="$2"
1986     local virtline
1987     local line x i
1988    
1989     # parse file to get virtual_name line
1990     IFS=$'\n'
1991     for line in $(< ${MROOT}${VIRTUALDB_FILE})
1992     do
1993     IFS=$' '
1994     for x in ${line}
1995     do
1996     if [[ ${x} = ${virtualname} ]]
1997     then
1998     virtline="${line}"
1999     [[ ${command} = showline ]] && echo "${line}"
2000     fi
2001     done
2002     IFS=$'\n'
2003     done
2004    
2005     unset IFS
2006    
2007     # now read the packages linked to VIRTUAL_NAME and output them
2008     if [ -n "${virtline}" ]
2009     then
2010     if [[ ${command} = showpkgs ]]
2011     then
2012     declare -i x=0
2013     for i in ${virtline}
2014     do
2015     if [ ${x} -ge 1 ]
2016     then
2017     echo "${i}"
2018     fi
2019     ((x++))
2020     done
2021     fi
2022     return 0
2023     fi
2024     return 1
2025     }
2026    
2027    
2028     #add pkg to virtualdb
2029     # $1 == virtualname $2= pkgname
2030     # retvals: 0=ok,added; 1=error; 3=pkg already in virtual
2031     virtuals_add()
2032     {
2033     local virtualname="$1"
2034     local pkgname="$2"
2035     local oldline
2036     local line i
2037     local installed_file
2038 niro 273 local OLDIFS
2039 niro 226
2040     if virtuals_read ${virtualname}
2041     then
2042 niro 329 # make sure ${PKG_NAME} is *not* in ${VIRTUAL_NAME} already
2043 niro 226 for i in $(virtuals_read ${virtualname} showpkgs)
2044     do
2045     if [[ ${i} = ${pkgname} ]]
2046     then
2047     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2048     echo "${pkgname} already linked as ${virtualname} ..."
2049     #return 3
2050     return 0
2051     fi
2052     done
2053    
2054     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2055     echo "updating ${virtualname} entry with ${pkgname} ..."
2056     oldline="$(virtuals_read ${virtualname} showline)"
2057    
2058     # make a backup
2059     mv ${MROOT}${VIRTUALDB_FILE} ${MROOT}${VIRTUALDB_FILE}.old
2060    
2061 niro 273 OLDIFS="${IFS}"
2062 niro 226 IFS=$'\n'
2063     for line in $(< ${MROOT}${VIRTUALDB_FILE}.old)
2064     do
2065     # if the right line, append ${pkgname}, else do nothing
2066     if [[ ${line} = ${oldline} ]]
2067     then
2068     echo "${line} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
2069     else
2070     echo "${line}" >> ${MROOT}${VIRTUALDB_FILE}
2071     fi
2072     done
2073 niro 273 # unset IFS
2074     IFS="${OLDIFS}"
2075 niro 226 else
2076 niro 273 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2077 niro 226 echo "register ${pkgname} as ${virtualname} ..."
2078     echo "${virtualname} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
2079     fi
2080    
2081     return 0
2082     }
2083    
2084     #deletes pakages from virtual database
2085     #$1 virtualname; $2 pkgname
2086 niro 1209 virtuals_del()
2087     {
2088 niro 226
2089 niro 273 local virtualname="$1"
2090     local pkgname="$2"
2091     local oldline
2092     local method
2093     local line i x
2094     local pkg_installed
2095     local OLDIFS
2096    
2097     # first check if exists
2098     if virtuals_read ${virtualname}
2099 niro 226 then
2100 niro 273 # get method -> delall or update and check if ${PKG_NAME} exists in ${VIRTUAL_NAME}
2101 niro 226 declare -i x=0
2102 niro 273 for i in $(virtuals_read ${virtualname} showpkgs)
2103 niro 226 do
2104 niro 273 if [[ ${i} = ${pkgname} ]]
2105 niro 226 then
2106 niro 273 pkg_installed=true
2107 niro 226 fi
2108     ((x++))
2109     done
2110 niro 273
2111     # abort if not installed
2112     if [[ ${pkg_installed} != true ]]
2113 niro 226 then
2114 niro 273 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2115     echo "${pkgname} does not exists in ${virtualname}."
2116 niro 226 return 0
2117     fi
2118 niro 273
2119 niro 226 if [ ${x} -ge 2 ]
2120     then
2121 niro 273 method=update
2122 niro 226 else
2123 niro 273 method=delall
2124 niro 226 fi
2125 niro 273
2126     # get the complete line
2127     oldline="$(virtuals_read ${virtualname} showline)"
2128    
2129     # make a backup of the db
2130 niro 226 mv ${VIRTUALDB_FILE} ${VIRTUALDB_FILE}.old
2131 niro 273
2132     # parse virtualdb
2133     OLDIFS="${IFS}"
2134 niro 226 IFS=$'\n'
2135     for line in $(< ${VIRTUALDB_FILE}.old)
2136     do
2137 niro 273 if [[ ${line} = ${oldline} ]]
2138 niro 226 then
2139     #delall or update?
2140 niro 273 case ${method} in
2141 niro 226 update)
2142 niro 273 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2143     echo "Unlinking ${pkgname} from ${virtualname} in virtual database ..."
2144     # del PKG_NAME from line
2145     echo "${line/ ${pkgname}/}" >> ${VIRTUALDB_FILE}
2146 niro 226 ;;
2147     delall)
2148 niro 273 echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2149     echo "Deleting ${virtualname} in virtual database ..."
2150     # continue; do not write anything
2151 niro 226 continue
2152     ;;
2153     esac
2154     else
2155     echo "${line}" >> ${VIRTUALDB_FILE}
2156     fi
2157     done
2158 niro 273 # unset IFS
2159     IFS="${OLDIFS}"
2160 niro 226 else
2161 niro 273 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2162     echo "${virtualname} does not exists in virtual database."
2163 niro 226 fi
2164     }
2165    
2166     # gets real pkgname from virtuals.default
2167     #$1=VIRTUAL_NAME; returns PKG_NAME
2168     default_virtualname_to_pkgname()
2169     {
2170     local VIRTUAL_NAME PKG_NAME db_virtualname db_pkgname
2171    
2172     VIRTUAL_NAME=$1
2173    
2174     while read db_virtualname db_pkgname
2175     do
2176     if [ "${db_virtualname}" == "${VIRTUAL_NAME}" ]
2177     then
2178     PKG_NAME="${db_pkgname}"
2179     fi
2180     done << EOF
2181     $(< ${VIRTUALDB_DEFAULTS})
2182     EOF
2183    
2184     if [ -n "${PKG_NAME}" ]
2185     then
2186     echo "${PKG_NAME}"
2187     fi
2188     }
2189    
2190     minclude()
2191     {
2192     local i
2193    
2194 niro 437 if [[ -n $* ]]
2195 niro 226 then
2196 niro 437 for i in $*
2197 niro 226 do
2198 niro 1584 mqueryfeature "debug" && \
2199 niro 226 echo "--- Including ${MAGEDIR}/include/${i}.minc"
2200     source ${MAGEDIR}/include/${i}.minc
2201     done
2202 niro 1584 mqueryfeature "debug" && echo
2203 niro 226 fi
2204     }
2205    
2206     sminclude()
2207     {
2208     local i
2209    
2210 niro 437 if [[ -n $* ]]
2211 niro 226 then
2212 niro 437 for i in $*
2213 niro 226 do
2214 niro 2364 [[ ${SILENT} = 1 ]] || echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc"
2215 niro 226 source ${SMAGESCRIPTSDIR}/include/${i}.sminc
2216     done
2217 niro 2364 [[ ${SILENT} = 1 ]] || echo
2218 niro 226 fi
2219     }
2220    
2221     # checks if an newer mage version is available
2222     is_newer_mage_version_available()
2223     {
2224     local newest_mage
2225     local installed_mage
2226    
2227 niro 252 newest_mage="$(basename $(get_highest_magefile app-mage mage) .mage)"
2228 niro 226 installed_mage="$(magequery -n mage | cut -d' ' -f5)"
2229    
2230     if [[ ${newest_mage} > ${installed_mage} ]]
2231     then
2232     echo
2233     echo -en ${COLRED}"An update for your packetmanager is available. "${COLDEFAULT}
2234     echo -e ${COLBLUE}"[ ${newest_mage} ]"${COLDEFAULT}
2235     echo "It is recommened to install this newer version"
2236 niro 373 echo "or your current system installation may break."
2237 niro 226 echo
2238     echo -en "Please update mage by running "
2239     echo -e ${COLGREEN}"'mage install mage'"${COLDEFAULT}
2240     echo
2241     fi
2242     }
2243    
2244     # returns pname from pkgname
2245     # pkgname2pname $PKGNAME
2246     pkgname2pname()
2247     {
2248     local pname
2249    
2250     pname="${1%-*-*-*}"
2251     echo "${pname}"
2252     }
2253    
2254     # returns pver from pkgname
2255     # pkgname2pver $PKGNAME
2256     pkgname2pver()
2257     {
2258     local i pver
2259    
2260     i="${1/$(pkgname2pname $1)-/}"
2261     pver="${i%-*-*}"
2262     echo "${pver}"
2263     }
2264    
2265     # returns pbuild from pkgname
2266     # pkgname2pbuild $PKGNAME
2267     pkgname2pbuild()
2268     {
2269     local pbuild
2270    
2271     pbuild="${1##*-}"
2272     echo "${pbuild}"
2273     }
2274    
2275     # returns parch from pkgname
2276     # pkgname2parch $PKGNAME
2277     pkgname2parch()
2278     {
2279     local i x parch
2280    
2281     i="${1%-*-*}-"
2282     x="${1%-*}"
2283     parch="${x/${i}/}"
2284     echo "${parch}"
2285     }
2286    
2287     # returns pname from magename
2288     # magename2pname /PATH/TO/MAGE/FILE
2289     magename2pname()
2290     {
2291     local i pname
2292    
2293     i="$(basename $1 .mage)"
2294     pname="${i%-*-*}"
2295     echo "${pname}"
2296     }
2297    
2298     # returns pver from magename
2299     # magename2pver /PATH/TO/MAGE/FILE
2300     magename2pver()
2301     {
2302     local i pver
2303    
2304     i="$(basename $1 .mage)"
2305     i="${i/$(magename2pname $1)-/}"
2306     pver="${i%-*}"
2307     echo "${pver}"
2308     }
2309    
2310     # returns pbuild from magename
2311     # magename2pbuild /PATH/TO/MAGE/FILE
2312     magename2pbuild()
2313     {
2314     local i pbuild
2315    
2316     i="$(basename $1 .mage)"
2317     pbuild="${i##*-}"
2318     echo "${pbuild}"
2319     }
2320    
2321     # returns pcat from magename
2322     # magename2pcat /PATH/TO/MAGE/FILE
2323     magename2pcat()
2324     {
2325     local i pcat
2326    
2327     if [[ ${2} = installdb ]]
2328     then
2329     # go 1 dir back
2330     i="${1%/*}"
2331     else
2332     # go 2 dirs back
2333     i="${1%/*/*}"
2334     fi
2335    
2336     # get basename
2337     pcat="${i##*/}"
2338     echo "${pcat}"
2339     }
2340    
2341     # returns pcat from DEPEND (without operand ! PCAT/PNAME-VERSION)
2342     # dep2pcat DEPEND
2343     dep2pcat()
2344     {
2345     local pcat
2346    
2347     pcat="${1%/*}"
2348     echo "${pcat}"
2349     }
2350    
2351     # returns pname from DEPEND (without operand ! PCAT/PNAME-VERSION)
2352     # $2=virtual is used to resolv VDEPEND from virtual packages
2353     # dep2pcat DEPEND (virtual)
2354     dep2pname()
2355     {
2356     local pname
2357    
2358     pname="${1##*/}"
2359    
2360     # cut version only if not virtual or it will cut the name
2361     if [[ $(dep2pcat $1) != virtual ]] && \
2362     [[ $2 != virtual ]]
2363     then
2364     pname="${pname%-*}"
2365     fi
2366    
2367     echo "${pname}"
2368     }
2369    
2370     dep2highest_magefile()
2371     {
2372     local pcat
2373     local pname
2374     local magefile
2375     local installed_virtuals
2376    
2377     pcat="$(dep2pcat $1)"
2378     pname="$(dep2pname $1)"
2379    
2380     if [[ ${pcat} = virtual ]]
2381     then
2382     # first check if virtual is already installed
2383     installed_virtuals="$(virtuals_read ${pcat}/${pname} showpkgs)"
2384     if [ -n "${installed_virtuals}" ]
2385     then
2386     for vpkg in ${installed_virtuals}
2387     do
2388     realpkgname="${vpkg}"
2389     virtualpkgname="${pcat}/${pname}"
2390     pcat="$(dep2pcat ${realpkgname})"
2391     pname="$(dep2pname ${realpkgname} virtual)"
2392     done
2393     else
2394     # choose one from virtualdb defaults (virtuals.defaults)
2395     realpkgname="$(default_virtualname_to_pkgname ${pcat}/${pname})"
2396     virtualpkgname="${pcat}/${pname}"
2397     pcat="$(dep2pcat ${realpkgname})"
2398     pname="$(dep2pname ${realpkgname} virtual)"
2399     fi
2400     fi
2401    
2402     magefile="$(get_highest_magefile ${pcat} ${pname})"
2403     echo "${magefile}"
2404     }
2405    
2406     # is_installed ${PCAT}/${PNAME}-${PVER}-${PBUILD}
2407     is_installed()
2408     {
2409     local fullpkgname="$1"
2410    
2411     # return 0 if installed
2412     [ -d ${MROOT}${INSTALLDB}/${fullpkgname} ] && return 0
2413    
2414     return 1
2415     }
2416    
2417     install_packages()
2418     {
2419     local list="$@"
2420     local pkg
2421     local pcat
2422     local pname
2423     local pver
2424     local pbuild
2425     local total_pkgs
2426     local current_pkg
2427     local src_install
2428     local uninstall_list
2429    
2430     # check for --src-install
2431     if [[ $1 = --src-install ]]
2432     then
2433     # remove --src-install from list
2434     list=${list/--src-install/}
2435     # enable src-install
2436     src_install="--src-install"
2437     fi
2438    
2439     # reset MAGE_PROTECT_COUNTER
2440     declare -i MAGE_PROTECT_COUNTER=0
2441     export MAGE_PROTECT_COUNTER
2442    
2443     # get count of total packages
2444     declare -i total_pkgs=0
2445     declare -i current_pkg=0
2446     for i in ${list}; do (( total_pkgs++ )); done
2447    
2448     echo
2449    
2450     if [[ -n ${MROOT} ]]
2451     then
2452     echo -ne ${COLRED}
2453     echo "!! installing in MROOT=${MROOT}"
2454     echo -ne ${COLDEFAULT}
2455     echo
2456     fi
2457    
2458     for pkg in ${list}
2459     do
2460     (( current_pkg++ ))
2461     pcat=$(magename2pcat ${pkg})
2462     pname=$(magename2pname ${pkg})
2463     pver=$(magename2pver ${pkg})
2464     pbuild=$(magename2pbuild ${pkg})
2465    
2466     mage_install \
2467     --pcat ${pcat} \
2468     --pname ${pname} \
2469     --pver ${pver} \
2470     --pbuild ${pbuild} \
2471     --count-total ${total_pkgs} \
2472     --count-current ${current_pkg} \
2473     ${src_install}
2474    
2475     # check for allready installed packages and remove them
2476     # except the package we have installed
2477     uninstall_list="$(get_uninstall_candidates \
2478     --pcat "${pcat}" \
2479     --pname "${pname}" \
2480     --protected ${pcat}/${pname}-${pver}-${pbuild})"
2481    
2482     # uninstall all packges in uninstall_list if not empty
2483     if [ -n "${uninstall_list}" ]
2484     then
2485     echo
2486     uninstall_packages ${uninstall_list} \
2487     || die "install_packges() uninstalling not-needed."
2488     fi
2489    
2490     # crlf for better view in VERBOSE mode
2491     #if [[ ${VERBOSE} = on ]]; then echo; fi
2492     echo
2493     done
2494    
2495     #echo "DEBUG MAGE_PROTECT_COUNTER=${MAGE_PROTECT_COUNTER}"
2496     show_etc_update_mesg
2497     }
2498    
2499     # get_value_from_magefile VARIABLE
2500     # returns the content of this VAR
2501     get_value_from_magefile()
2502     {
2503     local var="$1"
2504     local magefile="$2"
2505     local value
2506    
2507 niro 370 [[ -z ${var} ]] && return 1
2508     [[ -z ${magefile} ]] && return 1
2509    
2510 niro 226 # local all possible vars of a mage file
2511     # to prevent bad issues
2512     local PKGNAME
2513     local STATE
2514     local DESCRIPTION
2515     local HOMEPAGE
2516     local DEPEND
2517     local SDEPEND
2518     local PROVIDE
2519     local PKGTYPE
2520 niro 943 local SPLIT_PACKAGE_BASE
2521 niro 226 local preinstall
2522     local postinstall
2523 niro 248 local preremove
2524     local postremove
2525 niro 226
2526     # sanity checks
2527     [ -f ${magefile} ] && source ${magefile} || \
2528     die "get_value_from_magefile: ${magefile} not found."
2529     [ -z "${var}" ] && die "get_value_from_magefile: \$var not given."
2530    
2531     source ${magefile}
2532     eval value=\$$(echo ${var})
2533     echo "${value}"
2534 niro 248
2535 niro 258 # unset these functions
2536     unset -f preinstall
2537     unset -f postinstall
2538     unset -f preremove
2539     unset -f postremove
2540 niro 226 }
2541    
2542     mage_install()
2543     {
2544     # local all possible vars of a mage file
2545     # to prevent bad issues
2546     local PKGNAME
2547     local STATE
2548     local DESCRIPTION
2549     local HOMEPAGE
2550     local DEPEND
2551     local SDEPEND
2552     local PROVIDE
2553     local PKGTYPE
2554     local preinstall
2555     local postinstall
2556 niro 248 local preremove
2557     local postremove
2558 niro 226
2559     local pcat
2560     local pname
2561     local pver
2562     local pbuild
2563     local count_total
2564     local count_current
2565     local magefile
2566     local src_install
2567 niro 876 local i
2568 niro 226
2569     # very basic getops
2570     for i in $*
2571     do
2572     case $1 in
2573     --pcat|-c) shift; pcat="$1" ;;
2574     --pname|-n) shift; pname="$1" ;;
2575     --pver|-v) shift; pver="$1" ;;
2576     --pbuild|-b) shift; pbuild="$1" ;;
2577     --count-total) shift; count_total="$1" ;;
2578     --count-current) shift; count_current="$1" ;;
2579     --src-install|-s) shift; src_install=true ;;
2580     esac
2581     shift
2582     done
2583    
2584     # sanity checks; abort if not given
2585     [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
2586     [ -z "${pname}" ] && die "mage_install() \$pname not given."
2587     [ -z "${pver}" ] && die "mage_install() \$pver not given."
2588     [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
2589    
2590     # check needed global vars
2591     [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
2592     [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
2593     [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
2594    
2595     xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
2596     echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2597     echo -n "installing (${count_current}/${count_total}): "
2598     echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2599     echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2600    
2601     magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
2602     source ${magefile}
2603    
2604     # abort on sources if no srcinstall
2605     if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
2606     then
2607     echo
2608     echo -e "This Package is a Source Package."
2609     echo
2610     echo -e "Only 'srcinstall' works with this type of packages"
2611     echo -en "If you have done a srcinstall before, "
2612     echo -e "you will find the files in /usr/src."
2613     echo
2614     exit 1
2615     fi
2616    
2617     ## preinstall scripts
2618     if [ -n "$(typeset -f preinstall)" ]
2619     then
2620     echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
2621     preinstall
2622     unset preinstall
2623     fi
2624    
2625     if [[ ${src_install} = true ]]
2626     then
2627     local smage2file
2628     # check needed global vars
2629     [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
2630     [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
2631     [ -z "${BINDIR}" ] && die "\$BINDIR not set."
2632    
2633     # build the package first
2634     if [[ ${MAGEDEBUG} = on ]]
2635     then
2636     echo M:${pname}
2637     echo V:${pver}
2638     echo B:${pbuild}
2639     fi
2640    
2641 niro 2365 if [[ -n ${SPLIT_PACKAGE_BASE} ]]
2642 niro 675 then
2643 niro 876 # basic svn compat
2644 niro 1502 if [[ -d ${SMAGESCRIPTSDIR}/.svn ]]
2645 niro 876 then
2646 niro 1502 for i in ${SMAGESCRIPTSDIR}/*/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2647 niro 943 do
2648     smage2file="${i}"
2649     done
2650     else
2651     smage2file=${SMAGESCRIPTSDIR}/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2652     fi
2653    
2654 niro 675 else
2655 niro 876 # basic svn compat
2656 niro 1502 if [[ -d ${SMAGESCRIPTSDIR}/.svn ]]
2657 niro 876 then
2658 niro 1502 for i in ${SMAGESCRIPTSDIR}/*/${pname}/${pname}-${pver}-${pbuild}.smage2
2659 niro 876 do
2660     smage2file="${i}"
2661     done
2662     else
2663 niro 943 smage2file=${SMAGESCRIPTSDIR}/${pname}/${pname}-${pver}-${pbuild}.smage2
2664 niro 876 fi
2665 niro 675 fi
2666 niro 943
2667 niro 226 if [ -f "${smage2file}" ]
2668     then
2669 niro 385 echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
2670 niro 226 smage2 ${smage2file} || die "compile failed"
2671     else
2672     echo
2673     echo "$(basename ${SMAGEFILE}) not found."
2674     echo "update your smage-tree and try it again."
2675     echo
2676     die
2677     fi
2678     fi
2679    
2680     if [[ ${PKGTYPE} != virtual ]] && \
2681     [[ ${PKGTYPE} != sources ]]
2682     then
2683 niro 2156 unpack_package "${magefile}"
2684 niro 385 echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
2685 niro 226 build_doinstall ${PKGNAME}
2686     fi
2687    
2688     ## postinstall scripts
2689     if [ -n "$(typeset -f postinstall)" ]
2690     then
2691     echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
2692     postinstall
2693     unset postinstall
2694     fi
2695    
2696     # install a database entry
2697     install_database_entry \
2698     --pcat "${pcat}" \
2699     --pname "${pname}" \
2700     --pver "${pver}" \
2701     --pbuild "${pbuild}" \
2702     --pkgname "${PKGNAME}" \
2703     --pkgtype "${PKGTYPE}" \
2704     || die "error in mage_install() running install_database_entry()."
2705    
2706     # remove the package dir now
2707     if [ -d ${BUILDDIR}/${PKGNAME} ]
2708     then
2709     rm -rf ${BUILDDIR}/${PKGNAME}
2710     fi
2711    
2712     # rebuilds toplevel info node
2713     if [[ ${MAGE_INFO_REBUILD} = true ]]
2714     then
2715     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2716     echo -n "rebuilding top-level info node ... "
2717     ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2718     > ${MROOT}/usr/share/info/dir && \
2719     echo "done." || echo "failure."
2720     unset MAGE_INFO_REBUILD
2721     fi
2722    
2723     # rebuilds the enviroment with the content of /etc/env.d
2724     if [[ ${MAGE_ENV_REBUILD} = true ]]
2725     then
2726     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2727     echo -n "rebuilding environment ... "
2728 niro 2606 ${MLIBDIR}/env-rebuild > /dev/null && \
2729 niro 226 echo "done." || echo "failure."
2730     unset MAGE_ENV_REBUILD
2731     fi
2732    
2733     xtitleclean
2734    
2735     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2736     echo -n "package "
2737     # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2738     # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2739     echo "successfully installed."
2740 niro 248
2741 niro 258 # unset these functions
2742     unset -f preinstall
2743     unset -f postinstall
2744     unset -f preremove
2745     unset -f postremove
2746 niro 226 }
2747    
2748     md5sum_packages()
2749     {
2750     local list="$@"
2751     local magefile
2752     local pcat
2753     local pname
2754     local pkgname
2755     local pkgfile
2756     local pkgtype
2757     local count_current
2758     local count_total
2759    
2760     # get count of total packages
2761     declare -i count_current=0
2762     declare -i count_total=0
2763    
2764     for i in ${list}; do (( count_total++ )); done
2765    
2766     for magefile in ${list}
2767     do
2768     pcat=$(magename2pcat ${magefile})
2769     pname=$(magename2pname ${magefile})
2770     pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
2771     md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
2772 niro 2271 pkgfile="${pkgname}.${PKGSUFFIX}"
2773 niro 226 pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
2774    
2775     (( count_current++ ))
2776     xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
2777    
2778     # abort on virtual pkg
2779     if [[ ${pkgtype} = virtual ]]
2780     then
2781     echo -ne " ${COLBLUE}---${COLDEFAULT}"
2782 niro 2271 echo " !md5sum virtual (${count_current}/${count_total}): ${pkgname} ... "
2783 niro 226 continue
2784     fi
2785    
2786     # abort on sources pkg
2787     if [[ ${pkgtype} = sources ]]
2788     then
2789     echo -ne " ${COLBLUE}---${COLDEFAULT}"
2790 niro 2271 echo " !md5sum sources (${count_current}/${count_total}): ${pkgname} ... "
2791 niro 226 continue
2792     fi
2793    
2794     if [ -f "${md5file}" ]
2795     then
2796     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2797     echo -ne "checking md5sum (${count_current}/${count_total}): "
2798 niro 1652 mchecksum --rundir "${PKGDIR}" --file "${md5file}" --method md5 || die "md5 for ${pkgfile} failed"
2799 niro 226 else
2800     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2801     echo -e "!! no md5sum file found for ${pkgfile} :("
2802     fi
2803     done
2804    
2805     # add a crlf for a better view
2806     if [ ${count_total} -gt 1 ]; then echo; fi
2807     }
2808    
2809     ## uninstall_packages ulist
2810     uninstall_packages()
2811     {
2812     local list="$@"
2813     local pcat
2814     local pname
2815     local pver
2816     local pbuild
2817     local can_pcat
2818     local can_pname
2819     local can_ver_list
2820    
2821     if [[ -n ${MROOT} ]]
2822     then
2823     echo -ne ${COLRED}
2824     echo "!! uninstalling from MROOT=${MROOT}"
2825     echo -ne ${COLDEFAULT}
2826     echo
2827     fi
2828    
2829     # generate a candidates list
2830     for pkg in ${list}
2831     do
2832     pcat=$(dep2pcat ${pkg})
2833     pname=$(magename2pname ${pkg})
2834     pver=$(magename2pver ${pkg})
2835     pbuild=$(magename2pbuild ${pkg})
2836     can_pcat="${pcat}"
2837     can_pname="${pname}"
2838 niro 2270
2839 niro 226 if [ -z "${can_ver_list}" ]
2840     then
2841     can_ver_list=" ${pver}-${pbuild}"
2842     else
2843     can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
2844     fi
2845     done
2846    
2847     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2848     echo "following candidate(s) will be removed:"
2849     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2850 niro 240 echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
2851 niro 226 echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
2852 niro 501 echo
2853 niro 240 if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
2854     then
2855     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2856     echo "( Press [CTRL+C] to abort )"
2857     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2858     echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
2859     for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
2860     do
2861     echo -ne "${COLRED} ${i}${COLDEFAULT}"
2862     sleep 1
2863     done
2864     echo
2865     echo
2866     fi
2867 niro 226
2868     for pkg in ${list}
2869     do
2870     pcat=$(dep2pcat ${pkg})
2871     pname=$(magename2pname ${pkg})
2872     pver=$(magename2pver ${pkg})
2873     pbuild=$(magename2pbuild ${pkg})
2874    
2875     mage_uninstall \
2876     --pcat ${pcat} \
2877     --pname ${pname} \
2878     --pver ${pver} \
2879     --pbuild ${pbuild} \
2880     --count-total ${total_pkgs} \
2881     --count-current ${current_pkg} \
2882     ${src_install}
2883    
2884     # crlf for better view in VERBOSE mode
2885     #if [[ ${VERBOSE} = on ]]; then echo; fi
2886     echo
2887     done
2888     }
2889    
2890     mage_uninstall()
2891     {
2892     # local all possible vars of a mage file
2893     # to prevent bad issues
2894     local PKGNAME
2895     local STATE
2896     local DESCRIPTION
2897     local HOMEPAGE
2898     local DEPEND
2899     local SDEPEND
2900     local PROVIDE
2901     local PKGTYPE
2902     local preinstall
2903     local postinstall
2904 niro 248 local preremove
2905     local postremove
2906 niro 226
2907     local pcat
2908     local pname
2909     local pver
2910     local pbuild
2911     local magefile
2912     local i
2913    
2914     # very basic getops
2915     for i in $*
2916     do
2917     case $1 in
2918 niro 501 --pcat|-c) shift; pcat="$1" ;;
2919 niro 226 --pname|-n) shift; pname="$1" ;;
2920     --pver|-v) shift; pver="$1" ;;
2921 niro 501 --pbuild|-b) shift; pbuild="$1" ;;
2922 niro 226 esac
2923     shift
2924 niro 501 done
2925 niro 226
2926     # sanity checks; abort if not given
2927 niro 501 [ -z "${pcat}" ] && die "mage_uninstall() \$pcat not given."
2928 niro 226 [ -z "${pname}" ] && die "mage_uninstall() \$pname not given."
2929     [ -z "${pver}" ] && die "mage_uninstall() \$pver not given."
2930 niro 501 [ -z "${pbuild}" ] && die "mage_uninstall() \$pbuild not given."
2931 niro 226
2932     # check needed global vars
2933     [ -z "${MAGEDIR}" ] && die "mage_uninstall() \$MAGEDIR not set."
2934     [ -z "${INSTALLDB}" ] && die "mage_uninstall() \$INSTALLDB not set."
2935     [ -z "${BUILDDIR}" ] && die "mage_uninstall() \$BUILDDIR not set."
2936    
2937     xtitle "[ (${count_current}/${count_total}) Removing ${pcat}/${pname}-${pver}-${pbuild} ]"
2938     echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2939     echo -n "removing: "
2940     echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2941 niro 416 echo -e "${COLRED}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2942 niro 226
2943 niro 499 magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
2944 niro 226 source ${magefile}
2945    
2946     ## preremove scripts
2947     if [ -n "$(typeset -f preremove)" ]
2948     then
2949     echo -e " ${COLBLUE}***${COLDEFAULT} running preremove ... "
2950     preremove
2951     unset preremove
2952     fi
2953    
2954     # runs uninstall
2955     build_douninstall \
2956     --pcat "${pcat}" \
2957     --pname "${pname}" \
2958     --pver "${pver}" \
2959     --pbuild "${pbuild}"
2960    
2961     ## postremove scripts
2962     if [ -n "$(typeset -f postremove)" ]
2963     then
2964     echo -e " ${COLBLUE}***${COLDEFAULT} running postremove ... "
2965     postremove
2966     unset postremove
2967     fi
2968    
2969     # removes the database entry
2970     remove_database_entry \
2971     --pcat "${pcat}" \
2972     --pname "${pname}" \
2973     --pver "${pver}" \
2974     --pbuild "${pbuild}" \
2975     || die "error in mage_uninstall() running remove_database_entry()."
2976    
2977     # rebuilds toplevel info node
2978     if [[ ${MAGE_INFO_REBUILD} = true ]]
2979     then
2980     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2981     echo -n "rebuilding top-level info node ... "
2982     ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2983     > ${MROOT}/usr/share/info/dir && \
2984     echo "done." || echo "failure."
2985     unset MAGE_INFO_REBUILD
2986     fi
2987    
2988     # rebuilds the enviroment with the content of /etc/env.d
2989     if [[ ${MAGE_ENV_REBUILD} = true ]]
2990     then
2991     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2992     echo -n "rebuilding environment ... "
2993 niro 2606 ${MLIBDIR}/env-rebuild > /dev/null && \
2994 niro 226 echo "done." || echo "failure."
2995     unset MAGE_ENV_REBUILD
2996     fi
2997    
2998     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2999     echo -n "package "
3000     # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
3001     # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
3002     echo "successfully removed."
3003 niro 248
3004 niro 258 # unset these functions
3005     unset -f preinstall
3006     unset -f postinstall
3007     unset -f preremove
3008     unset -f postremove
3009 niro 226 }
3010    
3011 niro 2371 # rerun_pkgfunctions [method] pkg1 pkg2 pkg3
3012     rerun_pkgfunctions()
3013     {
3014     local method
3015     local list
3016     local pcat
3017     local pname
3018     local pver
3019     local pbuild
3020     local magefile
3021     local i
3022    
3023     # very basic getops
3024 niro 2372 case $1 in
3025     --method) shift; method="$1" ;;
3026     esac
3027     shift
3028 niro 2371 local list="$@"
3029    
3030     # sanity check
3031     case ${method} in
3032     preinstall|postinstall) ;;
3033     preremove|postremove) ;;
3034     *) die "rerun_pkgfunctions(): Unknown method '${method}'." ;;
3035     esac
3036    
3037     if [[ -n ${MROOT} ]]
3038     then
3039     echo -ne ${COLRED}
3040     echo "!! running in MROOT=${MROOT}"
3041     echo -ne ${COLDEFAULT}
3042     echo
3043     fi
3044    
3045     for pkg in ${list}
3046     do
3047     pcat=$(dep2pcat ${pkg})
3048     pname=$(magename2pname ${pkg})
3049     pver=$(magename2pver ${pkg})
3050     pbuild=$(magename2pbuild ${pkg})
3051     magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
3052    
3053     if [ -e ${magefile} ]
3054     then
3055     source ${magefile}
3056     if [ -n "$(typeset -f ${method})" ]
3057     then
3058     echo -e " ${COLBLUE}***${COLDEFAULT} running ${method} for ${pkg} ... "
3059     ${method}
3060     else
3061     echo "No ${method}() for pkg '${pkg}' defined. Doing nothing."
3062     fi
3063     unset -f preinstall postinstall preremove postremove
3064     else
3065     die "Magefile '${magefile}' does not exist."
3066     fi
3067     done
3068     }
3069    
3070 niro 1209 show_etc_update_mesg()
3071     {
3072 niro 226 [ ${MAGE_PROTECT_COUNTER} -eq 0 ] && return 0
3073    
3074     echo
3075     echo -ne "${COLRED}"
3076     echo "Important:"
3077     echo -ne ${COLDEFAULT}
3078     echo "${MAGE_PROTECT_COUNTER} protected file(s) were installed."
3079     echo
3080     echo "Please run 'etc-update' to update your configuration files."
3081     echo
3082     }
3083 niro 237
3084     pkgsearch()
3085     {
3086     local string="$1"
3087     local result
3088     local pkg
3089     local pcat
3090     local pname
3091     local magefile
3092     local pver
3093     local pbuild
3094     local state
3095     local descriptiom
3096     local homepage
3097 niro 1648 local license
3098 niro 237 local i
3099     local all_installed
3100     local ipver
3101     local ipbuild
3102 niro 445 local latest_available
3103 niro 458 local depsfull
3104     local sdepsfull
3105     local deps
3106     local sdeps
3107     local dep
3108     local sign
3109 niro 237
3110     # only names no versions
3111 niro 391 result="$(find ${MAGEDIR} -mindepth 2 -maxdepth 2 -type d -name '*'${string}'*'| sed '/profiles/d' | sed '/includes/d')"
3112 niro 328 #result="$(find ${MAGEDIR} -type f -name '*'${string}'*'.mage | sort)"
3113 niro 237
3114     # nothing found
3115     [[ -z ${result} ]] && die "No package found containing '${string}' in the name."
3116    
3117     for pkg in ${result}
3118     do
3119     # dirty, but does the job
3120     pcat="$(magename2pcat ${pkg}/foo)"
3121     pname="$(magename2pname ${pkg}-foo-foo)"
3122    
3123     # get highest version available
3124     magefile=$(get_highest_magefile ${pcat} ${pname})
3125    
3126 niro 445 if [[ ! -z ${magefile} ]]
3127     then
3128     # now get all needed infos to print a nice output
3129     pver="$(magename2pver ${magefile})"
3130     pbuild="$(magename2pbuild ${magefile})"
3131     state="$(get_value_from_magefile STATE ${magefile})"
3132     description="$(get_value_from_magefile DESCRIPTION ${magefile})"
3133     homepage="$(get_value_from_magefile HOMEPAGE ${magefile})"
3134 niro 1648 license="$(get_value_from_magefile LICENSE ${magefile})"
3135    
3136 niro 445 # all installed
3137     for i in $(get_uninstall_candidates --pname ${pname} --pcat ${pcat})
3138     do
3139     ipver="$(magename2pver ${i})"
3140     ipbuild="$(magename2pbuild ${i})"
3141 niro 1648
3142 niro 445 if [[ -z ${all_installed} ]]
3143     then
3144     all_installed="${ipver}-${ipbuild}"
3145     else
3146     all_installed="${all_installed} ${ipver}-${ipbuild}"
3147     fi
3148     done
3149     [[ -z ${all_installed} ]] && all_installed="none"
3150 niro 1648
3151 niro 445 case ${state} in
3152     stable) state=${COLGREEN}"[s] ";;
3153     testing) state=${COLYELLOW}"[t] ";;
3154     unstable) state=${COLRED}"[u] ";;
3155     old) state=${COLGRAY}"[o] ";;
3156     esac
3157 niro 237
3158 niro 445 latest_available="${pver}-${pbuild}"
3159     else
3160     # package is masked
3161     state="${COLRED}[m] "
3162     latest_available="${COLRED}masked for this distribution.${COLDEFAULT}"
3163     fi
3164 niro 237
3165 niro 458 depsfull="$(get_value_from_magefile DEPEND ${magefile})"
3166     sdepsfull="$(get_value_from_magefile SDEPEND ${magefile})"
3167    
3168     while read sign dep
3169     do
3170     case ${dep} in
3171     "") continue;;
3172     esac
3173    
3174 niro 1961 if [[ -z ${deps} ]]
3175     then
3176     deps="$(basename ${dep%-*})"
3177     else
3178     deps="${deps} $(basename ${dep%-*})"
3179     fi
3180 niro 458 done << EOF
3181     ${depsfull}
3182     EOF
3183    
3184     while read sign dep
3185     do
3186     case ${dep} in
3187     "") continue;;
3188     esac
3189    
3190 niro 1961 if [[ -z ${sdeps} ]]
3191     then
3192     sdeps="$(basename ${dep%-*})"
3193     else
3194     sdeps="${sdeps} $(basename ${dep%-*})"
3195     fi
3196 niro 458 done << EOF
3197     ${sdepsfull}
3198     EOF
3199    
3200 niro 237 echo -e "${state}${pcat}/${pname}"${COLDEFAULT}
3201 niro 445 echo -e " Latest available: ${latest_available}"
3202 niro 237 echo " Installed versions: ${all_installed}"
3203     echo " Description: ${description}"
3204     echo " Homepage: ${homepage}"
3205 niro 1648 if [[ ! -z ${license} ]]
3206     then
3207     echo " License: ${license}"
3208     fi
3209 niro 1961 echo " Depends: ${deps}"
3210 niro 458 echo " SDepends: ${sdeps}"
3211 niro 237 echo
3212    
3213     unset pcat
3214     unset pname
3215     unset magefile
3216     unset pver
3217     unset pbuild
3218     unset state
3219     unset descriptiom
3220     unset homepage
3221     unset all_installed
3222     unset ipver
3223     unset ipbuild
3224 niro 458 unset depsfull
3225     unset sdepsfull
3226     unset deps
3227     unset sdeps
3228     unset dep
3229     unset sign
3230 niro 237 done
3231     }
3232 niro 249
3233     export_inherits()
3234     {
3235     local include="$1"
3236     shift
3237    
3238     while [ "$1" ]
3239     do
3240     local functions="$1"
3241    
3242     # sanity checks
3243     [ -z "${include}" ] && die "export_inherits(): \$include not given."
3244     [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
3245    
3246     eval "${functions}() { ${include}_${functions} ; }"
3247    
3248     # debug
3249 niro 1584 mqueryfeature "debug" && typeset -f "${functions}"
3250 niro 249
3251     shift
3252     done
3253     }
3254 niro 350
3255     mlibdir()
3256     {
3257     local libdir=lib
3258     [[ ${ARCH} = x86_64 ]] && libdir=lib64
3259    
3260     echo "${libdir}"
3261     }
3262 niro 370
3263     ## blacklisted ${magefile}
3264     blacklisted()
3265     {
3266     [[ -z ${MAGE_DISTRIBUTION} ]] && local MAGE_DISTRIBUTION=stable
3267    
3268     # compat
3269     [[ ${USE_UNSTABLE} = true ]] && local MAGE_DISTRIBUTION=unstable
3270     [[ ${USE_TESTING} = true ]] && local MAGE_DISTRIBUTION=testing
3271    
3272 niro 892 # support both types for the moment
3273     if [[ -f /etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION} ]]
3274     then
3275     local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION}"
3276     else
3277     local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}"
3278     fi
3279 niro 370
3280     # return 0 if the list not exist; nothin is masked
3281     [[ ! -f ${EXCLUDED} ]] && return 0
3282    
3283     local MAGEFILE="$1"
3284    
3285     local PCAT="$(magename2pcat ${MAGEFILE})"
3286     local PNAME="$(magename2pname ${MAGEFILE})"
3287     local PVER="$(magename2pver ${MAGEFILE})"
3288     local PBUILD="$(magename2pbuild ${MAGEFILE})"
3289    
3290     local EXPCAT EXPNAME EXPVER EXPBUILD
3291     while read EXPCAT EXPNAME EXPVER EXPBUILD
3292     do
3293     # ignore spaces and comments
3294     case "${EXPCAT}" in
3295     \#*|"") continue ;;
3296     esac
3297    
3298     # exclude full pver
3299     if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
3300     [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
3301     [[ -n ${PVER} ]] && [[ -n ${PBUILD} ]] &&
3302     [[ -n ${EXPVER} ]] && [[ -n ${EXPBUILD} ]]
3303     then
3304     [[ ${EXPCAT}/${EXPNAME}-${EXPVER}-${EXPBUILD} = ${PCAT}/${PNAME}-${PVER}-${PBUILD} ]] && return 1
3305     fi
3306    
3307     # exclude pcat/pname only
3308     if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
3309     [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
3310     [[ -z ${EXPVER} ]] && [[ -z ${EXPBUILD} ]]
3311     then
3312     [[ ${EXPCAT}/${EXPNAME} = ${PCAT}/${PNAME} ]] && return 1
3313     fi
3314     done << EOF
3315     $( cat ${EXCLUDED}; echo)
3316     EOF
3317    
3318     return 0
3319     }
3320    
3321 niro 1273 # need_busybox_support ${cmd}
3322     # return 0 (no error = needs busybox support) or return 1 (error = no busybox support required)
3323     need_busybox_support()
3324     {
3325     local cmd
3326 niro 1952 local busybox
3327 niro 1273 cmd="$1"
3328    
3329 niro 1952 for busybox in {,/usr}/bin/busybox
3330     do
3331     if [[ -x ${busybox} ]]
3332 niro 1273 then
3333 niro 2223 if [[ $(readlink $(type -P ${cmd})) = ${busybox} ]]
3334 niro 1952 then
3335     # needs busybox support
3336     return 0
3337     fi
3338 niro 1273 fi
3339 niro 1952 done
3340 niro 1318
3341     # no busybox
3342     return 1
3343 niro 1273 }
3344    
3345     # busybox_filter_wget_options ${wget_opts}
3346     busybox_filter_wget_options()
3347     {
3348     local opts="$@"
3349     local i
3350     local fixed_opts
3351    
3352     if need_busybox_support wget
3353     then
3354     for i in ${opts}
3355     do
3356     # show only the allowed ones
3357     case ${i} in
3358     -c|--continue) fixed_opts+=" -c" ;;
3359     -s|--spider) fixed_opts+=" -s" ;;
3360     -q|--quiet) fixed_opts+=" -q" ;;
3361     -O|--output-document) shift; fixed_opts+=" -O $1" ;;
3362     --header) shift; fixed_opts+=" --header $1" ;;
3363     -Y|--proxy) shift; fixed_opts+=" -Y $1" ;;
3364     -P) shift; fixed_opts+=" -P $1" ;;
3365     --no-check-certificate) fixed_opts+=" --no-check-certificate ${i}" ;;
3366     -U|--user-agent) shift; fixed_opts+=" -U ${i}" ;;
3367     # simply drop all other opts
3368     *) continue ;;
3369     esac
3370     done
3371    
3372     echo "${fixed_opts}"
3373     else
3374     echo "${opts}"
3375     fi
3376     }
3377 niro 1541
3378     have_root_privileges()
3379     {
3380     local retval
3381    
3382     if [[ $(id -u) = 0 ]]
3383     then
3384     retval=0
3385     else
3386     retval=1
3387     fi
3388    
3389     return ${retval}
3390     }
3391 niro 1584
3392     known_mage_feature()
3393     {
3394     local feature="$1"
3395     local retval
3396 niro 2167
3397 niro 1584 case "${feature}" in
3398     autosvc|!autosvc) retval=0 ;;
3399     buildlog|!buildlog) retval=0 ;;
3400     ccache|!ccache) retval=0 ;;
3401     check|!check) retval=0 ;;
3402     compressdoc|!compressdoc) retval=0 ;;
3403 niro 1627 debug|!debug) retval=0 ;;
3404 niro 1584 distcc|!distcc) retval=0 ;;
3405 niro 2167 icecc|!icecc) retval=0 ;;
3406 niro 1584 kernelsrcunpack|!kernelsrcunpack) retval=0 ;;
3407     libtool|!libtool) retval=0 ;;
3408     linuxsymlink|!linuxsymlink) retval=0 ;;
3409 niro 2606 multilib|!multilib) reval=0 ;;
3410 niro 1584 pkgbuild|!pkgbuild) retval=0 ;;
3411 niro 1649 pkgdistrotag|!pkgdistrotag) retval=0 ;;
3412 niro 2606 pkgmetadata|!pkgmetadata) retval=0 ;;
3413 niro 1584 purge|!purge) retval=0 ;;
3414     qalint|!qalint) retval=0 ;;
3415     regentree|!regentree) retval=0 ;;
3416 niro 1620 resume|!resume) retval=0 ;;
3417 niro 1584 srcpkgbuild|!srcpkgbuild) retval=0 ;;
3418     srcpkgtarball|!srcpkgtarball) retval=0 ;;
3419 niro 1627 static|!static) retval=0 ;;
3420     stepbystep|!stepbystep) retval=0 ;;
3421 niro 1584 strip|!strip) retval=0 ;;
3422 niro 1627 verbose|!verbose) retval=0 ;;
3423 niro 1584 *) retval=1 ;;
3424     esac
3425    
3426     return "${retval}"
3427     }
3428    
3429     load_mage_features()
3430     {
3431     for i in ${MAGE_FEATURES_GLOBAL[*]} ${MAGE_FEATURES[*]}
3432     do
3433     FVERBOSE=off msetfeature ${i}
3434     done
3435     }
3436    
3437     msetfeature()
3438     {
3439     local feature
3440     local count
3441     local i
3442     local found
3443    
3444     for feature in $@
3445     do
3446     found=0
3447     count="${#MAGE_FEATURES_CURRENT[*]}"
3448    
3449     if ! known_mage_feature "${feature}"
3450     then
3451 niro 1628 [[ ${FVERBOSE} = off ]] || echo -e "${COLRED}Unknown feature '${feature}', ignoring it${COLDEFAULT}"
3452 niro 1584 return 3
3453     fi
3454    
3455     for ((i=0; i<count; i++))
3456     do
3457     if [[ ${MAGE_FEATURES_CURRENT[${i}]} = ${feature} ]]
3458     then
3459 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' already enabled${COLDEFAULT}"
3460 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3461     found=1
3462     elif [[ ${MAGE_FEATURES_CURRENT[${i}]} = !${feature} ]]
3463     then
3464 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' currently disabled, enabling it!${COLDEFAULT}"
3465 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3466     found=1
3467     elif [[ ${MAGE_FEATURES_CURRENT[${i}]} = ${feature//!} ]]
3468     then
3469 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature//!}' currently enabled, disabling it!${COLDEFAULT}"
3470 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3471     found=1
3472     fi
3473     done
3474    
3475     # if the feature was not found after proccessing the whole array
3476     # it was not declared. in this case enable it
3477     if [[ ${found} = 0 ]]
3478     then
3479 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' was not declared, enabling it!${COLDEFAULT}"
3480 niro 1584 MAGE_FEATURES_CURRENT=( ${MAGE_FEATURES_CURRENT[*]} "${feature}" )
3481     fi
3482    
3483 niro 2720 export MAGE_FEATURES_CURRENT
3484 niro 1584 done
3485     }
3486    
3487     mqueryfeature()
3488     {
3489     local feature="$1"
3490     local retval=1
3491     local i
3492    
3493     if known_mage_feature "${feature}"
3494     then
3495     for i in ${MAGE_FEATURES_CURRENT[*]}
3496     do
3497     if [[ ${i} = ${feature} ]]
3498     then
3499     retval=0
3500     break # found break here
3501     fi
3502     done
3503     else
3504 niro 1628 [[ ${FVERBOSE} = off ]] || echo -e "${COLRED}Unknown feature '${feature}', ignoring it${COLDEFAULT}"
3505 niro 1584 retval=3
3506     fi
3507    
3508     return ${retval}
3509     }
3510    
3511     mprintfeatures()
3512     {
3513 niro 1781 echo -e "${COLRED}Global features:${COLDEFAULT} ${MAGE_FEATURES_GLOBAL[*]}"
3514     echo -e "${COLYELLOW}Local features:${COLDEFAULT} ${MAGE_FEATURES[*]}"
3515     echo -e "${COLGREEN}Current features:${COLDEFAULT} ${MAGE_FEATURES_CURRENT[*]}"
3516 niro 1584 }