Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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