Magellan Linux

Annotation of /trunk/mage/usr/lib/mage/mage4.functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


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