Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 942 - (show annotations) (download) (as text)
Fri Nov 20 21:53:33 2009 UTC (14 years, 5 months ago) by niro
File MIME type: application/x-sh
File size: 67156 byte(s)
-support CONFIG_PROTECT target CONFIG_PROTECT_IGNORE to keep user/system modified config files, like passwd
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 preinstall
2164 local postinstall
2165 local preremove
2166 local postremove
2167
2168 # sanity checks
2169 [ -f ${magefile} ] && source ${magefile} || \
2170 die "get_value_from_magefile: ${magefile} not found."
2171 [ -z "${var}" ] && die "get_value_from_magefile: \$var not given."
2172
2173 source ${magefile}
2174 eval value=\$$(echo ${var})
2175 echo "${value}"
2176
2177 # unset these functions
2178 unset -f preinstall
2179 unset -f postinstall
2180 unset -f preremove
2181 unset -f postremove
2182 }
2183
2184 mage_install()
2185 {
2186 # local all possible vars of a mage file
2187 # to prevent bad issues
2188 local PKGNAME
2189 local STATE
2190 local DESCRIPTION
2191 local HOMEPAGE
2192 local DEPEND
2193 local SDEPEND
2194 local PROVIDE
2195 local PKGTYPE
2196 local preinstall
2197 local postinstall
2198 local preremove
2199 local postremove
2200
2201 local pcat
2202 local pname
2203 local pver
2204 local pbuild
2205 local count_total
2206 local count_current
2207 local magefile
2208 local src_install
2209 local i
2210
2211 # very basic getops
2212 for i in $*
2213 do
2214 case $1 in
2215 --pcat|-c) shift; pcat="$1" ;;
2216 --pname|-n) shift; pname="$1" ;;
2217 --pver|-v) shift; pver="$1" ;;
2218 --pbuild|-b) shift; pbuild="$1" ;;
2219 --count-total) shift; count_total="$1" ;;
2220 --count-current) shift; count_current="$1" ;;
2221 --src-install|-s) shift; src_install=true ;;
2222 esac
2223 shift
2224 done
2225
2226 # sanity checks; abort if not given
2227 [ -z "${pcat}" ] && die "mage_install() \$pcat not given."
2228 [ -z "${pname}" ] && die "mage_install() \$pname not given."
2229 [ -z "${pver}" ] && die "mage_install() \$pver not given."
2230 [ -z "${pbuild}" ] && die "mage_install() \$pbuild not given."
2231
2232 # check needed global vars
2233 [ -z "${MAGEDIR}" ] && die "mage_install() \$MAGEDIR not set."
2234 [ -z "${INSTALLDB}" ] && die "mage_install() \$INSTALLDB not set."
2235 [ -z "${BUILDDIR}" ] && die "mage_install() \$BUILDDIR not set."
2236
2237 xtitle "[ (${count_current}/${count_total}) Installing ${pcat}/${pname}-${pver}-${pbuild} ]"
2238 echo -ne "${COLBLUE} >>> ${COLDEFAULT}"
2239 echo -n "installing (${count_current}/${count_total}): "
2240 echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2241 echo -e "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2242
2243 magefile="${MAGEDIR}/${pcat}/${pname}/${pname}-${pver}-${pbuild}.mage"
2244 source ${magefile}
2245
2246 # abort on sources if no srcinstall
2247 if [[ ${PKGTYPE} = sources ]] && [[ ${src_install} != true ]]
2248 then
2249 echo
2250 echo -e "This Package is a Source Package."
2251 echo
2252 echo -e "Only 'srcinstall' works with this type of packages"
2253 echo -en "If you have done a srcinstall before, "
2254 echo -e "you will find the files in /usr/src."
2255 echo
2256 exit 1
2257 fi
2258
2259 ## preinstall scripts
2260 if [ -n "$(typeset -f preinstall)" ]
2261 then
2262 echo -e " ${COLBLUE}***${COLDEFAULT} running preinstall ... "
2263 preinstall
2264 unset preinstall
2265 fi
2266
2267 if [[ ${src_install} = true ]]
2268 then
2269 local smage2file
2270 # check needed global vars
2271 [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not set."
2272 [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not set."
2273 [ -z "${BINDIR}" ] && die "\$BINDIR not set."
2274
2275 # build the package first
2276 if [[ ${MAGEDEBUG} = on ]]
2277 then
2278 echo M:${pname}
2279 echo V:${pver}
2280 echo B:${pbuild}
2281 fi
2282
2283 if [[ -z ${MAGE_TARGETS} ]]
2284 then
2285 # basic svn compat
2286 if [[ -d ${SMAGESCRIPTSDIR}/trunk ]]
2287 then
2288 for i in ${SMAGESCRIPTSDIR}/trunk/*/${pname}/${pname}-${pver}-${pbuild}.smage2
2289 do
2290 smage2file="${i}"
2291 done
2292 else
2293 smage2file=${SMAGESCRIPTSDIR}/${pname}/${pname}-${pver}-${pbuild}.smage2
2294 fi
2295 else
2296 # basic svn compat
2297 if [[ -d ${SMAGESCRIPTSDIR}/trunk ]]
2298 then
2299 for i in ${SMAGESCRIPTSDIR}/trunk/*/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2300 do
2301 smage2file="${i}"
2302 done
2303 else
2304 smage2file=${SMAGESCRIPTSDIR}/${pname/${MAGE_TARGETS}/}/${pname/${MAGE_TARGETS}/}-${pver}-${pbuild}.smage2
2305 fi
2306 fi
2307 if [ -f "${smage2file}" ]
2308 then
2309 echo -e " ${COLBLUE}***${COLDEFAULT} building package from source ... "
2310 smage2 ${smage2file} || die "compile failed"
2311 else
2312 echo
2313 echo "$(basename ${SMAGEFILE}) not found."
2314 echo "update your smage-tree and try it again."
2315 echo
2316 die
2317 fi
2318 fi
2319
2320 if [[ ${PKGTYPE} != virtual ]] && \
2321 [[ ${PKGTYPE} != sources ]]
2322 then
2323 echo -e " ${COLBLUE}***${COLDEFAULT} merging files into system ... "
2324 build_doinstall ${PKGNAME}
2325 fi
2326
2327 ## postinstall scripts
2328 if [ -n "$(typeset -f postinstall)" ]
2329 then
2330 echo -e " ${COLBLUE}***${COLDEFAULT} running postinstall ... "
2331 postinstall
2332 unset postinstall
2333 fi
2334
2335 # install a database entry
2336 install_database_entry \
2337 --pcat "${pcat}" \
2338 --pname "${pname}" \
2339 --pver "${pver}" \
2340 --pbuild "${pbuild}" \
2341 --pkgname "${PKGNAME}" \
2342 --pkgtype "${PKGTYPE}" \
2343 || die "error in mage_install() running install_database_entry()."
2344
2345 # remove the package dir now
2346 if [ -d ${BUILDDIR}/${PKGNAME} ]
2347 then
2348 rm -rf ${BUILDDIR}/${PKGNAME}
2349 fi
2350
2351 # rebuilds toplevel info node
2352 if [[ ${MAGE_INFO_REBUILD} = true ]]
2353 then
2354 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2355 echo -n "rebuilding top-level info node ... "
2356 ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2357 > ${MROOT}/usr/share/info/dir && \
2358 echo "done." || echo "failure."
2359 unset MAGE_INFO_REBUILD
2360 fi
2361
2362 # rebuilds the enviroment with the content of /etc/env.d
2363 if [[ ${MAGE_ENV_REBUILD} = true ]]
2364 then
2365 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2366 echo -n "rebuilding environment ... "
2367 ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2368 echo "done." || echo "failure."
2369 unset MAGE_ENV_REBUILD
2370 fi
2371
2372 xtitleclean
2373
2374 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2375 echo -n "package "
2376 # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2377 # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2378 echo "successfully installed."
2379
2380 # unset these functions
2381 unset -f preinstall
2382 unset -f postinstall
2383 unset -f preremove
2384 unset -f postremove
2385 }
2386
2387 md5sum_packages()
2388 {
2389 local list="$@"
2390 local magefile
2391 local pcat
2392 local pname
2393 local pkgname
2394 local pkgfile
2395 local pkgtype
2396 local count_current
2397 local count_total
2398
2399 # get count of total packages
2400 declare -i count_current=0
2401 declare -i count_total=0
2402
2403 for i in ${list}; do (( count_total++ )); done
2404
2405 for magefile in ${list}
2406 do
2407 pcat=$(magename2pcat ${magefile})
2408 pname=$(magename2pname ${magefile})
2409 pkgname="$(get_value_from_magefile PKGNAME ${magefile})"
2410 md5file="${MAGEDIR}/${pcat}/${pname}/md5/${pkgname}.md5"
2411 pkgfile="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}"
2412 pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})"
2413
2414 (( count_current++ ))
2415 xtitle "[ (${count_current}/${count_total}) MD5SUM: ${pkgfile} ]"
2416
2417 # abort on virtual pkg
2418 if [[ ${pkgtype} = virtual ]]
2419 then
2420 echo -ne " ${COLBLUE}---${COLDEFAULT}"
2421 echo " !md5sum virtual (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2422 continue
2423 fi
2424
2425 # abort on sources pkg
2426 if [[ ${pkgtype} = sources ]]
2427 then
2428 echo -ne " ${COLBLUE}---${COLDEFAULT}"
2429 echo " !md5sum sources (${count_current}/${count_total}): ${pkgfile/.${PKGSUFFIX}/} ... "
2430 continue
2431 fi
2432
2433 if [ -f "${md5file}" ]
2434 then
2435 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2436 echo -ne "checking md5sum (${count_current}/${count_total}): "
2437 ( cd ${PKGDIR}; md5sum --check ${md5file}) || die "md5 for ${pkgfile} failed"
2438 else
2439 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2440 echo -e "!! no md5sum file found for ${pkgfile} :("
2441 fi
2442 done
2443
2444 # add a crlf for a better view
2445 if [ ${count_total} -gt 1 ]; then echo; fi
2446 }
2447
2448 ## uninstall_packages ulist
2449 uninstall_packages()
2450 {
2451 local list="$@"
2452 local pcat
2453 local pname
2454 local pver
2455 local pbuild
2456 local can_pcat
2457 local can_pname
2458 local can_ver_list
2459
2460 if [[ -n ${MROOT} ]]
2461 then
2462 echo -ne ${COLRED}
2463 echo "!! uninstalling from MROOT=${MROOT}"
2464 echo -ne ${COLDEFAULT}
2465 echo
2466 fi
2467
2468 # generate a candidates list
2469 for pkg in ${list}
2470 do
2471 pcat=$(dep2pcat ${pkg})
2472 pname=$(magename2pname ${pkg})
2473 pver=$(magename2pver ${pkg})
2474 pbuild=$(magename2pbuild ${pkg})
2475 can_pcat="${pcat}"
2476 can_pname="${pname}"
2477
2478 if [ -z "${can_ver_list}" ]
2479 then
2480 can_ver_list=" ${pver}-${pbuild}"
2481 else
2482 can_ver_list="${can_ver_list}, ${pver}-${pbuild}"
2483 fi
2484 done
2485
2486 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2487 echo "following candidate(s) will be removed:"
2488 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2489 echo -ne "${COLBOLD}${can_pcat}/${can_pname}:${COLDEFAULT}"
2490 echo -e "${COLRED} ${can_ver_list} ${COLDEFAULT}"
2491 echo
2492 if [ ${MAGE_UNINSTALL_TIMEOUT} -gt 0 ]
2493 then
2494 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2495 echo "( Press [CTRL+C] to abort )"
2496 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2497 echo -n "Waiting ${MAGE_UNINSTALL_TIMEOUT} seconds ..."
2498 for ((i=MAGE_UNINSTALL_TIMEOUT; i >= 0; i--))
2499 do
2500 echo -ne "${COLRED} ${i}${COLDEFAULT}"
2501 sleep 1
2502 done
2503 echo
2504 echo
2505 fi
2506
2507 for pkg in ${list}
2508 do
2509 pcat=$(dep2pcat ${pkg})
2510 pname=$(magename2pname ${pkg})
2511 pver=$(magename2pver ${pkg})
2512 pbuild=$(magename2pbuild ${pkg})
2513
2514 mage_uninstall \
2515 --pcat ${pcat} \
2516 --pname ${pname} \
2517 --pver ${pver} \
2518 --pbuild ${pbuild} \
2519 --count-total ${total_pkgs} \
2520 --count-current ${current_pkg} \
2521 ${src_install}
2522
2523 # crlf for better view in VERBOSE mode
2524 #if [[ ${VERBOSE} = on ]]; then echo; fi
2525 echo
2526 done
2527 }
2528
2529 mage_uninstall()
2530 {
2531 # local all possible vars of a mage file
2532 # to prevent bad issues
2533 local PKGNAME
2534 local STATE
2535 local DESCRIPTION
2536 local HOMEPAGE
2537 local DEPEND
2538 local SDEPEND
2539 local PROVIDE
2540 local PKGTYPE
2541 local preinstall
2542 local postinstall
2543 local preremove
2544 local postremove
2545
2546 local pcat
2547 local pname
2548 local pver
2549 local pbuild
2550 local magefile
2551 local i
2552
2553 # very basic getops
2554 for i in $*
2555 do
2556 case $1 in
2557 --pcat|-c) shift; pcat="$1" ;;
2558 --pname|-n) shift; pname="$1" ;;
2559 --pver|-v) shift; pver="$1" ;;
2560 --pbuild|-b) shift; pbuild="$1" ;;
2561 esac
2562 shift
2563 done
2564
2565 # sanity checks; abort if not given
2566 [ -z "${pcat}" ] && die "mage_uninstall() \$pcat not given."
2567 [ -z "${pname}" ] && die "mage_uninstall() \$pname not given."
2568 [ -z "${pver}" ] && die "mage_uninstall() \$pver not given."
2569 [ -z "${pbuild}" ] && die "mage_uninstall() \$pbuild not given."
2570
2571 # check needed global vars
2572 [ -z "${MAGEDIR}" ] && die "mage_uninstall() \$MAGEDIR not set."
2573 [ -z "${INSTALLDB}" ] && die "mage_uninstall() \$INSTALLDB not set."
2574 [ -z "${BUILDDIR}" ] && die "mage_uninstall() \$BUILDDIR not set."
2575
2576 xtitle "[ (${count_current}/${count_total}) Removing ${pcat}/${pname}-${pver}-${pbuild} ]"
2577 echo -ne "${COLBLUE} <<< ${COLDEFAULT}"
2578 echo -n "removing: "
2579 echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2580 echo -e "${COLRED}${pname}-${pver}-${pbuild}${COLDEFAULT}"
2581
2582 magefile="${MROOT}${INSTALLDB}/${pcat}/${pname}-${pver}-${pbuild}/${pname}-${pver}-${pbuild}.mage"
2583 source ${magefile}
2584
2585 ## preremove scripts
2586 if [ -n "$(typeset -f preremove)" ]
2587 then
2588 echo -e " ${COLBLUE}***${COLDEFAULT} running preremove ... "
2589 preremove
2590 unset preremove
2591 fi
2592
2593 # runs uninstall
2594 build_douninstall \
2595 --pcat "${pcat}" \
2596 --pname "${pname}" \
2597 --pver "${pver}" \
2598 --pbuild "${pbuild}"
2599
2600 ## postremove scripts
2601 if [ -n "$(typeset -f postremove)" ]
2602 then
2603 echo -e " ${COLBLUE}***${COLDEFAULT} running postremove ... "
2604 postremove
2605 unset postremove
2606 fi
2607
2608 # removes the database entry
2609 remove_database_entry \
2610 --pcat "${pcat}" \
2611 --pname "${pname}" \
2612 --pver "${pver}" \
2613 --pbuild "${pbuild}" \
2614 || die "error in mage_uninstall() running remove_database_entry()."
2615
2616 # rebuilds toplevel info node
2617 if [[ ${MAGE_INFO_REBUILD} = true ]]
2618 then
2619 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2620 echo -n "rebuilding top-level info node ... "
2621 ${MLIBDIR}/mkinfodir ${MROOT}/usr/share/info \
2622 > ${MROOT}/usr/share/info/dir && \
2623 echo "done." || echo "failure."
2624 unset MAGE_INFO_REBUILD
2625 fi
2626
2627 # rebuilds the enviroment with the content of /etc/env.d
2628 if [[ ${MAGE_ENV_REBUILD} = true ]]
2629 then
2630 echo -ne "${COLBLUE} *** ${COLDEFAULT}"
2631 echo -n "rebuilding environment ... "
2632 ${MLIBDIR}/env-rebuild.sh > /dev/null && \
2633 echo "done." || echo "failure."
2634 unset MAGE_ENV_REBUILD
2635 fi
2636
2637 echo -ne "${COLBLUE} --- ${COLDEFAULT}"
2638 echo -n "package "
2639 # echo -ne "${COLBLUE}${pcat}/${COLDEFAULT}"
2640 # echo -ne "${COLGREEN}${pname}-${pver}-${pbuild}${COLDEFAULT} "
2641 echo "successfully removed."
2642
2643 # unset these functions
2644 unset -f preinstall
2645 unset -f postinstall
2646 unset -f preremove
2647 unset -f postremove
2648 }
2649
2650 show_etc_update_mesg() {
2651 [ ${MAGE_PROTECT_COUNTER} -eq 0 ] && return 0
2652
2653 echo
2654 echo -ne "${COLRED}"
2655 echo "Important:"
2656 echo -ne ${COLDEFAULT}
2657 echo "${MAGE_PROTECT_COUNTER} protected file(s) were installed."
2658 echo
2659 echo "Please run 'etc-update' to update your configuration files."
2660 echo
2661 }
2662
2663 pkgsearch()
2664 {
2665 local string="$1"
2666 local result
2667 local pkg
2668 local pcat
2669 local pname
2670 local magefile
2671 local pver
2672 local pbuild
2673 local state
2674 local descriptiom
2675 local homepage
2676 local i
2677 local all_installed
2678 local ipver
2679 local ipbuild
2680 local latest_available
2681 local depsfull
2682 local sdepsfull
2683 local deps
2684 local sdeps
2685 local dep
2686 local sign
2687
2688 # only names no versions
2689 result="$(find ${MAGEDIR} -mindepth 2 -maxdepth 2 -type d -name '*'${string}'*'| sed '/profiles/d' | sed '/includes/d')"
2690 #result="$(find ${MAGEDIR} -type f -name '*'${string}'*'.mage | sort)"
2691
2692 # nothing found
2693 [[ -z ${result} ]] && die "No package found containing '${string}' in the name."
2694
2695 for pkg in ${result}
2696 do
2697 # dirty, but does the job
2698 pcat="$(magename2pcat ${pkg}/foo)"
2699 pname="$(magename2pname ${pkg}-foo-foo)"
2700
2701 # get highest version available
2702 magefile=$(get_highest_magefile ${pcat} ${pname})
2703
2704 if [[ ! -z ${magefile} ]]
2705 then
2706 # now get all needed infos to print a nice output
2707 pver="$(magename2pver ${magefile})"
2708 pbuild="$(magename2pbuild ${magefile})"
2709 state="$(get_value_from_magefile STATE ${magefile})"
2710 description="$(get_value_from_magefile DESCRIPTION ${magefile})"
2711 homepage="$(get_value_from_magefile HOMEPAGE ${magefile})"
2712
2713 # all installed
2714 for i in $(get_uninstall_candidates --pname ${pname} --pcat ${pcat})
2715 do
2716 ipver="$(magename2pver ${i})"
2717 ipbuild="$(magename2pbuild ${i})"
2718
2719 if [[ -z ${all_installed} ]]
2720 then
2721 all_installed="${ipver}-${ipbuild}"
2722 else
2723 all_installed="${all_installed} ${ipver}-${ipbuild}"
2724 fi
2725 done
2726 [[ -z ${all_installed} ]] && all_installed="none"
2727
2728 case ${state} in
2729 stable) state=${COLGREEN}"[s] ";;
2730 testing) state=${COLYELLOW}"[t] ";;
2731 unstable) state=${COLRED}"[u] ";;
2732 old) state=${COLGRAY}"[o] ";;
2733 esac
2734
2735 latest_available="${pver}-${pbuild}"
2736 else
2737 # package is masked
2738 state="${COLRED}[m] "
2739 latest_available="${COLRED}masked for this distribution.${COLDEFAULT}"
2740 fi
2741
2742 depsfull="$(get_value_from_magefile DEPEND ${magefile})"
2743 sdepsfull="$(get_value_from_magefile SDEPEND ${magefile})"
2744
2745 while read sign dep
2746 do
2747 case ${dep} in
2748 "") continue;;
2749 esac
2750
2751 deps="${deps} $(basename ${dep%-*})"
2752 done << EOF
2753 ${depsfull}
2754 EOF
2755
2756 while read sign dep
2757 do
2758 case ${dep} in
2759 "") continue;;
2760 esac
2761
2762 sdeps="${sdeps} $(basename ${dep%-*})"
2763 done << EOF
2764 ${sdepsfull}
2765 EOF
2766
2767 echo -e "${state}${pcat}/${pname}"${COLDEFAULT}
2768 echo -e " Latest available: ${latest_available}"
2769 echo " Installed versions: ${all_installed}"
2770 echo " Description: ${description}"
2771 echo " Homepage: ${homepage}"
2772 echo " Depends: ${deps}"
2773 echo " SDepends: ${sdeps}"
2774 echo
2775
2776 unset pcat
2777 unset pname
2778 unset magefile
2779 unset pver
2780 unset pbuild
2781 unset state
2782 unset descriptiom
2783 unset homepage
2784 unset all_installed
2785 unset ipver
2786 unset ipbuild
2787 unset depsfull
2788 unset sdepsfull
2789 unset deps
2790 unset sdeps
2791 unset dep
2792 unset sign
2793 done
2794 }
2795
2796 export_inherits()
2797 {
2798 local include="$1"
2799 shift
2800
2801 while [ "$1" ]
2802 do
2803 local functions="$1"
2804
2805 # sanity checks
2806 [ -z "${include}" ] && die "export_inherits(): \$include not given."
2807 [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
2808
2809 eval "${functions}() { ${include}_${functions} ; }"
2810
2811 # debug
2812 [[ ${MAGEDEBUG} = on ]] && typeset -f "${functions}"
2813
2814 shift
2815 done
2816 }
2817
2818 mlibdir()
2819 {
2820 local libdir=lib
2821 [[ ${ARCH} = x86_64 ]] && libdir=lib64
2822
2823 echo "${libdir}"
2824 }
2825
2826 ## blacklisted ${magefile}
2827 blacklisted()
2828 {
2829 [[ -z ${MAGE_DISTRIBUTION} ]] && local MAGE_DISTRIBUTION=stable
2830
2831 # compat
2832 [[ ${USE_UNSTABLE} = true ]] && local MAGE_DISTRIBUTION=unstable
2833 [[ ${USE_TESTING} = true ]] && local MAGE_DISTRIBUTION=testing
2834
2835 # support both types for the moment
2836 if [[ -f /etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION} ]]
2837 then
2838 local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}-${MAGE_DISTRIBUTION}"
2839 else
2840 local EXCLUDED="/etc/mage-profile/package.blacklist-${ARCH}"
2841 fi
2842
2843 # return 0 if the list not exist; nothin is masked
2844 [[ ! -f ${EXCLUDED} ]] && return 0
2845
2846 local MAGEFILE="$1"
2847
2848 local PCAT="$(magename2pcat ${MAGEFILE})"
2849 local PNAME="$(magename2pname ${MAGEFILE})"
2850 local PVER="$(magename2pver ${MAGEFILE})"
2851 local PBUILD="$(magename2pbuild ${MAGEFILE})"
2852
2853 local EXPCAT EXPNAME EXPVER EXPBUILD
2854 while read EXPCAT EXPNAME EXPVER EXPBUILD
2855 do
2856 # ignore spaces and comments
2857 case "${EXPCAT}" in
2858 \#*|"") continue ;;
2859 esac
2860
2861 # exclude full pver
2862 if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
2863 [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
2864 [[ -n ${PVER} ]] && [[ -n ${PBUILD} ]] &&
2865 [[ -n ${EXPVER} ]] && [[ -n ${EXPBUILD} ]]
2866 then
2867 [[ ${EXPCAT}/${EXPNAME}-${EXPVER}-${EXPBUILD} = ${PCAT}/${PNAME}-${PVER}-${PBUILD} ]] && return 1
2868 fi
2869
2870 # exclude pcat/pname only
2871 if [[ -n ${PCAT} ]] && [[ -n ${PNAME} ]] &&
2872 [[ -n ${EXPCAT} ]] && [[ -n ${EXPNAME} ]] &&
2873 [[ -z ${EXPVER} ]] && [[ -z ${EXPBUILD} ]]
2874 then
2875 [[ ${EXPCAT}/${EXPNAME} = ${PCAT}/${PNAME} ]] && return 1
2876 fi
2877 done << EOF
2878 $( cat ${EXCLUDED}; echo)
2879 EOF
2880
2881 return 0
2882 }
2883