Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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