Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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