Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (hide annotations) (download) (as text)
Fri Oct 21 16:01:40 2005 UTC (18 years, 6 months ago) by niro
File MIME type: application/x-sh
File size: 60105 byte(s)
added config prot support for uninstalling

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