Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1084 - (show annotations) (download) (as text)
Mon Jun 28 18:08:15 2010 UTC (13 years, 10 months ago) by niro
File MIME type: application/x-sh
File size: 68212 byte(s)
-fixed whitespaces
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 a newer mage version is available
1229 is_newer_mage_version_available
1230 }
1231
1232 syncmage_tarball()
1233 {
1234 local latest_tarball
1235 local latest_md5
1236 local temp="$(mktemp -d)"
1237 local mirr mymirr
1238
1239 # try to get the md5 marked as latest on the server
1240 latest_md5="mage-latest.md5"
1241
1242 # try to get the tarball marked as latest on the server
1243 latest_tarball="mage-latest.tar.bz2"
1244
1245 for mirr in ${MIRRORS}
1246 do
1247 # path without distribution
1248 mymirr="${mirr%/*}"
1249
1250 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1251 echo "fetching latest md5 from ${mymirr} ..."
1252 wget \
1253 --passive-ftp \
1254 --tries 3 \
1255 --continue \
1256 --progress bar \
1257 --directory-prefix=${temp} \
1258 ${mymirr}/rsync/tarballs/${latest_md5}
1259
1260 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1261 echo "fetching latest tarball from ${mymirr} ..."
1262 wget \
1263 --passive-ftp \
1264 --tries 3 \
1265 --continue \
1266 --progress bar \
1267 --directory-prefix=${temp} \
1268 ${mymirr}/rsync/tarballs/${latest_tarball}
1269 if [[ $? = 0 ]]
1270 then
1271 break
1272 else
1273 continue
1274 fi
1275 done
1276
1277 if [[ -f ${temp}/${latest_tarball} ]]
1278 then
1279 # check md5
1280 if [[ ! -f ${temp}/${latest_md5} ]]
1281 then
1282 die "md5 is missing ... aborting"
1283 else
1284 ( cd ${temp}; md5sum --check ${lastest_md5} ) || die "md5 for ${lastest_tarball} failed"
1285 fi
1286
1287 if [[ -d ${MAGEDIR} ]]
1288 then
1289 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1290 echo "cleaning old mage-tree ${MAGEDIR}..."
1291 rm -rf ${MAGEDIR}
1292 fi
1293
1294 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1295 echo "updating mage-tree from tarball ..."
1296 # unpack in dirname of MAGEDIR, as the tarball has already the mage
1297 tar xjmf ${temp}/${latest_tarball} -C ${MAGEDIR%/*} || die "Unpacking tarball"
1298
1299 if [[ -d ${temp} ]]
1300 then
1301 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1302 echo "cleaning temp-files ..."
1303 rm -rf ${temp}
1304 fi
1305
1306 # check if a newer mage version is available
1307 is_newer_mage_version_available
1308 else
1309 die "Could not fetch the latest tarball ... aborting"
1310 fi
1311 }
1312
1313 cleanpkg()
1314 {
1315 if [ -d "${PKGDIR}" ]
1316 then
1317 echo -n "Removing downloaded packages... "
1318 rm -rf ${PKGDIR}/*
1319 echo "done."
1320 fi
1321 }
1322
1323 xtitle()
1324 {
1325 if [[ ${TERM} = xterm ]]
1326 then
1327 echo -ne "\033]0;Mage: $1\007"
1328 fi
1329 return 0
1330 }
1331
1332
1333 xtitleclean()
1334 {
1335 if [[ ${TERM} = xterm ]]
1336 then
1337 echo -ne "\033]0;\007"
1338 fi
1339 return 0
1340 }
1341
1342
1343 # cuts full pathnames or versioniezed names down to basename
1344 choppkgname()
1345 {
1346 #we want this only if full name was used
1347 if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ]
1348 then
1349 #cuts ARCH and PBUILD
1350 #ARCH comes from ${MAGERC}
1351 MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}-r*.::g")
1352
1353 #cuts version number
1354 MAGENAME=$(basename ${MAGENAME%-*} .mage)
1355 fi
1356 }
1357
1358 # get_categorie $PNAME, returns CATEGORIE
1359 # $1=pname
1360 # ret 0=ok, 1=not_found
1361 pname2pcat()
1362 {
1363 local pname="$1"
1364 local repo="$2"
1365 local pcat
1366 local categorie
1367
1368 for pcat in ${MAGEDIR}/*
1369 do
1370 if [ -d ${pcat}/${pname} ]
1371 then
1372 categorie=$(basename ${pcat})
1373 fi
1374 done
1375
1376 echo "${categorie}"
1377 }
1378
1379 # check_stable_package /path/to/foo.mage
1380 # returns 0=stable 1=unstable
1381 check_stable_package()
1382 {
1383 # first check if this magefile is not blacklisted
1384 blacklisted "$1" || return 1
1385
1386 local STATE
1387 STATE="$(get_value_from_magefile STATE "$1")"
1388
1389 # state testing
1390 if [[ ${USE_TESTING} = true ]] || [[ ${MAGE_DISTRIBUTION} = testing ]]
1391 then
1392 case ${STATE} in
1393 testing|stable) return 0 ;;
1394 *) return 1 ;;
1395 esac
1396 fi
1397
1398 # state unstable
1399 if [[ ${USE_UNSTABLE} = true ]] || [[ ${MAGE_DISTRIBUTION} = unstable ]]
1400 then
1401 case ${STATE} in
1402 unstable|testing|stable) return 0 ;;
1403 *) return 1 ;;
1404 esac
1405 fi
1406
1407 # no use_state given = stable
1408 case ${STATE} in
1409 stable) return 0 ;;
1410 *) return 1 ;;
1411 esac
1412 }
1413
1414
1415 # get_highest_magefile ${PCAT} ${PNAME}
1416 # fake at moment returns only stable pkgs (must set to be one)
1417 # return $HIGHEST_MAGEFILE
1418 get_highest_magefile()
1419 {
1420 local HIGHEST_MAGEFILE
1421 local PCAT="$1"
1422 local PNAME="$2"
1423 local magefile
1424
1425 # do not list the content of a directory, only the name (-d)
1426 for magefile in $(ls --format=single-column -v -d ${MAGEDIR}/${PCAT}/${PNAME}/*)
1427 do
1428 [[ -z ${magefile} ]] && continue
1429 # we exclude subdirs (for stuff like a md5sum dir)
1430 [[ -d ${magefile} ]] && continue
1431 if check_stable_package ${magefile}
1432 then
1433 HIGHEST_MAGEFILE=${magefile}
1434 #for debug only
1435 [[ ${MAGEDEBUG} = on ]] && echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}"
1436 fi
1437 done
1438
1439 # do not so anything
1440 # # stop here if HIGHEST_MAGEFILE is zero
1441 # # this package must be unstable or old
1442 # if [ -z "${HIGHEST_MAGEFILE}" ]
1443 # then
1444 # echo
1445 # echo -n "All packages named "
1446 # echo -en ${COLRED}\""${PKGNAME%-*-*-*}\""${COLDEFAULT}
1447 # echo -n " are marked "
1448 # echo -en ${COLRED}"*UNSTABLE*"${COLDEFAULT}
1449 # echo "."
1450 # echo "You need to declare USE_UNSTABLE=true to install this."
1451 # echo
1452 # echo "Example:"
1453 # echo " USE_UNSTABLE=true mage install ${PKGNAME%-*-*-*}"
1454 # echo
1455 # echo "Be warned that these packages are not stable and may cause serious problems."
1456 # echo "You should know what you are doing, so don't complain about any damage."
1457 # echo
1458 # return 1
1459 # fi
1460
1461 echo "${HIGHEST_MAGEFILE}"
1462 return 0
1463 }
1464
1465
1466 ###################################################
1467 # function is_config_protected #
1468 # is_config_protected /path/to/file #
1469 # #
1470 # returns: #
1471 # 0 - not protected #
1472 # 1 - error #
1473 # 2 - protected #
1474 # 3 - protected but masked #
1475 # 4 - protected but ignored #
1476 # #
1477 ###################################################
1478 is_config_protected()
1479 {
1480 local EXPFILE
1481 local TEST
1482 local PROTECTED
1483 local IFS
1484 local i
1485 local x
1486
1487 EXPFILE="${MROOT}$1"
1488
1489 # file does not exist; it can be written
1490 [[ ! -e ${EXPFILE} ]] && return 0
1491
1492 # to be safe; it may be '§'
1493 IFS=' '
1494
1495 # check if config protected
1496 for i in ${CONFIG_PROTECT}
1497 do
1498 # only replace $i in the beginning of the variable
1499 TEST="${EXPFILE/#${MROOT}${i}/Protected}"
1500 if [[ ${TEST} != ${EXPFILE} ]]
1501 then
1502 # file is config proteced
1503 PROTECTED=TRUE
1504
1505 # check if not masked
1506 for x in ${CONFIG_PROTECT_MASK}
1507 do
1508 TEST="${EXPFILE/#${MROOT}${x}/Protect_Masked}"
1509 if [[ ${TEST} != ${EXPFILE} ]]
1510 then
1511 PROTECTED=MASKED
1512 fi
1513 done
1514
1515 # check if not ignored
1516 for x in ${CONFIG_PROTECT_IGNORE}
1517 do
1518 TEST="${EXPFILE/#${MROOT}${x}/Protect_Ignored}"
1519 if [[ ${TEST} != ${EXPFILE} ]]
1520 then
1521 PROTECTED=IGNORED
1522 fi
1523 done
1524 fi
1525 done
1526
1527 unset IFS
1528
1529 case ${PROTECTED} in
1530 TRUE)
1531 #echo "I'm protected"
1532 return 2
1533 ;;
1534 MASKED)
1535 #echo "I'm protected, but masked - delete me"
1536 return 3
1537 ;;
1538 IGNORED)
1539 #echo "I'm protected, but ignored - keep me, del update"
1540 return 4
1541 ;;
1542 *)
1543 #echo "delete me"
1544 return 0
1545 ;;
1546 esac
1547 }
1548
1549
1550 ###################################################
1551 # function count_protected_files #
1552 # count_protected_files /path/to/file #
1553 # #
1554 # note: prints number of protected files #
1555 # exp: 0012 #
1556 ###################################################
1557 count_protected_files()
1558 {
1559 local file="$1"
1560 local dirname="${file%/*}"
1561 local filename="${file##*/}"
1562 local count
1563 local output
1564 local i
1565
1566 declare -i count=0
1567
1568 # check if there are already protected files
1569 for oldpretected in $(find ${dirname} -iname "._cfg????_${filename}" |
1570 sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" |
1571 sort -t'%' -k3 -k2 | cut -f1 -d'%')
1572 do
1573 count=$(echo ${oldpretected} | cut -d_ -f2 | sed -e "s:cfg::")
1574 done
1575 (( count ++ ))
1576
1577 # fill output up with zeros
1578 for (( i=${#count}; i < 4; i++ )); do output="${output}0"; done
1579 output="${output}${count}"
1580
1581 echo "${output}"
1582 }
1583
1584 # call with
1585 # 'get_uninstall_candidates (--pcat cat --protected pcat/pfull) --pname PNAME'
1586 # returns /path/to/magefile(s)
1587 get_uninstall_candidates()
1588 {
1589 local search_pname
1590 local pkg
1591 local pcat
1592 local pname
1593 local pver
1594 local pbuild
1595 local list
1596 local pcatdir
1597 local protected
1598 local i
1599
1600 # very basic getops
1601 for i in $*
1602 do
1603 case $1 in
1604 --pcat|-c) shift; pcatdir="$1" ;;
1605 --pname|-n) shift; search_pname="$1" ;;
1606 --protected|-p) shift; protected="$1" ;;
1607 esac
1608 shift
1609 done
1610
1611 # it's not good to complain here about empty pnames; better to continue later anyway
1612 # # sanity checks; abort if not given
1613 # [ -z "${search_pname}" ] && die "get_uninstall_candidates() \$search_pname not given."
1614
1615
1616 # check needed global vars
1617 [ -z "${INSTALLDB}" ] && die "get_uninstall_candidates() \$INSTALLDB not set."
1618
1619 # set pcatdir to '*' if empty
1620 [ -z "${pcatdir}" ] && pcatdir='*'
1621
1622 for pkg in ${MROOT}${INSTALLDB}/${pcatdir}/*
1623 do
1624 # abort if not a dir
1625 [ ! -d ${pkg} ] && continue
1626
1627 pname="$(magename2pname ${pkg})"
1628
1629 if [[ ${search_pname} = ${pname} ]]
1630 then
1631 pcat="$(magename2pcat ${pkg} installdb)"
1632 pver="$(magename2pver ${pkg})"
1633 pbuild="$(magename2pbuild ${pkg})"
1634
1635 # exclude proteced
1636 [[ ${protected} = ${pcat}/${pname}-${pver}-${pbuild} ]] && continue
1637
1638 list="${list} ${pcat}/${pname}-${pver}-${pbuild}"
1639 fi
1640 done
1641
1642 echo "${list}"
1643 }
1644
1645 # reads virtualdb file
1646 #$1 = virtualname; $2 commands: showpkgs, showline
1647 #return 0 == installed -> shows installed pkg as well
1648 #return 1 == not installed
1649 virtuals_read()
1650 {
1651 local virtualname="$1"
1652 local command="$2"
1653 local virtline
1654 local line x i
1655
1656 # parse file to get virtual_name line
1657 IFS=$'\n'
1658 for line in $(< ${MROOT}${VIRTUALDB_FILE})
1659 do
1660 IFS=$' '
1661 for x in ${line}
1662 do
1663 if [[ ${x} = ${virtualname} ]]
1664 then
1665 virtline="${line}"
1666 [[ ${command} = showline ]] && echo "${line}"
1667 fi
1668 done
1669 IFS=$'\n'
1670 done
1671
1672 unset IFS
1673
1674 # now read the packages linked to VIRTUAL_NAME and output them
1675 if [ -n "${virtline}" ]
1676 then
1677 if [[ ${command} = showpkgs ]]
1678 then
1679 declare -i x=0
1680 for i in ${virtline}
1681 do
1682 if [ ${x} -ge 1 ]
1683 then
1684 echo "${i}"
1685 fi
1686 ((x++))
1687 done
1688 fi
1689 return 0
1690 fi
1691 return 1
1692 }
1693
1694
1695 #add pkg to virtualdb
1696 # $1 == virtualname $2= pkgname
1697 # retvals: 0=ok,added; 1=error; 3=pkg already in virtual
1698 virtuals_add()
1699 {
1700 local virtualname="$1"
1701 local pkgname="$2"
1702 local oldline
1703 local line i
1704 local installed_file
1705 local OLDIFS
1706
1707 if virtuals_read ${virtualname}
1708 then
1709 # make sure ${PKG_NAME} is *not* in ${VIRTUAL_NAME} already
1710 for i in $(virtuals_read ${virtualname} showpkgs)
1711 do
1712 if [[ ${i} = ${pkgname} ]]
1713 then
1714 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1715 echo "${pkgname} already linked as ${virtualname} ..."
1716 #return 3
1717 return 0
1718 fi
1719 done
1720
1721 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
1722 echo "updating ${virtualname} entry with ${pkgname} ..."
1723 oldline="$(virtuals_read ${virtualname} showline)"
1724
1725 # make a backup
1726 mv ${MROOT}${VIRTUALDB_FILE} ${MROOT}${VIRTUALDB_FILE}.old
1727
1728 OLDIFS="${IFS}"
1729 IFS=$'\n'
1730 for line in $(< ${MROOT}${VIRTUALDB_FILE}.old)
1731 do
1732 # if the right line, append ${pkgname}, else do nothing
1733 if [[ ${line} = ${oldline} ]]
1734 then
1735 echo "${line} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
1736 else
1737 echo "${line}" >> ${MROOT}${VIRTUALDB_FILE}
1738 fi
1739 done
1740 # unset IFS
1741 IFS="${OLDIFS}"
1742 else
1743 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
1744 echo "register ${pkgname} as ${virtualname} ..."
1745 echo "${virtualname} ${pkgname}" >> ${MROOT}${VIRTUALDB_FILE}
1746 fi
1747
1748 return 0
1749 }
1750
1751 #deletes pakages from virtual database
1752 #$1 virtualname; $2 pkgname
1753 virtuals_del() {
1754
1755 local virtualname="$1"
1756 local pkgname="$2"
1757 local oldline
1758 local method
1759 local line i x
1760 local pkg_installed
1761 local OLDIFS
1762
1763 # first check if exists
1764 if virtuals_read ${virtualname}
1765 then
1766 # get method -> delall or update and check if ${PKG_NAME} exists in ${VIRTUAL_NAME}
1767 declare -i x=0
1768 for i in $(virtuals_read ${virtualname} showpkgs)
1769 do
1770 if [[ ${i} = ${pkgname} ]]
1771 then
1772 pkg_installed=true
1773 fi
1774 ((x++))
1775 done
1776
1777 # abort if not installed
1778 if [[ ${pkg_installed} != true ]]
1779 then
1780 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1781 echo "${pkgname} does not exists in ${virtualname}."
1782 return 0
1783 fi
1784
1785 if [ ${x} -ge 2 ]
1786 then
1787 method=update
1788 else
1789 method=delall
1790 fi
1791
1792 # get the complete line
1793 oldline="$(virtuals_read ${virtualname} showline)"
1794
1795 # make a backup of the db
1796 mv ${VIRTUALDB_FILE} ${VIRTUALDB_FILE}.old
1797
1798 # parse virtualdb
1799 OLDIFS="${IFS}"
1800 IFS=$'\n'
1801 for line in $(< ${VIRTUALDB_FILE}.old)
1802 do
1803 if [[ ${line} = ${oldline} ]]
1804 then
1805 #delall or update?
1806 case ${method} in
1807 update)
1808 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
1809 echo "Unlinking ${pkgname} from ${virtualname} in virtual database ..."
1810 # del PKG_NAME from line
1811 echo "${line/ ${pkgname}/}" >> ${VIRTUALDB_FILE}
1812 ;;
1813 delall)
1814 echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
1815 echo "Deleting ${virtualname} in virtual database ..."
1816 # continue; do not write anything
1817 continue
1818 ;;
1819 esac
1820 else
1821 echo "${line}" >> ${VIRTUALDB_FILE}
1822 fi
1823 done
1824 # unset IFS
1825 IFS="${OLDIFS}"
1826 else
1827 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
1828 echo "${virtualname} does not exists in virtual database."
1829 fi
1830 }
1831
1832 # gets real pkgname from virtuals.default
1833 #$1=VIRTUAL_NAME; returns PKG_NAME
1834 default_virtualname_to_pkgname()
1835 {
1836 local VIRTUAL_NAME PKG_NAME db_virtualname db_pkgname
1837
1838 VIRTUAL_NAME=$1
1839
1840 while read db_virtualname db_pkgname
1841 do
1842 if [ "${db_virtualname}" == "${VIRTUAL_NAME}" ]
1843 then
1844 PKG_NAME="${db_pkgname}"
1845 fi
1846 done << EOF
1847 $(< ${VIRTUALDB_DEFAULTS})
1848 EOF
1849
1850 if [ -n "${PKG_NAME}" ]
1851 then
1852 echo "${PKG_NAME}"
1853 fi
1854 }
1855
1856 minclude()
1857 {
1858 local i
1859
1860 if [[ -n $* ]]
1861 then
1862 for i in $*
1863 do
1864 [[ ${MAGEDEBUG} = on ]] && \
1865 echo "--- Including ${MAGEDIR}/include/${i}.minc"
1866 source ${MAGEDIR}/include/${i}.minc
1867 done
1868 [[ ${MAGEDEBUG} = on ]] && echo
1869 fi
1870 }
1871
1872 sminclude()
1873 {
1874 local i
1875
1876 if [[ -n $* ]]
1877 then
1878 for i in $*
1879 do
1880 echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc"
1881 source ${SMAGESCRIPTSDIR}/include/${i}.sminc
1882 done
1883 echo
1884 fi
1885 }
1886
1887 # checks if an newer mage version is available
1888 is_newer_mage_version_available()
1889 {
1890 local newest_mage
1891 local installed_mage
1892
1893 newest_mage="$(basename $(get_highest_magefile app-mage mage) .mage)"
1894 installed_mage="$(magequery -n mage | cut -d' ' -f5)"
1895
1896 if [[ ${newest_mage} > ${installed_mage} ]]
1897 then
1898 echo
1899 echo -en ${COLRED}"An update for your packetmanager is available. "${COLDEFAULT}
1900 echo -e ${COLBLUE}"[ ${newest_mage} ]"${COLDEFAULT}
1901 echo "It is recommened to install this newer version"
1902 echo "or your current system installation may break."
1903 echo
1904 echo -en "Please update mage by running "
1905 echo -e ${COLGREEN}"'mage install mage'"${COLDEFAULT}
1906 echo
1907 fi
1908 }
1909
1910
1911 # returns pname from pkgname
1912 # pkgname2pname $PKGNAME
1913 pkgname2pname()
1914 {
1915 local pname
1916
1917 pname="${1%-*-*-*}"
1918 echo "${pname}"
1919 }
1920
1921 # returns pver from pkgname
1922 # pkgname2pver $PKGNAME
1923 pkgname2pver()
1924 {
1925 local i pver
1926
1927 i="${1/$(pkgname2pname $1)-/}"
1928 pver="${i%-*-*}"
1929 echo "${pver}"
1930 }
1931
1932 # returns pbuild from pkgname
1933 # pkgname2pbuild $PKGNAME
1934 pkgname2pbuild()
1935 {
1936 local pbuild
1937
1938 pbuild="${1##*-}"
1939 echo "${pbuild}"
1940 }
1941
1942 # returns parch from pkgname
1943 # pkgname2parch $PKGNAME
1944 pkgname2parch()
1945 {
1946 local i x parch
1947
1948 i="${1%-*-*}-"
1949 x="${1%-*}"
1950 parch="${x/${i}/}"
1951 echo "${parch}"
1952 }
1953
1954 # returns pname from magename
1955 # magename2pname /PATH/TO/MAGE/FILE
1956 magename2pname()
1957 {
1958 local i pname
1959
1960 i="$(basename $1 .mage)"
1961 pname="${i%-*-*}"
1962 echo "${pname}"
1963 }
1964
1965 # returns pver from magename
1966 # magename2pver /PATH/TO/MAGE/FILE
1967 magename2pver()
1968 {
1969 local i pver
1970
1971 i="$(basename $1 .mage)"
1972 i="${i/$(magename2pname $1)-/}"
1973 pver="${i%-*}"
1974 echo "${pver}"
1975 }
1976
1977 # returns pbuild from magename
1978 # magename2pbuild /PATH/TO/MAGE/FILE
1979 magename2pbuild()
1980 {
1981 local i pbuild
1982
1983 i="$(basename $1 .mage)"
1984 pbuild="${i##*-}"
1985 echo "${pbuild}"
1986 }
1987
1988 # returns pcat from magename
1989 # magename2pcat /PATH/TO/MAGE/FILE
1990 magename2pcat()
1991 {
1992 local i pcat
1993
1994 if [[ ${2} = installdb ]]
1995 then
1996 # go 1 dir back
1997 i="${1%/*}"
1998 else
1999 # go 2 dirs back
2000 i="${1%/*/*}"
2001 fi
2002
2003 # get basename
2004 pcat="${i##*/}"
2005 echo "${pcat}"
2006 }
2007
2008 # returns pcat from DEPEND (without operand ! PCAT/PNAME-VERSION)
2009 # dep2pcat DEPEND
2010 dep2pcat()
2011 {
2012 local pcat
2013
2014 pcat="${1%/*}"
2015 echo "${pcat}"
2016 }
2017
2018 # returns pname from DEPEND (without operand ! PCAT/PNAME-VERSION)
2019 # $2=virtual is used to resolv VDEPEND from virtual packages
2020 # dep2pcat DEPEND (virtual)
2021 dep2pname()
2022 {
2023 local pname
2024
2025 pname="${1##*/}"
2026
2027 # cut version only if not virtual or it will cut the name
2028 if [[ $(dep2pcat $1) != virtual ]] && \
2029 [[ $2 != virtual ]]
2030 then
2031 pname="${pname%-*}"
2032 fi
2033
2034 echo "${pname}"
2035 }
2036
2037 dep2highest_magefile()
2038 {
2039 local pcat
2040 local pname
2041 local magefile
2042 local installed_virtuals
2043
2044 pcat="$(dep2pcat $1)"
2045 pname="$(dep2pname $1)"
2046
2047 if [[ ${pcat} = virtual ]]
2048 then
2049 # first check if virtual is already installed
2050 installed_virtuals="$(virtuals_read ${pcat}/${pname} showpkgs)"
2051 if [ -n "${installed_virtuals}" ]
2052 then
2053 for vpkg in ${installed_virtuals}
2054 do
2055 realpkgname="${vpkg}"
2056 virtualpkgname="${pcat}/${pname}"
2057 pcat="$(dep2pcat ${realpkgname})"
2058 pname="$(dep2pname ${realpkgname} virtual)"
2059 done
2060 else
2061 # choose one from virtualdb defaults (virtuals.defaults)
2062 realpkgname="$(default_virtualname_to_pkgname ${pcat}/${pname})"
2063 virtualpkgname="${pcat}/${pname}"
2064 pcat="$(dep2pcat ${realpkgname})"
2065 pname="$(dep2pname ${realpkgname} virtual)"
2066 fi
2067 fi
2068
2069 magefile="$(get_highest_magefile ${pcat} ${pname})"
2070 echo "${magefile}"
2071 }
2072
2073 # is_installed ${PCAT}/${PNAME}-${PVER}-${PBUILD}
2074 is_installed()
2075 {
2076 local fullpkgname="$1"
2077
2078 # return 0 if installed
2079 [ -d ${MROOT}${INSTALLDB}/${fullpkgname} ] && return 0
2080
2081 return 1
2082 }
2083
2084 install_packages()
2085 {
2086 local list="$@"
2087 local pkg
2088 local pcat
2089 local pname
2090 local pver
2091 local pbuild
2092 local total_pkgs
2093 local current_pkg
2094 local src_install
2095 local uninstall_list
2096
2097 # check for --src-install
2098 if [[ $1 = --src-install ]]
2099 then
2100 # remove --src-install from list
2101 list=${list/--src-install/}
2102 # enable src-install
2103 src_install="--src-install"
2104 fi
2105
2106 # reset MAGE_PROTECT_COUNTER
2107 declare -i MAGE_PROTECT_COUNTER=0
2108 export MAGE_PROTECT_COUNTER
2109
2110 # get count of total packages
2111 declare -i total_pkgs=0
2112 declare -i current_pkg=0
2113 for i in ${list}; do (( total_pkgs++ )); done
2114
2115 echo
2116
2117 if [[ -n ${MROOT} ]]
2118 then
2119 echo -ne ${COLRED}
2120 echo "!! installing in MROOT=${MROOT}"
2121 echo -ne ${COLDEFAULT}
2122 echo
2123 fi
2124
2125 for pkg in ${list}
2126 do
2127 (( current_pkg++ ))
2128 pcat=$(magename2pcat ${pkg})
2129 pname=$(magename2pname ${pkg})
2130 pver=$(magename2pver ${pkg})
2131 pbuild=$(magename2pbuild ${pkg})
2132
2133 mage_install \
2134 --pcat ${pcat} \
2135 --pname ${pname} \
2136 --pver ${pver} \
2137 --pbuild ${pbuild} \
2138 --count-total ${total_pkgs} \
2139 --count-current ${current_pkg} \
2140 ${src_install}
2141
2142 # check for allready installed packages and remove them
2143 # except the package we have installed
2144 uninstall_list="$(get_uninstall_candidates \
2145 --pcat "${pcat}" \
2146 --pname "${pname}" \
2147 --protected ${pcat}/${pname}-${pver}-${pbuild})"
2148
2149 # uninstall all packges in uninstall_list if not empty
2150 if [ -n "${uninstall_list}" ]
2151 then
2152 echo
2153 uninstall_packages ${uninstall_list} \
2154 || die "install_packges() uninstalling not-needed."
2155 fi
2156
2157 # crlf for better view in VERBOSE mode
2158 #if [[ ${VERBOSE} = on ]]; then echo; fi
2159 echo
2160 done
2161
2162 #echo "DEBUG MAGE_PROTECT_COUNTER=${MAGE_PROTECT_COUNTER}"
2163 show_etc_update_mesg
2164 }
2165
2166 # get_value_from_magefile VARIABLE
2167 # returns the content of this VAR
2168 get_value_from_magefile()
2169 {
2170 local var="$1"
2171 local magefile="$2"
2172 local value
2173
2174 [[ -z ${var} ]] && return 1
2175 [[ -z ${magefile} ]] && return 1
2176
2177 # local all possible vars of a mage file
2178 # to prevent bad issues
2179 local PKGNAME
2180 local STATE
2181 local DESCRIPTION
2182 local HOMEPAGE
2183 local DEPEND
2184 local SDEPEND
2185 local PROVIDE
2186 local PKGTYPE
2187 local MAGE_TARGETS
2188 local SPLIT_PACKAGE_BASE
2189 local preinstall
2190 local postinstall
2191 local preremove
2192 local postremove
2193
2194 # sanity checks
2195 [ -f ${magefile} ] && source ${magefile} || \
2196 die "get_value_from_magefile: ${magefile} not found."
2197 [ -z "${var}" ] && die "get_value_from_magefile: \$var not given."
2198
2199 source ${magefile}
2200 eval value=\$$(echo ${var})
2201 echo "${value}"
2202
2203 # unset these functions
2204 unset -f preinstall
2205 unset -f postinstall
2206 unset -f preremove
2207 unset -f postremove
2208 }
2209
2210 mage_install()
2211 {
2212 # local all possible vars of a mage file
2213 # to prevent bad issues
2214 local PKGNAME
2215 local STATE
2216 local DESCRIPTION
2217 local HOMEPAGE
2218 local DEPEND
2219 local SDEPEND
2220 local PROVIDE
2221 local PKGTYPE
2222 local preinstall
2223 local postinstall
2224 local preremove
2225 local postremove
2226
2227 local pcat
2228 local pname
2229 local pver
2230 local pbuild
2231 local count_total
2232 local count_current
2233 local magefile
2234 local src_install
2235 local i
2236
2237 # very basic getops
2238 for i in $*
2239 do
2240 case $1 in
2241 --pcat|-c) shift; pcat="$1" ;;
2242 --pname|-n) shift; pname="$1" ;;
2243 --pver|-v) shift; pver="$1" ;;
2244 --pbuild|-b) shift; pbuild="$1" ;;
2245 --count-total) shift; count_total="$1" ;;
2246 --count-current) shift; count_current="$1" ;;
2247 --src-install|-s) shift; src_install=true ;;
2248 esac
2249 shift
2250 done
2251
2252 # sanity checks; abort if not given
2253 [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
2254 [ -z "${pname}" ] && die "mage_install() \$pname not given."
2255 [ -z "${pver}" ] && die "mage_install() \$pver not given."
2256 [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
2257
2258 # check needed global vars
2259 [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
2260 [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
2261 [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
2262
2263 xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
2264 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2265 echo -n "installing (${count_current}/${count_total}): "
2266 echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2267 echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2268
2269 magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
2270 source ${magefile}
2271
2272 # abort on sources if no srcinstall
2273 if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
2274 then
2275 echo
2276 echo -e "This Package is a Source Package."
2277 echo
2278 echo -e "Only 'srcinstall' works with this type of packages"
2279 echo -en "If you have done a srcinstall before, "
2280 echo -e "you will find the files in /usr/src."
2281 echo
2282 exit 1
2283 fi
2284
2285 ## preinstall scripts
2286 if [ -n "$(typeset -f preinstall)" ]
2287 then
2288 echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
2289 preinstall
2290 unset preinstall
2291 fi
2292
2293 if [[ ${src_install} = true ]]
2294 then
2295 local smage2file
2296 # check needed global vars
2297 [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
2298 [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
2299 [ -z "${BINDIR}" ] && die "\$BINDIR not set."
2300
2301 # build the package first
2302 if [[ ${MAGEDEBUG} = on ]]
2303 then
2304 echo M:${pname}
2305 echo V:${pver}
2306 echo B:${pbuild}
2307 fi
2308
2309 if [[ -n ${MAGE_TARGETS} ]]
2310 then
2311 # basic svn compat
2312 if [[ -d ${SMAGESCRIPTSDIR}/trunk ]]
2313 then
2314 for i in ${SMAGESCRIPTSDIR}/trunk/*/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2315 do
2316 smage2file="${i}"
2317 done
2318 else
2319 smage2file=${SMAGESCRIPTSDIR}/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2320 fi
2321
2322 elif [[ -n ${SPLIT_PACKAGE_BASE} ]]
2323 then
2324 # basic svn compat
2325 if [[ -d ${SMAGESCRIPTSDIR}/trunk ]]
2326 then
2327 for i in ${SMAGESCRIPTSDIR}/trunk/*/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2328 do
2329 smage2file="${i}"
2330 done
2331 else
2332 smage2file=${SMAGESCRIPTSDIR}/${SPLIT_PACKAGE_BASE}/${SPLIT_PACKAGE_BASE}-${pver}-${pbuild}.smage2
2333 fi
2334
2335 else
2336 # basic svn compat
2337 if [[ -d ${SMAGESCRIPTSDIR}/trunk ]]
2338 then
2339 for i in ${SMAGESCRIPTSDIR}/trunk/*/${pname}/${pname}-${pver}-${pbuild}.smage2
2340 do
2341 smage2file="${i}"
2342 done
2343 else
2344 smage2file=${SMAGESCRIPTSDIR}/${pname}/${pname}-${pver}-${pbuild}.smage2
2345 fi
2346 fi
2347
2348 if [ -f "${smage2file}" ]
2349 then
2350 echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
2351 smage2 ${smage2file} || die "compile failed"
2352 else
2353 echo
2354 echo "$(basename ${SMAGEFILE}) not found."
2355 echo "update your smage-tree and try it again."
2356 echo
2357 die
2358 fi
2359 fi
2360
2361 if [[ ${PKGTYPE} != virtual ]] && \
2362 [[ ${PKGTYPE} != sources ]]
2363 then
2364 echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
2365 build_doinstall ${PKGNAME}
2366 fi
2367
2368 ## postinstall scripts
2369 if [ -n "$(typeset -f postinstall)" ]
2370 then
2371 echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
2372 postinstall
2373 unset postinstall
2374 fi
2375
2376 # install a database entry
2377 install_database_entry \
2378 --pcat "${pcat}" \
2379 --pname "${pname}" \
2380 --pver "${pver}" \
2381 --pbuild "${pbuild}" \
2382 --pkgname "${PKGNAME}" \
2383 --pkgtype "${PKGTYPE}" \
2384 || die "error in mage_install() running install_database_entry()."
2385
2386 # remove the package dir now
2387 if [ -d ${BUILDDIR}/${PKGNAME} ]
2388 then
2389 rm -rf ${BUILDDIR}/${PKGNAME}
2390 fi
2391
2392 # rebuilds toplevel info node
2393 if [[ ${MAGE_INFO_REBUILD} = true ]]
2394 then
2395 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2396 echo -n "rebuilding top-level info node ... "
2397 ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2398 > ${MROOT}/usr/share/info/dir && \
2399 echo "done." || echo "failure."
2400 unset MAGE_INFO_REBUILD
2401 fi
2402
2403 # rebuilds the enviroment with the content of /etc/env.d
2404 if [[ ${MAGE_ENV_REBUILD} = true ]]
2405 then
2406 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2407 echo -n "rebuilding environment ... "
2408 ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2409 echo "done." || echo "failure."
2410 unset MAGE_ENV_REBUILD
2411 fi
2412
2413 xtitleclean
2414
2415 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2416 echo -n "package "
2417 # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2418 # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2419 echo "successfully installed."
2420
2421 # unset these functions
2422 unset -f preinstall
2423 unset -f postinstall
2424 unset -f preremove
2425 unset -f postremove
2426 }
2427
2428 md5sum_packages()
2429 {
2430 local list="$@"
2431 local magefile
2432 local pcat
2433 local pname
2434 local pkgname
2435 local pkgfile
2436 local pkgtype
2437 local count_current
2438 local count_total
2439
2440 # get count of total packages
2441 declare -i count_current=0
2442 declare -i count_total=0
2443
2444 for i in ${list}; do (( count_total++ )); done
2445
2446 for magefile in ${list}
2447 do
2448 pcat=$(magename2pcat ${magefile})
2449 pname=$(magename2pname ${magefile})
2450 pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
2451 md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
2452 pkgfile="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}"
2453 pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
2454
2455 (( count_current++ ))
2456 xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
2457
2458 # abort on virtual pkg
2459 if [[ ${pkgtype} = virtual ]]
2460 then
2461 echo -ne " ${COLBLUE}---${COLDEFAULT}"
2462 echo " !md5sum virtual (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2463 continue
2464 fi
2465
2466 # abort on sources pkg
2467 if [[ ${pkgtype} = sources ]]
2468 then
2469 echo -ne " ${COLBLUE}---${COLDEFAULT}"
2470 echo " !md5sum sources (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2471 continue
2472 fi
2473
2474 if [ -f "${md5file}" ]
2475 then
2476 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2477 echo -ne "checking md5sum (${count_current}/${count_total}): "
2478 ( cd ${PKGDIR}; md5sum --check ${md5file}) || die "md5 for ${pkgfile} failed"
2479 else
2480 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2481 echo -e "!! no md5sum file found for ${pkgfile} :("
2482 fi
2483 done
2484
2485 # add a crlf for a better view
2486 if [ ${count_total} -gt 1 ]; then echo; fi
2487 }
2488
2489 ## uninstall_packages ulist
2490 uninstall_packages()
2491 {
2492 local list="$@"
2493 local pcat
2494 local pname
2495 local pver
2496 local pbuild
2497 local can_pcat
2498 local can_pname
2499 local can_ver_list
2500
2501 if [[ -n ${MROOT} ]]
2502 then
2503 echo -ne ${COLRED}
2504 echo "!! uninstalling from MROOT=${MROOT}"
2505 echo -ne ${COLDEFAULT}
2506 echo
2507 fi
2508
2509 # generate a candidates list
2510 for pkg in ${list}
2511 do
2512 pcat=$(dep2pcat ${pkg})
2513 pname=$(magename2pname ${pkg})
2514 pver=$(magename2pver ${pkg})
2515 pbuild=$(magename2pbuild ${pkg})
2516 can_pcat="${pcat}"
2517 can_pname="${pname}"
2518
2519 if [ -z "${can_ver_list}" ]
2520 then
2521 can_ver_list=" ${pver}-${pbuild}"
2522 else
2523 can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
2524 fi
2525 done
2526
2527 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2528 echo "following candidate(s) will be removed:"
2529 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2530 echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
2531 echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
2532 echo
2533 if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
2534 then
2535 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2536 echo "( Press [CTRL+C] to abort )"
2537 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2538 echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
2539 for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
2540 do
2541 echo -ne "${COLRED} ${i}${COLDEFAULT}"
2542 sleep 1
2543 done
2544 echo
2545 echo
2546 fi
2547
2548 for pkg in ${list}
2549 do
2550 pcat=$(dep2pcat ${pkg})
2551 pname=$(magename2pname ${pkg})
2552 pver=$(magename2pver ${pkg})
2553 pbuild=$(magename2pbuild ${pkg})
2554
2555 mage_uninstall \
2556 --pcat ${pcat} \
2557 --pname ${pname} \
2558 --pver ${pver} \
2559 --pbuild ${pbuild} \
2560 --count-total ${total_pkgs} \
2561 --count-current ${current_pkg} \
2562 ${src_install}
2563
2564 # crlf for better view in VERBOSE mode
2565 #if [[ ${VERBOSE} = on ]]; then echo; fi
2566 echo
2567 done
2568 }
2569
2570 mage_uninstall()
2571 {
2572 # local all possible vars of a mage file
2573 # to prevent bad issues
2574 local PKGNAME
2575 local STATE
2576 local DESCRIPTION
2577 local HOMEPAGE
2578 local DEPEND
2579 local SDEPEND
2580 local PROVIDE
2581 local PKGTYPE
2582 local preinstall
2583 local postinstall
2584 local preremove
2585 local postremove
2586
2587 local pcat
2588 local pname
2589 local pver
2590 local pbuild
2591 local magefile
2592 local i
2593
2594 # very basic getops
2595 for i in $*
2596 do
2597 case $1 in
2598 --pcat|-c) shift; pcat="$1" ;;
2599 --pname|-n) shift; pname="$1" ;;
2600 --pver|-v) shift; pver="$1" ;;
2601 --pbuild|-b) shift; pbuild="$1" ;;
2602 esac
2603 shift
2604 done
2605
2606 # sanity checks; abort if not given
2607 [ -z "${pcat}" ] && die "mage_uninstall() \$pcat not given."
2608 [ -z "${pname}" ] && die "mage_uninstall() \$pname not given."
2609 [ -z "${pver}" ] && die "mage_uninstall() \$pver not given."
2610 [ -z "${pbuild}" ] && die "mage_uninstall() \$pbuild not given."
2611
2612 # check needed global vars
2613 [ -z "${MAGEDIR}" ] && die "mage_uninstall() \$MAGEDIR not set."
2614 [ -z "${INSTALLDB}" ] && die "mage_uninstall() \$INSTALLDB not set."
2615 [ -z "${BUILDDIR}" ] && die "mage_uninstall() \$BUILDDIR not set."
2616
2617 xtitle "[ (${count_current}/${count_total}) Removing ${pcat}/${pname}-${pver}-${pbuild} ]"
2618 echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2619 echo -n "removing: "
2620 echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2621 echo -e "${COLRED}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2622
2623 magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
2624 source ${magefile}
2625
2626 ## preremove scripts
2627 if [ -n "$(typeset -f preremove)" ]
2628 then
2629 echo -e " ${COLBLUE}***${COLDEFAULT} running preremove ... "
2630 preremove
2631 unset preremove
2632 fi
2633
2634 # runs uninstall
2635 build_douninstall \
2636 --pcat "${pcat}" \
2637 --pname "${pname}" \
2638 --pver "${pver}" \
2639 --pbuild "${pbuild}"
2640
2641 ## postremove scripts
2642 if [ -n "$(typeset -f postremove)" ]
2643 then
2644 echo -e " ${COLBLUE}***${COLDEFAULT} running postremove ... "
2645 postremove
2646 unset postremove
2647 fi
2648
2649 # removes the database entry
2650 remove_database_entry \
2651 --pcat "${pcat}" \
2652 --pname "${pname}" \
2653 --pver "${pver}" \
2654 --pbuild "${pbuild}" \
2655 || die "error in mage_uninstall() running remove_database_entry()."
2656
2657 # rebuilds toplevel info node
2658 if [[ ${MAGE_INFO_REBUILD} = true ]]
2659 then
2660 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2661 echo -n "rebuilding top-level info node ... "
2662 ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2663 > ${MROOT}/usr/share/info/dir && \
2664 echo "done." || echo "failure."
2665 unset MAGE_INFO_REBUILD
2666 fi
2667
2668 # rebuilds the enviroment with the content of /etc/env.d
2669 if [[ ${MAGE_ENV_REBUILD} = true ]]
2670 then
2671 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2672 echo -n "rebuilding environment ... "
2673 ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2674 echo "done." || echo "failure."
2675 unset MAGE_ENV_REBUILD
2676 fi
2677
2678 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2679 echo -n "package "
2680 # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2681 # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2682 echo "successfully removed."
2683
2684 # unset these functions
2685 unset -f preinstall
2686 unset -f postinstall
2687 unset -f preremove
2688 unset -f postremove
2689 }
2690
2691 show_etc_update_mesg() {
2692 [ ${MAGE_PROTECT_COUNTER} -eq 0 ] && return 0
2693
2694 echo
2695 echo -ne "${COLRED}"
2696 echo "Important:"
2697 echo -ne ${COLDEFAULT}
2698 echo "${MAGE_PROTECT_COUNTER} protected file(s) were installed."
2699 echo
2700 echo "Please run 'etc-update' to update your configuration files."
2701 echo
2702 }
2703
2704 pkgsearch()
2705 {
2706 local string="$1"
2707 local result
2708 local pkg
2709 local pcat
2710 local pname
2711 local magefile
2712 local pver
2713 local pbuild
2714 local state
2715 local descriptiom
2716 local homepage
2717 local i
2718 local all_installed
2719 local ipver
2720 local ipbuild
2721 local latest_available
2722 local depsfull
2723 local sdepsfull
2724 local deps
2725 local sdeps
2726 local dep
2727 local sign
2728
2729 # only names no versions
2730 result="$(find ${MAGEDIR} -mindepth 2 -maxdepth 2 -type d -name '*'${string}'*'| sed '/profiles/d' | sed '/includes/d')"
2731 #result="$(find ${MAGEDIR} -type f -name '*'${string}'*'.mage | sort)"
2732
2733 # nothing found
2734 [[ -z ${result} ]] && die "No package found containing '${string}' in the name."
2735
2736 for pkg in ${result}
2737 do
2738 # dirty, but does the job
2739 pcat="$(magename2pcat ${pkg}/foo)"
2740 pname="$(magename2pname ${pkg}-foo-foo)"
2741
2742 # get highest version available
2743 magefile=$(get_highest_magefile ${pcat} ${pname})
2744
2745 if [[ ! -z ${magefile} ]]
2746 then
2747 # now get all needed infos to print a nice output
2748 pver="$(magename2pver ${magefile})"
2749 pbuild="$(magename2pbuild ${magefile})"
2750 state="$(get_value_from_magefile STATE ${magefile})"
2751 description="$(get_value_from_magefile DESCRIPTION ${magefile})"
2752 homepage="$(get_value_from_magefile HOMEPAGE ${magefile})"
2753
2754 # all installed
2755 for i in $(get_uninstall_candidates --pname ${pname} --pcat ${pcat})
2756 do
2757 ipver="$(magename2pver ${i})"
2758 ipbuild="$(magename2pbuild ${i})"
2759
2760 if [[ -z ${all_installed} ]]
2761 then
2762 all_installed="${ipver}-${ipbuild}"
2763 else
2764 all_installed="${all_installed} ${ipver}-${ipbuild}"
2765 fi
2766 done
2767 [[ -z ${all_installed} ]] && all_installed="none"
2768
2769 case ${state} in
2770 stable) state=${COLGREEN}"[s] ";;
2771 testing) state=${COLYELLOW}"[t] ";;
2772 unstable) state=${COLRED}"[u] ";;
2773 old) state=${COLGRAY}"[o] ";;
2774 esac
2775
2776 latest_available="${pver}-${pbuild}"
2777 else
2778 # package is masked
2779 state="${COLRED}[m] "
2780 latest_available="${COLRED}masked for this distribution.${COLDEFAULT}"
2781 fi
2782
2783 depsfull="$(get_value_from_magefile DEPEND ${magefile})"
2784 sdepsfull="$(get_value_from_magefile SDEPEND ${magefile})"
2785
2786 while read sign dep
2787 do
2788 case ${dep} in
2789 "") continue;;
2790 esac
2791
2792 deps="${deps} $(basename ${dep%-*})"
2793 done << EOF
2794 ${depsfull}
2795 EOF
2796
2797 while read sign dep
2798 do
2799 case ${dep} in
2800 "") continue;;
2801 esac
2802
2803 sdeps="${sdeps} $(basename ${dep%-*})"
2804 done << EOF
2805 ${sdepsfull}
2806 EOF
2807
2808 echo -e "${state}${pcat}/${pname}"${COLDEFAULT}
2809 echo -e " Latest available: ${latest_available}"
2810 echo " Installed versions: ${all_installed}"
2811 echo " Description: ${description}"
2812 echo " Homepage: ${homepage}"
2813 echo " Depends: ${deps}"
2814 echo " SDepends: ${sdeps}"
2815 echo
2816
2817 unset pcat
2818 unset pname
2819 unset magefile
2820 unset pver
2821 unset pbuild
2822 unset state
2823 unset descriptiom
2824 unset homepage
2825 unset all_installed
2826 unset ipver
2827 unset ipbuild
2828 unset depsfull
2829 unset sdepsfull
2830 unset deps
2831 unset sdeps
2832 unset dep
2833 unset sign
2834 done
2835 }
2836
2837 export_inherits()
2838 {
2839 local include="$1"
2840 shift
2841
2842 while [ "$1" ]
2843 do
2844 local functions="$1"
2845
2846 # sanity checks
2847 [ -z "${include}" ] && die "export_inherits(): \$include not given."
2848 [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
2849
2850 eval "${functions}() { ${include}_${functions} ; }"
2851
2852 # debug
2853 [[ ${MAGEDEBUG} = on ]] && typeset -f "${functions}"
2854
2855 shift
2856 done
2857 }
2858
2859 mlibdir()
2860 {
2861 local libdir=lib
2862 [[ ${ARCH} = x86_64 ]] && libdir=lib64
2863
2864 echo "${libdir}"
2865 }
2866
2867 ## blacklisted ${magefile}
2868 blacklisted()
2869 {
2870 [[ -z ${MAGE_DISTRIBUTION} ]] && local MAGE_DISTRIBUTION=stable
2871
2872 # compat
2873 [[ ${USE_UNSTABLE} = true ]] && local MAGE_DISTRIBUTION=unstable
2874 [[ ${USE_TESTING} = true ]] && local MAGE_DISTRIBUTION=testing
2875
2876 # support both types for the moment
2877 if [[ -f /etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION} ]]
2878 then
2879 local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION}"
2880 else
2881 local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}"
2882 fi
2883
2884 # return 0 if the list not exist; nothin is masked
2885 [[ ! -f ${EXCLUDED} ]] && return 0
2886
2887 local MAGEFILE="$1"
2888
2889 local PCAT="$(magename2pcat ${MAGEFILE})"
2890 local PNAME="$(magename2pname ${MAGEFILE})"
2891 local PVER="$(magename2pver ${MAGEFILE})"
2892 local PBUILD="$(magename2pbuild ${MAGEFILE})"
2893
2894 local EXPCAT EXPNAME EXPVER EXPBUILD
2895 while read EXPCAT EXPNAME EXPVER EXPBUILD
2896 do
2897 # ignore spaces and comments
2898 case "${EXPCAT}" in
2899 \#*|"") continue ;;
2900 esac
2901
2902 # exclude full pver
2903 if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
2904 [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
2905 [[ -n ${PVER} ]] && [[ -n ${PBUILD} ]] &&
2906 [[ -n ${EXPVER} ]] && [[ -n ${EXPBUILD} ]]
2907 then
2908 [[ ${EXPCAT}/${EXPNAME}-${EXPVER}-${EXPBUILD} = ${PCAT}/${PNAME}-${PVER}-${PBUILD} ]] && return 1
2909 fi
2910
2911 # exclude pcat/pname only
2912 if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
2913 [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
2914 [[ -z ${EXPVER} ]] && [[ -z ${EXPBUILD} ]]
2915 then
2916 [[ ${EXPCAT}/${EXPNAME} = ${PCAT}/${PNAME} ]] && return 1
2917 fi
2918 done << EOF
2919 $( cat ${EXCLUDED}; echo)
2920 EOF
2921
2922 return 0
2923 }
2924