Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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