Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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