Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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