Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 676 - (hide annotations) (download) (as text)
Tue Feb 26 19:49:20 2008 UTC (16 years, 2 months ago) by niro
File MIME type: application/x-sh
File size: 64284 byte(s)
-fixed processing of directories in get_highest_magefile(), suppressed some errors

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