Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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