Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1650 - (hide annotations) (download) (as text)
Fri Jan 13 21:05:41 2012 UTC (12 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 79382 byte(s)
-disabled unused choppkgname function
1 niro 226 #!/bin/bash
2     # Magellan Linux Installer Functions (mage.functions.sh)
3 niro 776 # $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/mage4.functions.sh,v 1.38 2008-10-05 10:32:24 niro Exp $
4 niro 226
5 niro 1547 COLRED="\033[1;6m\033[31m"
6     COLGREEN="\033[1;6m\033[32m"
7     COLYELLOW="\033[1;6m\033[33m"
8     COLBLUE="\033[1;6m\033[34m"
9     COLMAGENTA="\033[1;6m\033[35m"
10     COLWHITE="\033[1;6m\033[37m"
11     COLGRAY="\033[0;6m\033[37m"
12     COLBOLD="\033[1m"
13     COLDEFAULT="\033[0m"
14    
15     if [[ ${NOCOLORS} = true ]]
16     then
17     COLRED=""
18     COLGREEN=""
19     COLYELLOW=""
20     COLBLUE=""
21     COLMAGENTA=""
22     COLWHITE=""
23     COLGRAY=""
24     COLBOLD=""
25     COLDEFAULT=""
26     fi
27    
28 niro 226 mage_setup()
29     {
30     [ ! -d ${MROOT}${INSTALLDB} ] && \
31     install -d ${MROOT}${INSTALLDB}
32     [ ! -f ${MROOT}${VIRTUALDB_FILE} ] && \
33     touch ${MROOT}${VIRTUALDB_FILE}
34     [ ! -d ${PKGDIR} ] && install -d ${PKGDIR}
35     [ ! -d ${BUILDDIR} ] && install -d ${BUILDDIR}
36     [ ! -d ${MAGEDIR} ] && install -d ${MAGEDIR}
37    
38     return 0
39     }
40    
41 niro 1549 mchecksum()
42     {
43     local i
44     local rundir
45     local file
46     local method
47     local cmd
48     local retval
49    
50     # very basic getops
51     for i in $*
52     do
53     case $1 in
54     --rundir|-r) shift; rundir="$1" ;;
55     --file|-f) shift; file="$1" ;;
56     --method|-m) shift; method="$1" ;;
57     esac
58     shift
59     done
60    
61     # sanity checks
62     [[ -z ${rundir} ]] && die "mchecksum(): rundir missing"
63     [[ -z ${file} ]] && die "mchecksum(): file missing"
64     [[ -z ${method} ]] && die "mchecksum(): method missing"
65    
66     case ${method} in
67     md5) cmd="md5sum" ;;
68     sha256) cmd="sha256sum" ;;
69 niro 1625 *) die "mchecksum(): unknown method '${method}'" ;;
70 niro 1549 esac
71    
72     if [[ -d ${rundir} ]]
73     then
74     pushd ${rundir} &> /dev/null
75     ${cmd} -c ${file} &> /dev/null
76     retval="$?"
77     popd &> /dev/null
78     else
79     retval=1
80     fi
81    
82     return "${retval}"
83     }
84    
85 niro 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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> DIR: ${MROOT}${pathto}"
210 niro 226
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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> FILE: ${MROOT}${pathto}"
286 niro 226 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 niro 1584 if mqueryfeature "verbose"
304 niro 226 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 niro 1584 if mqueryfeature "verbose"
335 niro 942 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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> LINK: ${MROOT}${pathto}"
394 niro 226
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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> PIPE: ${MROOT}${pathto}"
444 niro 226
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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> CHAR: ${MROOT}${pathto}"
488 niro 226
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 niro 1584 mqueryfeature "verbose" && echo -e "\t>>> FIFO: ${MROOT}${pathto}"
532 niro 1209
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 niro 1584 mqueryfeature "verbose" && \
866 niro 226 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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< LINK: ${MROOT}${pathto}"
878 niro 226 rm "${MROOT}${pathto}"
879     ;;
880    
881     1)
882 niro 1584 mqueryfeature "verbose" && \
883 niro 226 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 niro 1584 mqueryfeature "verbose" && \
950 niro 226 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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< FILE: ${MROOT}${pathto}"
976 niro 280 rm "${MROOT}${pathto}"
977     ;;
978    
979     # file is protected, do not delete
980     2)
981 niro 1584 if mqueryfeature "verbose"
982 niro 280 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 niro 1584 if mqueryfeature "verbose"
993 niro 942 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 niro 1584 mqueryfeature "verbose" && \
1005 niro 226 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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< PIPE: ${MROOT}${pathto}"
1067 niro 226 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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< CHAR: ${MROOT}${pathto}"
1127 niro 226 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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< FIFO: ${MROOT}${pathto}"
1189 niro 1209 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 niro 1584 mqueryfeature "verbose" && \
1250 niro 226 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 niro 1584 mqueryfeature "verbose" && \
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 niro 1584 mqueryfeature "verbose" && echo -e "\t<<< DIR: ${MROOT}${pathto}"
1271 niro 226 else
1272 niro 1584 mqueryfeature "verbose" && \
1273 niro 226 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 niro 1626 mqueryfeature "!verbose" && wget_opts+=" --quiet"
1405 niro 1549
1406     # filter wget options if busybox was found
1407 niro 1626 wget_opts+=" $(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})"
1408 niro 1549
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 if [ ! -f ${PKGDIR}/${pkg} ]
1492     then
1493 niro 1549 die "Package '${pkg}' after download not found in '${PKGDIR}'"
1494 niro 226 fi
1495     done
1496    
1497     # add a crlf for a better view
1498     if [ ${count_total} -gt 1 ]; then echo; fi
1499     }
1500    
1501     syncmage()
1502     {
1503     if [ -z "${RSYNC}" ]
1504     then
1505 niro 419 die "You have no rsync-mirrors defined. Please edit your ${MAGERC}."
1506 niro 226 fi
1507    
1508     local i
1509     for i in ${RSYNC}
1510     do
1511 niro 386 rsync ${RSYNC_FETCH_OPTIONS} ${i} ${MAGEDIR}
1512 niro 226 if [[ $? = 0 ]]
1513     then
1514     break
1515     else
1516     continue
1517     fi
1518     done
1519    
1520     # clean up backup files (foo~)
1521 niro 1438 find ${MAGEDIR} -name \*~ -exec rm '{}' ';'
1522 niro 226
1523 niro 966 # check if a newer mage version is available
1524 niro 226 is_newer_mage_version_available
1525     }
1526    
1527 niro 739 syncmage_tarball()
1528     {
1529     local latest_tarball
1530 niro 1083 local latest_md5
1531 niro 739 local temp="$(mktemp -d)"
1532     local mirr mymirr
1533 niro 1087 local opt
1534 niro 1273 local tar_opts
1535 niro 1293 local wget_opts
1536 niro 739
1537 niro 1083 # try to get the md5 marked as latest on the server
1538     latest_md5="mage-latest.md5"
1539    
1540 niro 739 # try to get the tarball marked as latest on the server
1541     latest_tarball="mage-latest.tar.bz2"
1542    
1543 niro 1293 # filter wget command if busybox was found
1544     wget_opts="$(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})"
1545    
1546 niro 739 for mirr in ${MIRRORS}
1547     do
1548     # path without distribution
1549     mymirr="${mirr%/*}"
1550    
1551     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1552 niro 1083 echo "fetching latest md5 from ${mymirr} ..."
1553 niro 1584 mqueryfeature "!verbose" && opt="--quiet"
1554 niro 1083 wget \
1555 niro 1293 ${wget_opts} \
1556 niro 1083 --directory-prefix=${temp} \
1557 niro 1087 ${opt} ${mymirr}/rsync/tarballs/${latest_md5}
1558 niro 1083
1559     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1560 niro 739 echo "fetching latest tarball from ${mymirr} ..."
1561     wget \
1562 niro 1293 ${wget_opts} \
1563 niro 739 --directory-prefix=${temp} \
1564 niro 1087 ${opt} ${mymirr}/rsync/tarballs/${latest_tarball}
1565 niro 739 if [[ $? = 0 ]]
1566     then
1567     break
1568     else
1569     continue
1570     fi
1571     done
1572    
1573     if [[ -f ${temp}/${latest_tarball} ]]
1574     then
1575 niro 1083 # check md5
1576     if [[ ! -f ${temp}/${latest_md5} ]]
1577     then
1578     die "md5 is missing ... aborting"
1579     else
1580 niro 1087 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1581     echo -n "checking md5sum... "
1582 niro 1273 ( cd ${temp}; md5sum -c ${latest_md5} ) || die "md5 for ${latest_tarball} failed"
1583 niro 1083 fi
1584    
1585 niro 739 if [[ -d ${MAGEDIR} ]]
1586     then
1587     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1588     echo "cleaning old mage-tree ${MAGEDIR}..."
1589     rm -rf ${MAGEDIR}
1590     fi
1591    
1592 niro 1273 if need_busybox_support tar
1593     then
1594     tar_opts="xjf"
1595     else
1596     tar_opts="xjmf"
1597     fi
1598    
1599 niro 739 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1600     echo "updating mage-tree from tarball ..."
1601     # unpack in dirname of MAGEDIR, as the tarball has already the mage
1602 niro 1273 tar ${tar_opts} ${temp}/${latest_tarball} -C ${MAGEDIR%/*} || die "Unpacking tarball"
1603 niro 739
1604     if [[ -d ${temp} ]]
1605     then
1606     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1607 niro 972 echo "cleaning temp-files ..."
1608 niro 739 rm -rf ${temp}
1609     fi
1610 niro 966
1611     # check if a newer mage version is available
1612     is_newer_mage_version_available
1613 niro 739 else
1614     die "Could not fetch the latest tarball ... aborting"
1615     fi
1616     }
1617    
1618 niro 226 cleanpkg()
1619     {
1620     if [ -d "${PKGDIR}" ]
1621     then
1622     echo -n "Removing downloaded packages... "
1623     rm -rf ${PKGDIR}/*
1624     echo "done."
1625     fi
1626     }
1627    
1628     xtitle()
1629     {
1630     if [[ ${TERM} = xterm ]]
1631     then
1632     echo -ne "\033]0;Mage: $1\007"
1633     fi
1634     return 0
1635     }
1636    
1637    
1638     xtitleclean()
1639     {
1640     if [[ ${TERM} = xterm ]]
1641     then
1642     echo -ne "\033]0;\007"
1643     fi
1644     return 0
1645     }
1646    
1647    
1648 niro 1650 # unused?
1649     #
1650     # # cuts full pathnames or versionized names down to basename
1651     # choppkgname()
1652     # {
1653     # #we want this only if full name was used
1654     # if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ]
1655     # then
1656     # #cuts ARCH and PBUILD
1657     # #ARCH comes from ${MAGERC}
1658     # MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}$(print_distrotag)-r*.::g")
1659     #
1660     # #cuts version number
1661     # MAGENAME=$(basename ${MAGENAME%-*} .mage)
1662     # fi
1663     # }
1664 niro 226
1665    
1666     # get_categorie $PNAME, returns CATEGORIE
1667     # $1=pname
1668     # ret 0=ok, 1=not_found
1669     pname2pcat()
1670     {
1671     local pname="$1"
1672     local repo="$2"
1673     local pcat
1674     local categorie
1675    
1676     for pcat in ${MAGEDIR}/*
1677     do
1678     if [ -d ${pcat}/${pname} ]
1679     then
1680     categorie=$(basename ${pcat})
1681     fi
1682     done
1683    
1684     echo "${categorie}"
1685     }
1686    
1687     # check_stable_package /path/to/foo.mage
1688     # returns 0=stable 1=unstable
1689     check_stable_package()
1690     {
1691 niro 370 # first check if this magefile is not blacklisted
1692     blacklisted "$1" || return 1
1693    
1694 niro 226 local STATE
1695     STATE="$(get_value_from_magefile STATE "$1")"
1696    
1697     # state testing
1698 niro 286 if [[ ${USE_TESTING} = true ]] || [[ ${MAGE_DISTRIBUTION} = testing ]]
1699 niro 226 then
1700     case ${STATE} in
1701     testing|stable) return 0 ;;
1702     *) return 1 ;;
1703     esac
1704     fi
1705    
1706     # state unstable
1707 niro 286 if [[ ${USE_UNSTABLE} = true ]] || [[ ${MAGE_DISTRIBUTION} = unstable ]]
1708 niro 226 then
1709     case ${STATE} in
1710     unstable|testing|stable) return 0 ;;
1711     *) return 1 ;;
1712     esac
1713     fi
1714    
1715     # no use_state given = stable
1716     case ${STATE} in
1717     stable) return 0 ;;
1718     *) return 1 ;;
1719     esac
1720     }
1721    
1722    
1723     # get_highest_magefile ${PCAT} ${PNAME}
1724     # fake at moment returns only stable pkgs (must set to be one)
1725     # return $HIGHEST_MAGEFILE
1726     get_highest_magefile()
1727     {
1728     local HIGHEST_MAGEFILE
1729     local PCAT="$1"
1730     local PNAME="$2"
1731     local magefile
1732    
1733 niro 676 # do not list the content of a directory, only the name (-d)
1734 niro 1438 for magefile in $(ls --format=single-column -v -d ${MAGEDIR}/${PCAT}/${PNAME}/* 2> /dev/null)
1735 niro 226 do
1736 niro 676 [[ -z ${magefile} ]] && continue
1737 niro 226 # we exclude subdirs (for stuff like a md5sum dir)
1738 niro 676 [[ -d ${magefile} ]] && continue
1739 niro 226 if check_stable_package ${magefile}
1740     then
1741     HIGHEST_MAGEFILE=${magefile}
1742     #for debug only
1743 niro 1584 mqueryfeature "debug" && echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}"
1744 niro 226 fi
1745     done
1746    
1747     echo "${HIGHEST_MAGEFILE}"
1748     return 0
1749     }
1750    
1751    
1752     ###################################################
1753     # function is_config_protected #
1754     # is_config_protected /path/to/file #
1755     # #
1756     # returns: #
1757     # 0 - not protected #
1758     # 1 - error #
1759     # 2 - protected #
1760     # 3 - protected but masked #
1761 niro 942 # 4 - protected but ignored #
1762 niro 226 # #
1763     ###################################################
1764     is_config_protected()
1765     {
1766     local EXPFILE
1767     local TEST
1768     local PROTECTED
1769     local IFS
1770 niro 942 local i
1771     local x
1772 niro 226
1773     EXPFILE="${MROOT}$1"
1774    
1775     # file does not exist; it can be written
1776 niro 451 [[ ! -e ${EXPFILE} ]] && return 0
1777 niro 226
1778     # to be safe; it may be '§'
1779     IFS=' '
1780    
1781 niro 942 # check if config protected
1782 niro 226 for i in ${CONFIG_PROTECT}
1783     do
1784 niro 942 # only replace $i in the beginning of the variable
1785 niro 226 TEST="${EXPFILE/#${MROOT}${i}/Protected}"
1786 niro 451 if [[ ${TEST} != ${EXPFILE} ]]
1787 niro 226 then
1788 niro 942 # file is config proteced
1789 niro 226 PROTECTED=TRUE
1790    
1791 niro 942 # check if not masked
1792 niro 226 for x in ${CONFIG_PROTECT_MASK}
1793     do
1794     TEST="${EXPFILE/#${MROOT}${x}/Protect_Masked}"
1795 niro 451 if [[ ${TEST} != ${EXPFILE} ]]
1796 niro 226 then
1797     PROTECTED=MASKED
1798     fi
1799     done
1800 niro 942
1801     # check if not ignored
1802     for x in ${CONFIG_PROTECT_IGNORE}
1803     do
1804     TEST="${EXPFILE/#${MROOT}${x}/Protect_Ignored}"
1805     if [[ ${TEST} != ${EXPFILE} ]]
1806     then
1807     PROTECTED=IGNORED
1808     fi
1809     done
1810 niro 226 fi
1811     done
1812    
1813     unset IFS
1814    
1815     case ${PROTECTED} in
1816     TRUE)
1817     #echo "I'm protected"
1818     return 2
1819     ;;
1820     MASKED)
1821     #echo "I'm protected, but masked - delete me"
1822     return 3
1823     ;;
1824 niro 942 IGNORED)
1825     #echo "I'm protected, but ignored - keep me, del update"
1826     return 4
1827     ;;
1828 niro 226 *)
1829     #echo "delete me"
1830     return 0
1831     ;;
1832     esac
1833     }
1834    
1835    
1836     ###################################################
1837     # function count_protected_files #
1838     # count_protected_files /path/to/file #
1839     # #
1840     # note: prints number of protected files #
1841     # exp: 0012 #
1842     ###################################################
1843     count_protected_files()
1844     {
1845 niro 603 local file="$1"
1846     local dirname="${file%/*}"
1847     local filename="${file##*/}"
1848     local count
1849     local output
1850     local i
1851    
1852     declare -i count=0
1853    
1854     # check if there are already protected files
1855     for oldpretected in $(find ${dirname} -iname "._cfg????_${filename}" |
1856     sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" |
1857     sort -t'%' -k3 -k2 | cut -f1 -d'%')
1858     do
1859     count=$(echo ${oldpretected} | cut -d_ -f2 | sed -e "s:cfg::")
1860     done
1861     (( count ++ ))
1862    
1863     # fill output up with zeros
1864     for (( i=${#count}; i < 4; i++ )); do output="${output}0"; done
1865     output="${output}${count}"
1866    
1867     echo "${output}"
1868 niro 226 }
1869    
1870     # call with
1871     # 'get_uninstall_candidates (--pcat cat --protected pcat/pfull) --pname PNAME'
1872     # returns /path/to/magefile(s)
1873     get_uninstall_candidates()
1874     {
1875     local search_pname
1876     local pkg
1877     local pcat
1878     local pname
1879     local pver
1880     local pbuild
1881     local list
1882     local pcatdir
1883     local protected
1884 niro 449 local i
1885 niro 226
1886     # very basic getops
1887     for i in $*
1888     do
1889     case $1 in
1890     --pcat|-c) shift; pcatdir="$1" ;;
1891     --pname|-n) shift; search_pname="$1" ;;
1892     --protected|-p) shift; protected="$1" ;;
1893     esac
1894     shift
1895     done
1896    
1897 niro 329 # it's not good to complain here about empty pnames; better to continue later anyway
1898     # # sanity checks; abort if not given
1899     # [ -z "${search_pname}" ] && die "get_uninstall_candidates() \$search_pname not given."
1900 niro 226
1901    
1902     # check needed global vars
1903     [ -z "${INSTALLDB}" ] && die "get_uninstall_candidates() \$INSTALLDB not set."
1904    
1905     # set pcatdir to '*' if empty
1906 niro 329 [ -z "${pcatdir}" ] && pcatdir='*'
1907 niro 226
1908     for pkg in ${MROOT}${INSTALLDB}/${pcatdir}/*
1909     do
1910     # abort if not a dir
1911     [ ! -d ${pkg} ] && continue
1912    
1913     pname="$(magename2pname ${pkg})"
1914    
1915     if [[ ${search_pname} = ${pname} ]]
1916     then
1917     pcat="$(magename2pcat ${pkg} installdb)"
1918     pver="$(magename2pver ${pkg})"
1919     pbuild="$(magename2pbuild ${pkg})"
1920    
1921     # exclude proteced
1922     [[ ${protected} = ${pcat}/${pname}-${pver}-${pbuild} ]] && continue
1923    
1924     list="${list} ${pcat}/${pname}-${pver}-${pbuild}"
1925     fi
1926     done
1927    
1928     echo "${list}"
1929     }
1930    
1931     # reads virtualdb file
1932     #$1 = virtualname; $2 commands: showpkgs, showline
1933     #return 0 == installed -> shows installed pkg as well
1934     #return 1 == not installed
1935     virtuals_read()
1936     {
1937     local virtualname="$1"
1938     local command="$2"
1939     local virtline
1940     local line x i
1941    
1942     # parse file to get virtual_name line
1943     IFS=$'\n'
1944     for line in $(< ${MROOT}${VIRTUALDB_FILE})
1945     do
1946     IFS=$' '
1947     for x in ${line}
1948     do
1949     if [[ ${x} = ${virtualname} ]]
1950     then
1951     virtline="${line}"
1952     [[ ${command} = showline ]] && echo "${line}"
1953     fi
1954     done
1955     IFS=$'\n'
1956     done
1957    
1958     unset IFS
1959    
1960     # now read the packages linked to VIRTUAL_NAME and output them
1961     if [ -n "${virtline}" ]
1962     then
1963     if [[ ${command} = showpkgs ]]
1964     then
1965     declare -i x=0
1966     for i in ${virtline}
1967     do
1968     if [ ${x} -ge 1 ]
1969     then
1970     echo "${i}"
1971     fi
1972     ((x++))
1973     done
1974     fi
1975     return 0
1976     fi
1977     return 1
1978     }
1979    
1980    
1981     #add pkg to virtualdb
1982     # $1 == virtualname $2= pkgname
1983     # retvals: 0=ok,added; 1=error; 3=pkg already in virtual
1984     virtuals_add()
1985     {
1986     local virtualname="$1"
1987     local pkgname="$2"
1988     local oldline
1989     local line i
1990     local installed_file
1991 niro 273 local OLDIFS
1992 niro 226
1993     if virtuals_read ${virtualname}
1994     then
1995 niro 329 # make sure ${PKG_NAME} is *not* in ${VIRTUAL_NAME} already
1996 niro 226 for i in $(virtuals_read ${virtualname} showpkgs)
1997     do
1998     if [[ ${i} = ${pkgname} ]]
1999     then
2000     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2001     echo "${pkgname} already linked as ${virtualname} ..."
2002     #return 3
2003     return 0
2004     fi
2005     done
2006    
2007     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2008     echo "updating ${virtualname} entry with ${pkgname} ..."
2009     oldline="$(virtuals_read ${virtualname} showline)"
2010    
2011     # make a backup
2012     mv ${MROOT}${VIRTUALDB_FILE} ${MROOT}${VIRTUALDB_FILE}.old
2013    
2014 niro 273 OLDIFS="${IFS}"
2015 niro 226 IFS=$'\n'
2016     for line in $(< ${MROOT}${VIRTUALDB_FILE}.old)
2017     do
2018     # if the right line, append ${pkgname}, else do nothing
2019     if [[ ${line} = ${oldline} ]]
2020     then
2021     echo "${line} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
2022     else
2023     echo "${line}" >> ${MROOT}${VIRTUALDB_FILE}
2024     fi
2025     done
2026 niro 273 # unset IFS
2027     IFS="${OLDIFS}"
2028 niro 226 else
2029 niro 273 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2030 niro 226 echo "register ${pkgname} as ${virtualname} ..."
2031     echo "${virtualname} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
2032     fi
2033    
2034     return 0
2035     }
2036    
2037     #deletes pakages from virtual database
2038     #$1 virtualname; $2 pkgname
2039 niro 1209 virtuals_del()
2040     {
2041 niro 226
2042 niro 273 local virtualname="$1"
2043     local pkgname="$2"
2044     local oldline
2045     local method
2046     local line i x
2047     local pkg_installed
2048     local OLDIFS
2049    
2050     # first check if exists
2051     if virtuals_read ${virtualname}
2052 niro 226 then
2053 niro 273 # get method -> delall or update and check if ${PKG_NAME} exists in ${VIRTUAL_NAME}
2054 niro 226 declare -i x=0
2055 niro 273 for i in $(virtuals_read ${virtualname} showpkgs)
2056 niro 226 do
2057 niro 273 if [[ ${i} = ${pkgname} ]]
2058 niro 226 then
2059 niro 273 pkg_installed=true
2060 niro 226 fi
2061     ((x++))
2062     done
2063 niro 273
2064     # abort if not installed
2065     if [[ ${pkg_installed} != true ]]
2066 niro 226 then
2067 niro 273 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2068     echo "${pkgname} does not exists in ${virtualname}."
2069 niro 226 return 0
2070     fi
2071 niro 273
2072 niro 226 if [ ${x} -ge 2 ]
2073     then
2074 niro 273 method=update
2075 niro 226 else
2076 niro 273 method=delall
2077 niro 226 fi
2078 niro 273
2079     # get the complete line
2080     oldline="$(virtuals_read ${virtualname} showline)"
2081    
2082     # make a backup of the db
2083 niro 226 mv ${VIRTUALDB_FILE} ${VIRTUALDB_FILE}.old
2084 niro 273
2085     # parse virtualdb
2086     OLDIFS="${IFS}"
2087 niro 226 IFS=$'\n'
2088     for line in $(< ${VIRTUALDB_FILE}.old)
2089     do
2090 niro 273 if [[ ${line} = ${oldline} ]]
2091 niro 226 then
2092     #delall or update?
2093 niro 273 case ${method} in
2094 niro 226 update)
2095 niro 273 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2096     echo "Unlinking ${pkgname} from ${virtualname} in virtual database ..."
2097     # del PKG_NAME from line
2098     echo "${line/ ${pkgname}/}" >> ${VIRTUALDB_FILE}
2099 niro 226 ;;
2100     delall)
2101 niro 273 echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2102     echo "Deleting ${virtualname} in virtual database ..."
2103     # continue; do not write anything
2104 niro 226 continue
2105     ;;
2106     esac
2107     else
2108     echo "${line}" >> ${VIRTUALDB_FILE}
2109     fi
2110     done
2111 niro 273 # unset IFS
2112     IFS="${OLDIFS}"
2113 niro 226 else
2114 niro 273 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2115     echo "${virtualname} does not exists in virtual database."
2116 niro 226 fi
2117     }
2118    
2119     # gets real pkgname from virtuals.default
2120     #$1=VIRTUAL_NAME; returns PKG_NAME
2121     default_virtualname_to_pkgname()
2122     {
2123     local VIRTUAL_NAME PKG_NAME db_virtualname db_pkgname
2124    
2125     VIRTUAL_NAME=$1
2126    
2127     while read db_virtualname db_pkgname
2128     do
2129     if [ "${db_virtualname}" == "${VIRTUAL_NAME}" ]
2130     then
2131     PKG_NAME="${db_pkgname}"
2132     fi
2133     done << EOF
2134     $(< ${VIRTUALDB_DEFAULTS})
2135     EOF
2136    
2137     if [ -n "${PKG_NAME}" ]
2138     then
2139     echo "${PKG_NAME}"
2140     fi
2141     }
2142    
2143     minclude()
2144     {
2145     local i
2146    
2147 niro 437 if [[ -n $* ]]
2148 niro 226 then
2149 niro 437 for i in $*
2150 niro 226 do
2151 niro 1584 mqueryfeature "debug" && \
2152 niro 226 echo "--- Including ${MAGEDIR}/include/${i}.minc"
2153     source ${MAGEDIR}/include/${i}.minc
2154     done
2155 niro 1584 mqueryfeature "debug" && echo
2156 niro 226 fi
2157     }
2158    
2159     sminclude()
2160     {
2161     local i
2162    
2163 niro 437 if [[ -n $* ]]
2164 niro 226 then
2165 niro 437 for i in $*
2166 niro 226 do
2167     echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc"
2168     source ${SMAGESCRIPTSDIR}/include/${i}.sminc
2169     done
2170     echo
2171     fi
2172     }
2173    
2174     # checks if an newer mage version is available
2175     is_newer_mage_version_available()
2176     {
2177     local newest_mage
2178     local installed_mage
2179    
2180 niro 252 newest_mage="$(basename $(get_highest_magefile app-mage mage) .mage)"
2181 niro 226 installed_mage="$(magequery -n mage | cut -d' ' -f5)"
2182    
2183     if [[ ${newest_mage} > ${installed_mage} ]]
2184     then
2185     echo
2186     echo -en ${COLRED}"An update for your packetmanager is available. "${COLDEFAULT}
2187     echo -e ${COLBLUE}"[ ${newest_mage} ]"${COLDEFAULT}
2188     echo "It is recommened to install this newer version"
2189 niro 373 echo "or your current system installation may break."
2190 niro 226 echo
2191     echo -en "Please update mage by running "
2192     echo -e ${COLGREEN}"'mage install mage'"${COLDEFAULT}
2193     echo
2194     fi
2195     }
2196    
2197    
2198     # returns pname from pkgname
2199     # pkgname2pname $PKGNAME
2200     pkgname2pname()
2201     {
2202     local pname
2203    
2204     pname="${1%-*-*-*}"
2205     echo "${pname}"
2206     }
2207    
2208     # returns pver from pkgname
2209     # pkgname2pver $PKGNAME
2210     pkgname2pver()
2211     {
2212     local i pver
2213    
2214     i="${1/$(pkgname2pname $1)-/}"
2215     pver="${i%-*-*}"
2216     echo "${pver}"
2217     }
2218    
2219     # returns pbuild from pkgname
2220     # pkgname2pbuild $PKGNAME
2221     pkgname2pbuild()
2222     {
2223     local pbuild
2224    
2225     pbuild="${1##*-}"
2226     echo "${pbuild}"
2227     }
2228    
2229     # returns parch from pkgname
2230     # pkgname2parch $PKGNAME
2231     pkgname2parch()
2232     {
2233     local i x parch
2234    
2235     i="${1%-*-*}-"
2236     x="${1%-*}"
2237     parch="${x/${i}/}"
2238     echo "${parch}"
2239     }
2240    
2241     # returns pname from magename
2242     # magename2pname /PATH/TO/MAGE/FILE
2243     magename2pname()
2244     {
2245     local i pname
2246    
2247     i="$(basename $1 .mage)"
2248     pname="${i%-*-*}"
2249     echo "${pname}"
2250     }
2251    
2252     # returns pver from magename
2253     # magename2pver /PATH/TO/MAGE/FILE
2254     magename2pver()
2255     {
2256     local i pver
2257    
2258     i="$(basename $1 .mage)"
2259     i="${i/$(magename2pname $1)-/}"
2260     pver="${i%-*}"
2261     echo "${pver}"
2262     }
2263    
2264     # returns pbuild from magename
2265     # magename2pbuild /PATH/TO/MAGE/FILE
2266     magename2pbuild()
2267     {
2268     local i pbuild
2269    
2270     i="$(basename $1 .mage)"
2271     pbuild="${i##*-}"
2272     echo "${pbuild}"
2273     }
2274    
2275     # returns pcat from magename
2276     # magename2pcat /PATH/TO/MAGE/FILE
2277     magename2pcat()
2278     {
2279     local i pcat
2280    
2281     if [[ ${2} = installdb ]]
2282     then
2283     # go 1 dir back
2284     i="${1%/*}"
2285     else
2286     # go 2 dirs back
2287     i="${1%/*/*}"
2288     fi
2289    
2290     # get basename
2291     pcat="${i##*/}"
2292     echo "${pcat}"
2293     }
2294    
2295     # returns pcat from DEPEND (without operand ! PCAT/PNAME-VERSION)
2296     # dep2pcat DEPEND
2297     dep2pcat()
2298     {
2299     local pcat
2300    
2301     pcat="${1%/*}"
2302     echo "${pcat}"
2303     }
2304    
2305     # returns pname from DEPEND (without operand ! PCAT/PNAME-VERSION)
2306     # $2=virtual is used to resolv VDEPEND from virtual packages
2307     # dep2pcat DEPEND (virtual)
2308     dep2pname()
2309     {
2310     local pname
2311    
2312     pname="${1##*/}"
2313    
2314     # cut version only if not virtual or it will cut the name
2315     if [[ $(dep2pcat $1) != virtual ]] && \
2316     [[ $2 != virtual ]]
2317     then
2318     pname="${pname%-*}"
2319     fi
2320    
2321     echo "${pname}"
2322     }
2323    
2324     dep2highest_magefile()
2325     {
2326     local pcat
2327     local pname
2328     local magefile
2329     local installed_virtuals
2330    
2331     pcat="$(dep2pcat $1)"
2332     pname="$(dep2pname $1)"
2333    
2334     if [[ ${pcat} = virtual ]]
2335     then
2336     # first check if virtual is already installed
2337     installed_virtuals="$(virtuals_read ${pcat}/${pname} showpkgs)"
2338     if [ -n "${installed_virtuals}" ]
2339     then
2340     for vpkg in ${installed_virtuals}
2341     do
2342     realpkgname="${vpkg}"
2343     virtualpkgname="${pcat}/${pname}"
2344     pcat="$(dep2pcat ${realpkgname})"
2345     pname="$(dep2pname ${realpkgname} virtual)"
2346     done
2347     else
2348     # choose one from virtualdb defaults (virtuals.defaults)
2349     realpkgname="$(default_virtualname_to_pkgname ${pcat}/${pname})"
2350     virtualpkgname="${pcat}/${pname}"
2351     pcat="$(dep2pcat ${realpkgname})"
2352     pname="$(dep2pname ${realpkgname} virtual)"
2353     fi
2354     fi
2355    
2356     magefile="$(get_highest_magefile ${pcat} ${pname})"
2357     echo "${magefile}"
2358     }
2359    
2360     # is_installed ${PCAT}/${PNAME}-${PVER}-${PBUILD}
2361     is_installed()
2362     {
2363     local fullpkgname="$1"
2364    
2365     # return 0 if installed
2366     [ -d ${MROOT}${INSTALLDB}/${fullpkgname} ] && return 0
2367    
2368     return 1
2369     }
2370    
2371     install_packages()
2372     {
2373     local list="$@"
2374     local pkg
2375     local pcat
2376     local pname
2377     local pver
2378     local pbuild
2379     local total_pkgs
2380     local current_pkg
2381     local src_install
2382     local uninstall_list
2383    
2384     # check for --src-install
2385     if [[ $1 = --src-install ]]
2386     then
2387     # remove --src-install from list
2388     list=${list/--src-install/}
2389     # enable src-install
2390     src_install="--src-install"
2391     fi
2392    
2393     # reset MAGE_PROTECT_COUNTER
2394     declare -i MAGE_PROTECT_COUNTER=0
2395     export MAGE_PROTECT_COUNTER
2396    
2397     # get count of total packages
2398     declare -i total_pkgs=0
2399     declare -i current_pkg=0
2400     for i in ${list}; do (( total_pkgs++ )); done
2401    
2402     echo
2403    
2404     if [[ -n ${MROOT} ]]
2405     then
2406     echo -ne ${COLRED}
2407     echo "!! installing in MROOT=${MROOT}"
2408     echo -ne ${COLDEFAULT}
2409     echo
2410     fi
2411    
2412     for pkg in ${list}
2413     do
2414     (( current_pkg++ ))
2415     pcat=$(magename2pcat ${pkg})
2416     pname=$(magename2pname ${pkg})
2417     pver=$(magename2pver ${pkg})
2418     pbuild=$(magename2pbuild ${pkg})
2419    
2420     mage_install \
2421     --pcat ${pcat} \
2422     --pname ${pname} \
2423     --pver ${pver} \
2424     --pbuild ${pbuild} \
2425     --count-total ${total_pkgs} \
2426     --count-current ${current_pkg} \
2427     ${src_install}
2428    
2429     # check for allready installed packages and remove them
2430     # except the package we have installed
2431     uninstall_list="$(get_uninstall_candidates \
2432     --pcat "${pcat}" \
2433     --pname "${pname}" \
2434     --protected ${pcat}/${pname}-${pver}-${pbuild})"
2435    
2436     # uninstall all packges in uninstall_list if not empty
2437     if [ -n "${uninstall_list}" ]
2438     then
2439     echo
2440     uninstall_packages ${uninstall_list} \
2441     || die "install_packges() uninstalling not-needed."
2442     fi
2443    
2444     # crlf for better view in VERBOSE mode
2445     #if [[ ${VERBOSE} = on ]]; then echo; fi
2446     echo
2447     done
2448    
2449     #echo "DEBUG MAGE_PROTECT_COUNTER=${MAGE_PROTECT_COUNTER}"
2450     show_etc_update_mesg
2451     }
2452    
2453     # get_value_from_magefile VARIABLE
2454     # returns the content of this VAR
2455     get_value_from_magefile()
2456     {
2457     local var="$1"
2458     local magefile="$2"
2459     local value
2460    
2461 niro 370 [[ -z ${var} ]] && return 1
2462     [[ -z ${magefile} ]] && return 1
2463    
2464 niro 226 # local all possible vars of a mage file
2465     # to prevent bad issues
2466     local PKGNAME
2467     local STATE
2468     local DESCRIPTION
2469     local HOMEPAGE
2470     local DEPEND
2471     local SDEPEND
2472     local PROVIDE
2473     local PKGTYPE
2474 niro 943 local MAGE_TARGETS
2475     local SPLIT_PACKAGE_BASE
2476 niro 226 local preinstall
2477     local postinstall
2478 niro 248 local preremove
2479     local postremove
2480 niro 226
2481     # sanity checks
2482     [ -f ${magefile} ] && source ${magefile} || \
2483     die "get_value_from_magefile: ${magefile} not found."
2484     [ -z "${var}" ] && die "get_value_from_magefile: \$var not given."
2485    
2486     source ${magefile}
2487     eval value=\$$(echo ${var})
2488     echo "${value}"
2489 niro 248
2490 niro 258 # unset these functions
2491     unset -f preinstall
2492     unset -f postinstall
2493     unset -f preremove
2494     unset -f postremove
2495 niro 226 }
2496    
2497     mage_install()
2498     {
2499     # local all possible vars of a mage file
2500     # to prevent bad issues
2501     local PKGNAME
2502     local STATE
2503     local DESCRIPTION
2504     local HOMEPAGE
2505     local DEPEND
2506     local SDEPEND
2507     local PROVIDE
2508     local PKGTYPE
2509     local preinstall
2510     local postinstall
2511 niro 248 local preremove
2512     local postremove
2513 niro 226
2514     local pcat
2515     local pname
2516     local pver
2517     local pbuild
2518     local count_total
2519     local count_current
2520     local magefile
2521     local src_install
2522 niro 876 local i
2523 niro 226
2524     # very basic getops
2525     for i in $*
2526     do
2527     case $1 in
2528     --pcat|-c) shift; pcat="$1" ;;
2529     --pname|-n) shift; pname="$1" ;;
2530     --pver|-v) shift; pver="$1" ;;
2531     --pbuild|-b) shift; pbuild="$1" ;;
2532     --count-total) shift; count_total="$1" ;;
2533     --count-current) shift; count_current="$1" ;;
2534     --src-install|-s) shift; src_install=true ;;
2535     esac
2536     shift
2537     done
2538    
2539     # sanity checks; abort if not given
2540     [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
2541     [ -z "${pname}" ] && die "mage_install() \$pname not given."
2542     [ -z "${pver}" ] && die "mage_install() \$pver not given."
2543     [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
2544    
2545     # check needed global vars
2546     [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
2547     [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
2548     [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
2549    
2550     xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
2551     echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2552     echo -n "installing (${count_current}/${count_total}): "
2553     echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2554     echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2555    
2556     magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
2557     source ${magefile}
2558    
2559     # abort on sources if no srcinstall
2560     if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
2561     then
2562     echo
2563     echo -e "This Package is a Source Package."
2564     echo
2565     echo -e "Only 'srcinstall' works with this type of packages"
2566     echo -en "If you have done a srcinstall before, "
2567     echo -e "you will find the files in /usr/src."
2568     echo
2569     exit 1
2570     fi
2571    
2572     ## preinstall scripts
2573     if [ -n "$(typeset -f preinstall)" ]
2574     then
2575     echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
2576     preinstall
2577     unset preinstall
2578     fi
2579    
2580     if [[ ${src_install} = true ]]
2581     then
2582     local smage2file
2583     # check needed global vars
2584     [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
2585     [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
2586     [ -z "${BINDIR}" ] && die "\$BINDIR not set."
2587    
2588     # build the package first
2589     if [[ ${MAGEDEBUG} = on ]]
2590     then
2591     echo M:${pname}
2592     echo V:${pver}
2593     echo B:${pbuild}
2594     fi
2595    
2596 niro 943 if [[ -n ${MAGE_TARGETS} ]]
2597 niro 675 then
2598 niro 876 # basic svn compat
2599 niro 1502 if [[ -d ${SMAGESCRIPTSDIR}/.svn ]]
2600 niro 876 then
2601 niro 1502 for i in ${SMAGESCRIPTSDIR}/*/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2602 niro 876 do
2603     smage2file="${i}"
2604     done
2605     else
2606 niro 943 smage2file=${SMAGESCRIPTSDIR}/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2607 niro 876 fi
2608 niro 943
2609     elif [[ -n ${SPLIT_PACKAGE_BASE} ]]
2610     then
2611     # basic svn compat
2612 niro 1502 if [[ -d ${SMAGESCRIPTSDIR}/.svn ]]
2613 niro 943 then
2614 niro 1502 for i in ${SMAGESCRIPTSDIR}/*/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2615 niro 943 do
2616     smage2file="${i}"
2617     done
2618     else
2619     smage2file=${SMAGESCRIPTSDIR}/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2620     fi
2621    
2622 niro 675 else
2623 niro 876 # basic svn compat
2624 niro 1502 if [[ -d ${SMAGESCRIPTSDIR}/.svn ]]
2625 niro 876 then
2626 niro 1502 for i in ${SMAGESCRIPTSDIR}/*/${pname}/${pname}-${pver}-${pbuild}.smage2
2627 niro 876 do
2628     smage2file="${i}"
2629     done
2630     else
2631 niro 943 smage2file=${SMAGESCRIPTSDIR}/${pname}/${pname}-${pver}-${pbuild}.smage2
2632 niro 876 fi
2633 niro 675 fi
2634 niro 943
2635 niro 226 if [ -f "${smage2file}" ]
2636     then
2637 niro 385 echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
2638 niro 226 smage2 ${smage2file} || die "compile failed"
2639     else
2640     echo
2641     echo "$(basename ${SMAGEFILE}) not found."
2642     echo "update your smage-tree and try it again."
2643     echo
2644     die
2645     fi
2646     fi
2647    
2648     if [[ ${PKGTYPE} != virtual ]] && \
2649     [[ ${PKGTYPE} != sources ]]
2650     then
2651 niro 385 echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
2652 niro 226 build_doinstall ${PKGNAME}
2653     fi
2654    
2655     ## postinstall scripts
2656     if [ -n "$(typeset -f postinstall)" ]
2657     then
2658     echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
2659     postinstall
2660     unset postinstall
2661     fi
2662    
2663     # install a database entry
2664     install_database_entry \
2665     --pcat "${pcat}" \
2666     --pname "${pname}" \
2667     --pver "${pver}" \
2668     --pbuild "${pbuild}" \
2669     --pkgname "${PKGNAME}" \
2670     --pkgtype "${PKGTYPE}" \
2671     || die "error in mage_install() running install_database_entry()."
2672    
2673     # remove the package dir now
2674     if [ -d ${BUILDDIR}/${PKGNAME} ]
2675     then
2676     rm -rf ${BUILDDIR}/${PKGNAME}
2677     fi
2678    
2679     # rebuilds toplevel info node
2680     if [[ ${MAGE_INFO_REBUILD} = true ]]
2681     then
2682     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2683     echo -n "rebuilding top-level info node ... "
2684     ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2685     > ${MROOT}/usr/share/info/dir && \
2686     echo "done." || echo "failure."
2687     unset MAGE_INFO_REBUILD
2688     fi
2689    
2690     # rebuilds the enviroment with the content of /etc/env.d
2691     if [[ ${MAGE_ENV_REBUILD} = true ]]
2692     then
2693     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2694     echo -n "rebuilding environment ... "
2695     ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2696     echo "done." || echo "failure."
2697     unset MAGE_ENV_REBUILD
2698     fi
2699    
2700     xtitleclean
2701    
2702     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2703     echo -n "package "
2704     # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2705     # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2706     echo "successfully installed."
2707 niro 248
2708 niro 258 # unset these functions
2709     unset -f preinstall
2710     unset -f postinstall
2711     unset -f preremove
2712     unset -f postremove
2713 niro 226 }
2714    
2715     md5sum_packages()
2716     {
2717     local list="$@"
2718     local magefile
2719     local pcat
2720     local pname
2721     local pkgname
2722     local pkgfile
2723     local pkgtype
2724     local count_current
2725     local count_total
2726    
2727     # get count of total packages
2728     declare -i count_current=0
2729     declare -i count_total=0
2730    
2731     for i in ${list}; do (( count_total++ )); done
2732    
2733     for magefile in ${list}
2734     do
2735     pcat=$(magename2pcat ${magefile})
2736     pname=$(magename2pname ${magefile})
2737     pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
2738     md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
2739     pkgfile="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}"
2740     pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
2741    
2742     (( count_current++ ))
2743     xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
2744    
2745     # abort on virtual pkg
2746     if [[ ${pkgtype} = virtual ]]
2747     then
2748     echo -ne " ${COLBLUE}---${COLDEFAULT}"
2749     echo " !md5sum virtual (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2750     continue
2751     fi
2752    
2753     # abort on sources pkg
2754     if [[ ${pkgtype} = sources ]]
2755     then
2756     echo -ne " ${COLBLUE}---${COLDEFAULT}"
2757     echo " !md5sum sources (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2758     continue
2759     fi
2760    
2761     if [ -f "${md5file}" ]
2762     then
2763     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2764     echo -ne "checking md5sum (${count_current}/${count_total}): "
2765 niro 1273 ( cd ${PKGDIR}; md5sum -c ${md5file}) || die "md5 for ${pkgfile} failed"
2766 niro 226 else
2767     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2768     echo -e "!! no md5sum file found for ${pkgfile} :("
2769     fi
2770     done
2771    
2772     # add a crlf for a better view
2773     if [ ${count_total} -gt 1 ]; then echo; fi
2774     }
2775    
2776     ## uninstall_packages ulist
2777     uninstall_packages()
2778     {
2779     local list="$@"
2780     local pcat
2781     local pname
2782     local pver
2783     local pbuild
2784     local can_pcat
2785     local can_pname
2786     local can_ver_list
2787    
2788     if [[ -n ${MROOT} ]]
2789     then
2790     echo -ne ${COLRED}
2791     echo "!! uninstalling from MROOT=${MROOT}"
2792     echo -ne ${COLDEFAULT}
2793     echo
2794     fi
2795    
2796     # generate a candidates list
2797     for pkg in ${list}
2798     do
2799     pcat=$(dep2pcat ${pkg})
2800     pname=$(magename2pname ${pkg})
2801     pver=$(magename2pver ${pkg})
2802     pbuild=$(magename2pbuild ${pkg})
2803     can_pcat="${pcat}"
2804     can_pname="${pname}"
2805    
2806     if [ -z "${can_ver_list}" ]
2807     then
2808     can_ver_list=" ${pver}-${pbuild}"
2809     else
2810     can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
2811     fi
2812     done
2813    
2814     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2815     echo "following candidate(s) will be removed:"
2816     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2817 niro 240 echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
2818 niro 226 echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
2819 niro 501 echo
2820 niro 240 if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
2821     then
2822     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2823     echo "( Press [CTRL+C] to abort )"
2824     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2825     echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
2826     for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
2827     do
2828     echo -ne "${COLRED} ${i}${COLDEFAULT}"
2829     sleep 1
2830     done
2831     echo
2832     echo
2833     fi
2834 niro 226
2835     for pkg in ${list}
2836     do
2837     pcat=$(dep2pcat ${pkg})
2838     pname=$(magename2pname ${pkg})
2839     pver=$(magename2pver ${pkg})
2840     pbuild=$(magename2pbuild ${pkg})
2841    
2842     mage_uninstall \
2843     --pcat ${pcat} \
2844     --pname ${pname} \
2845     --pver ${pver} \
2846     --pbuild ${pbuild} \
2847     --count-total ${total_pkgs} \
2848     --count-current ${current_pkg} \
2849     ${src_install}
2850    
2851     # crlf for better view in VERBOSE mode
2852     #if [[ ${VERBOSE} = on ]]; then echo; fi
2853     echo
2854     done
2855     }
2856    
2857     mage_uninstall()
2858     {
2859     # local all possible vars of a mage file
2860     # to prevent bad issues
2861     local PKGNAME
2862     local STATE
2863     local DESCRIPTION
2864     local HOMEPAGE
2865     local DEPEND
2866     local SDEPEND
2867     local PROVIDE
2868     local PKGTYPE
2869     local preinstall
2870     local postinstall
2871 niro 248 local preremove
2872     local postremove
2873 niro 226
2874     local pcat
2875     local pname
2876     local pver
2877     local pbuild
2878     local magefile
2879     local i
2880    
2881     # very basic getops
2882     for i in $*
2883     do
2884     case $1 in
2885 niro 501 --pcat|-c) shift; pcat="$1" ;;
2886 niro 226 --pname|-n) shift; pname="$1" ;;
2887     --pver|-v) shift; pver="$1" ;;
2888 niro 501 --pbuild|-b) shift; pbuild="$1" ;;
2889 niro 226 esac
2890     shift
2891 niro 501 done
2892 niro 226
2893     # sanity checks; abort if not given
2894 niro 501 [ -z "${pcat}" ] && die "mage_uninstall() \$pcat not given."
2895 niro 226 [ -z "${pname}" ] && die "mage_uninstall() \$pname not given."
2896     [ -z "${pver}" ] && die "mage_uninstall() \$pver not given."
2897 niro 501 [ -z "${pbuild}" ] && die "mage_uninstall() \$pbuild not given."
2898 niro 226
2899     # check needed global vars
2900     [ -z "${MAGEDIR}" ] && die "mage_uninstall() \$MAGEDIR not set."
2901     [ -z "${INSTALLDB}" ] && die "mage_uninstall() \$INSTALLDB not set."
2902     [ -z "${BUILDDIR}" ] && die "mage_uninstall() \$BUILDDIR not set."
2903    
2904     xtitle "[ (${count_current}/${count_total}) Removing ${pcat}/${pname}-${pver}-${pbuild} ]"
2905     echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2906     echo -n "removing: "
2907     echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2908 niro 416 echo -e "${COLRED}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2909 niro 226
2910 niro 499 magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
2911 niro 226 source ${magefile}
2912    
2913     ## preremove scripts
2914     if [ -n "$(typeset -f preremove)" ]
2915     then
2916     echo -e " ${COLBLUE}***${COLDEFAULT} running preremove ... "
2917     preremove
2918     unset preremove
2919     fi
2920    
2921     # runs uninstall
2922     build_douninstall \
2923     --pcat "${pcat}" \
2924     --pname "${pname}" \
2925     --pver "${pver}" \
2926     --pbuild "${pbuild}"
2927    
2928     ## postremove scripts
2929     if [ -n "$(typeset -f postremove)" ]
2930     then
2931     echo -e " ${COLBLUE}***${COLDEFAULT} running postremove ... "
2932     postremove
2933     unset postremove
2934     fi
2935    
2936     # removes the database entry
2937     remove_database_entry \
2938     --pcat "${pcat}" \
2939     --pname "${pname}" \
2940     --pver "${pver}" \
2941     --pbuild "${pbuild}" \
2942     || die "error in mage_uninstall() running remove_database_entry()."
2943    
2944     # rebuilds toplevel info node
2945     if [[ ${MAGE_INFO_REBUILD} = true ]]
2946     then
2947     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2948     echo -n "rebuilding top-level info node ... "
2949     ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2950     > ${MROOT}/usr/share/info/dir && \
2951     echo "done." || echo "failure."
2952     unset MAGE_INFO_REBUILD
2953     fi
2954    
2955     # rebuilds the enviroment with the content of /etc/env.d
2956     if [[ ${MAGE_ENV_REBUILD} = true ]]
2957     then
2958     echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2959     echo -n "rebuilding environment ... "
2960     ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2961     echo "done." || echo "failure."
2962     unset MAGE_ENV_REBUILD
2963     fi
2964    
2965     echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2966     echo -n "package "
2967     # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2968     # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2969     echo "successfully removed."
2970 niro 248
2971 niro 258 # unset these functions
2972     unset -f preinstall
2973     unset -f postinstall
2974     unset -f preremove
2975     unset -f postremove
2976 niro 226 }
2977    
2978 niro 1209 show_etc_update_mesg()
2979     {
2980 niro 226 [ ${MAGE_PROTECT_COUNTER} -eq 0 ] && return 0
2981    
2982     echo
2983     echo -ne "${COLRED}"
2984     echo "Important:"
2985     echo -ne ${COLDEFAULT}
2986     echo "${MAGE_PROTECT_COUNTER} protected file(s) were installed."
2987     echo
2988     echo "Please run 'etc-update' to update your configuration files."
2989     echo
2990     }
2991 niro 237
2992     pkgsearch()
2993     {
2994     local string="$1"
2995     local result
2996     local pkg
2997     local pcat
2998     local pname
2999     local magefile
3000     local pver
3001     local pbuild
3002     local state
3003     local descriptiom
3004     local homepage
3005 niro 1648 local license
3006 niro 237 local i
3007     local all_installed
3008     local ipver
3009     local ipbuild
3010 niro 445 local latest_available
3011 niro 458 local depsfull
3012     local sdepsfull
3013     local deps
3014     local sdeps
3015     local dep
3016     local sign
3017 niro 237
3018     # only names no versions
3019 niro 391 result="$(find ${MAGEDIR} -mindepth 2 -maxdepth 2 -type d -name '*'${string}'*'| sed '/profiles/d' | sed '/includes/d')"
3020 niro 328 #result="$(find ${MAGEDIR} -type f -name '*'${string}'*'.mage | sort)"
3021 niro 237
3022     # nothing found
3023     [[ -z ${result} ]] && die "No package found containing '${string}' in the name."
3024    
3025     for pkg in ${result}
3026     do
3027     # dirty, but does the job
3028     pcat="$(magename2pcat ${pkg}/foo)"
3029     pname="$(magename2pname ${pkg}-foo-foo)"
3030    
3031     # get highest version available
3032     magefile=$(get_highest_magefile ${pcat} ${pname})
3033    
3034 niro 445 if [[ ! -z ${magefile} ]]
3035     then
3036     # now get all needed infos to print a nice output
3037     pver="$(magename2pver ${magefile})"
3038     pbuild="$(magename2pbuild ${magefile})"
3039     state="$(get_value_from_magefile STATE ${magefile})"
3040     description="$(get_value_from_magefile DESCRIPTION ${magefile})"
3041     homepage="$(get_value_from_magefile HOMEPAGE ${magefile})"
3042 niro 1648 license="$(get_value_from_magefile LICENSE ${magefile})"
3043    
3044 niro 445 # all installed
3045     for i in $(get_uninstall_candidates --pname ${pname} --pcat ${pcat})
3046     do
3047     ipver="$(magename2pver ${i})"
3048     ipbuild="$(magename2pbuild ${i})"
3049 niro 1648
3050 niro 445 if [[ -z ${all_installed} ]]
3051     then
3052     all_installed="${ipver}-${ipbuild}"
3053     else
3054     all_installed="${all_installed} ${ipver}-${ipbuild}"
3055     fi
3056     done
3057     [[ -z ${all_installed} ]] && all_installed="none"
3058 niro 1648
3059 niro 445 case ${state} in
3060     stable) state=${COLGREEN}"[s] ";;
3061     testing) state=${COLYELLOW}"[t] ";;
3062     unstable) state=${COLRED}"[u] ";;
3063     old) state=${COLGRAY}"[o] ";;
3064     esac
3065 niro 237
3066 niro 445 latest_available="${pver}-${pbuild}"
3067     else
3068     # package is masked
3069     state="${COLRED}[m] "
3070     latest_available="${COLRED}masked for this distribution.${COLDEFAULT}"
3071     fi
3072 niro 237
3073 niro 458 depsfull="$(get_value_from_magefile DEPEND ${magefile})"
3074     sdepsfull="$(get_value_from_magefile SDEPEND ${magefile})"
3075    
3076     while read sign dep
3077     do
3078     case ${dep} in
3079     "") continue;;
3080     esac
3081    
3082     deps="${deps} $(basename ${dep%-*})"
3083     done << EOF
3084     ${depsfull}
3085     EOF
3086    
3087     while read sign dep
3088     do
3089     case ${dep} in
3090     "") continue;;
3091     esac
3092    
3093     sdeps="${sdeps} $(basename ${dep%-*})"
3094     done << EOF
3095     ${sdepsfull}
3096     EOF
3097    
3098 niro 237 echo -e "${state}${pcat}/${pname}"${COLDEFAULT}
3099 niro 445 echo -e " Latest available: ${latest_available}"
3100 niro 237 echo " Installed versions: ${all_installed}"
3101     echo " Description: ${description}"
3102     echo " Homepage: ${homepage}"
3103 niro 1648 if [[ ! -z ${license} ]]
3104     then
3105     echo " License: ${license}"
3106     fi
3107 niro 458 echo " Depends: ${deps}"
3108     echo " SDepends: ${sdeps}"
3109 niro 237 echo
3110    
3111     unset pcat
3112     unset pname
3113     unset magefile
3114     unset pver
3115     unset pbuild
3116     unset state
3117     unset descriptiom
3118     unset homepage
3119     unset all_installed
3120     unset ipver
3121     unset ipbuild
3122 niro 458 unset depsfull
3123     unset sdepsfull
3124     unset deps
3125     unset sdeps
3126     unset dep
3127     unset sign
3128 niro 237 done
3129     }
3130 niro 249
3131     export_inherits()
3132     {
3133     local include="$1"
3134     shift
3135    
3136     while [ "$1" ]
3137     do
3138     local functions="$1"
3139    
3140     # sanity checks
3141     [ -z "${include}" ] && die "export_inherits(): \$include not given."
3142     [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
3143    
3144     eval "${functions}() { ${include}_${functions} ; }"
3145    
3146     # debug
3147 niro 1584 mqueryfeature "debug" && typeset -f "${functions}"
3148 niro 249
3149     shift
3150     done
3151     }
3152 niro 350
3153     mlibdir()
3154     {
3155     local libdir=lib
3156     [[ ${ARCH} = x86_64 ]] && libdir=lib64
3157    
3158     echo "${libdir}"
3159     }
3160 niro 370
3161     ## blacklisted ${magefile}
3162     blacklisted()
3163     {
3164     [[ -z ${MAGE_DISTRIBUTION} ]] && local MAGE_DISTRIBUTION=stable
3165    
3166     # compat
3167     [[ ${USE_UNSTABLE} = true ]] && local MAGE_DISTRIBUTION=unstable
3168     [[ ${USE_TESTING} = true ]] && local MAGE_DISTRIBUTION=testing
3169    
3170 niro 892 # support both types for the moment
3171     if [[ -f /etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION} ]]
3172     then
3173     local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION}"
3174     else
3175     local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}"
3176     fi
3177 niro 370
3178     # return 0 if the list not exist; nothin is masked
3179     [[ ! -f ${EXCLUDED} ]] && return 0
3180    
3181     local MAGEFILE="$1"
3182    
3183     local PCAT="$(magename2pcat ${MAGEFILE})"
3184     local PNAME="$(magename2pname ${MAGEFILE})"
3185     local PVER="$(magename2pver ${MAGEFILE})"
3186     local PBUILD="$(magename2pbuild ${MAGEFILE})"
3187    
3188     local EXPCAT EXPNAME EXPVER EXPBUILD
3189     while read EXPCAT EXPNAME EXPVER EXPBUILD
3190     do
3191     # ignore spaces and comments
3192     case "${EXPCAT}" in
3193     \#*|"") continue ;;
3194     esac
3195    
3196     # exclude full pver
3197     if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
3198     [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
3199     [[ -n ${PVER} ]] && [[ -n ${PBUILD} ]] &&
3200     [[ -n ${EXPVER} ]] && [[ -n ${EXPBUILD} ]]
3201     then
3202     [[ ${EXPCAT}/${EXPNAME}-${EXPVER}-${EXPBUILD} = ${PCAT}/${PNAME}-${PVER}-${PBUILD} ]] && return 1
3203     fi
3204    
3205     # exclude pcat/pname only
3206     if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
3207     [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
3208     [[ -z ${EXPVER} ]] && [[ -z ${EXPBUILD} ]]
3209     then
3210     [[ ${EXPCAT}/${EXPNAME} = ${PCAT}/${PNAME} ]] && return 1
3211     fi
3212     done << EOF
3213     $( cat ${EXCLUDED}; echo)
3214     EOF
3215    
3216     return 0
3217     }
3218    
3219 niro 1273 # need_busybox_support ${cmd}
3220     # return 0 (no error = needs busybox support) or return 1 (error = no busybox support required)
3221     need_busybox_support()
3222     {
3223     local cmd
3224     cmd="$1"
3225    
3226     if [[ -x /bin/busybox ]]
3227     then
3228     if [[ $(readlink $(which ${cmd})) = /bin/busybox ]]
3229     then
3230     # needs busybox support
3231     return 0
3232     fi
3233     fi
3234 niro 1318
3235     # no busybox
3236     return 1
3237 niro 1273 }
3238    
3239     # busybox_filter_wget_options ${wget_opts}
3240     busybox_filter_wget_options()
3241     {
3242     local opts="$@"
3243     local i
3244     local fixed_opts
3245    
3246     if need_busybox_support wget
3247     then
3248     for i in ${opts}
3249     do
3250     # show only the allowed ones
3251     case ${i} in
3252     -c|--continue) fixed_opts+=" -c" ;;
3253     -s|--spider) fixed_opts+=" -s" ;;
3254     -q|--quiet) fixed_opts+=" -q" ;;
3255     -O|--output-document) shift; fixed_opts+=" -O $1" ;;
3256     --header) shift; fixed_opts+=" --header $1" ;;
3257     -Y|--proxy) shift; fixed_opts+=" -Y $1" ;;
3258     -P) shift; fixed_opts+=" -P $1" ;;
3259     --no-check-certificate) fixed_opts+=" --no-check-certificate ${i}" ;;
3260     -U|--user-agent) shift; fixed_opts+=" -U ${i}" ;;
3261     # simply drop all other opts
3262     *) continue ;;
3263     esac
3264     done
3265    
3266     echo "${fixed_opts}"
3267     else
3268     echo "${opts}"
3269     fi
3270     }
3271 niro 1541
3272     have_root_privileges()
3273     {
3274     local retval
3275    
3276     if [[ $(id -u) = 0 ]]
3277     then
3278     retval=0
3279     else
3280     retval=1
3281     fi
3282    
3283     return ${retval}
3284     }
3285 niro 1584
3286     known_mage_feature()
3287     {
3288     local feature="$1"
3289     local retval
3290    
3291     case "${feature}" in
3292     autosvc|!autosvc) retval=0 ;;
3293     buildlog|!buildlog) retval=0 ;;
3294     ccache|!ccache) retval=0 ;;
3295     check|!check) retval=0 ;;
3296     compressdoc|!compressdoc) retval=0 ;;
3297 niro 1627 debug|!debug) retval=0 ;;
3298 niro 1584 distcc|!distcc) retval=0 ;;
3299     kernelsrcunpack|!kernelsrcunpack) retval=0 ;;
3300     libtool|!libtool) retval=0 ;;
3301     linuxsymlink|!linuxsymlink) retval=0 ;;
3302     pkgbuild|!pkgbuild) retval=0 ;;
3303 niro 1649 pkgdistrotag|!pkgdistrotag) retval=0 ;;
3304 niro 1584 purge|!purge) retval=0 ;;
3305     qalint|!qalint) retval=0 ;;
3306     regentree|!regentree) retval=0 ;;
3307 niro 1620 resume|!resume) retval=0 ;;
3308 niro 1584 srcpkgbuild|!srcpkgbuild) retval=0 ;;
3309     srcpkgtarball|!srcpkgtarball) retval=0 ;;
3310 niro 1627 static|!static) retval=0 ;;
3311     stepbystep|!stepbystep) retval=0 ;;
3312 niro 1584 strip|!strip) retval=0 ;;
3313 niro 1627 verbose|!verbose) retval=0 ;;
3314 niro 1584 *) retval=1 ;;
3315     esac
3316    
3317     return "${retval}"
3318     }
3319    
3320     load_mage_features()
3321     {
3322     for i in ${MAGE_FEATURES_GLOBAL[*]} ${MAGE_FEATURES[*]}
3323     do
3324     FVERBOSE=off msetfeature ${i}
3325     done
3326     }
3327    
3328     msetfeature()
3329     {
3330     local feature
3331     local count
3332     local i
3333     local found
3334    
3335     for feature in $@
3336     do
3337     found=0
3338     count="${#MAGE_FEATURES_CURRENT[*]}"
3339    
3340     if ! known_mage_feature "${feature}"
3341     then
3342 niro 1628 [[ ${FVERBOSE} = off ]] || echo -e "${COLRED}Unknown feature '${feature}', ignoring it${COLDEFAULT}"
3343 niro 1584 return 3
3344     fi
3345    
3346     for ((i=0; i<count; i++))
3347     do
3348     if [[ ${MAGE_FEATURES_CURRENT[${i}]} = ${feature} ]]
3349     then
3350 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' already enabled${COLDEFAULT}"
3351 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3352     found=1
3353     elif [[ ${MAGE_FEATURES_CURRENT[${i}]} = !${feature} ]]
3354     then
3355 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' currently disabled, enabling it!${COLDEFAULT}"
3356 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3357     found=1
3358     elif [[ ${MAGE_FEATURES_CURRENT[${i}]} = ${feature//!} ]]
3359     then
3360 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature//!}' currently enabled, disabling it!${COLDEFAULT}"
3361 niro 1584 MAGE_FEATURES_CURRENT[${i}]="${feature}"
3362     found=1
3363     fi
3364     done
3365    
3366     # if the feature was not found after proccessing the whole array
3367     # it was not declared. in this case enable it
3368     if [[ ${found} = 0 ]]
3369     then
3370 niro 1599 [[ ${FVERBOSE} = off ]] || echo -e "${COLBLUE}---${COLGREEN} Feature '${feature}' was not declared, enabling it!${COLDEFAULT}"
3371 niro 1584 MAGE_FEATURES_CURRENT=( ${MAGE_FEATURES_CURRENT[*]} "${feature}" )
3372     fi
3373    
3374     export MAGE_FEATURE_CURRENT
3375     done
3376     }
3377    
3378     mqueryfeature()
3379     {
3380     local feature="$1"
3381     local retval=1
3382     local i
3383    
3384     if known_mage_feature "${feature}"
3385     then
3386     for i in ${MAGE_FEATURES_CURRENT[*]}
3387     do
3388     if [[ ${i} = ${feature} ]]
3389     then
3390     retval=0
3391     break # found break here
3392     fi
3393     done
3394     else
3395 niro 1628 [[ ${FVERBOSE} = off ]] || echo -e "${COLRED}Unknown feature '${feature}', ignoring it${COLDEFAULT}"
3396 niro 1584 retval=3
3397     fi
3398    
3399     return ${retval}
3400     }
3401    
3402     mprintfeatures()
3403     {
3404     echo "Global features: ${MAGE_FEATURES_GLOBAL[*]}"
3405     echo "Local features: ${MAGE_FEATURES[*]}"
3406     echo "Current features: ${MAGE_FEATURES_CURRENT[*]}"
3407     }