Magellan Linux

Contents of /branches/mage-next/src/mage4.functions.sh.in

Parent Directory Parent Directory | Revision Log Revision Log


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