xorg-server: enable some more extensions.
[pkgfs.git] / pkgfs.sh
bloba212895b11a4328d3065989c4462e157569a71d4
1 #!/bin/sh
3 # pkgfs - Builds source distribution files and stows/unstows them into
4 # a master directory, thanks to Xstow.
6 #-
7 # Copyright (c) 2008 Juan Romero Pardines.
8 # All rights reserved.
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # TODO
32 # - Implement a routine that checks if installed version is sufficient
33 # to satisfy the required dependency... right now it's very prone
34 # to errors and slow.
35 # - Multiple distfiles in a package.
36 # - Multiple URLs to download source distribution files.
38 # Default path to configuration file, can be overriden
39 # via the environment or command line.
41 : ${PKGFS_CONFIG_FILE:=/usr/local/etc/pkgfs.conf}
43 : ${progname:=$(basename $0)}
44 : ${topdir:=$(/bin/pwd -P 2>/dev/null)}
45 : ${fetch_cmd:=/usr/bin/ftp -a}
46 : ${cksum_cmd:=/usr/bin/cksum -a rmd160}
47 : ${awk_cmd:=/usr/bin/awk}
48 : ${mkdir_cmd:=/bin/mkdir -p}
49 : ${tar_cmd:=/usr/bin/tar}
50 : ${rm_cmd:=/bin/rm}
51 : ${mv_cmd:=/bin/mv}
52 : ${cp_cmd:=/bin/cp}
53 : ${sed_cmd=/usr/bin/sed}
54 : ${grep_cmd=/usr/bin/grep}
55 : ${gunzip_cmd:=/usr/bin/gunzip}
56 : ${bunzip2_cmd:=/usr/bin/bunzip2}
57 : ${patch_cmd:=/usr/bin/patch}
58 : ${find_cmd:=/usr/bin/find}
59 : ${file_cmd:=/usr/bin/file}
60 : ${ln_cmd:=/bin/ln}
61 : ${chmod_cmd:=/bin/chmod}
62 : ${db_cmd:=/usr/bin/db -q}
63 : ${chmod_cmd:=/bin/chmod}
65 : ${xstow_args:=-ap}
66 : ${xstow_ignore_files:=perllocal.pod} # XXX For now ignore them.
68 set_defvars()
70 # Directories
71 : ${PKGFS_TEMPLATESDIR:=$PKGFS_DISTRIBUTIONDIR/templates}
72 : ${PKGFS_DEPSDIR:=$PKGFS_DISTRIBUTIONDIR/dependencies}
73 : ${PKGFS_BUILD_DEPS_DB:=$PKGFS_DEPSDIR/build-depends.db}
74 : ${PKGFS_TMPLHELPDIR:=$PKGFS_DISTRIBUTIONDIR/helper-templates}
75 : ${PKGFS_REGPKG_DB:=$PKGFS_DESTDIR/.pkgfs-registered-pkgs.db}
77 local DDIRS="PKGFS_DEPSDIR PKGFS_TEMPLATESDIR PKGFS_TMPLHELPDIR"
78 for i in ${DDIRS}; do
79 eval val="\$$i"
80 if [ ! -d "$val" ]; then
81 echo "**** ERROR: cannot find $i, aborting ***"
82 exit 1
84 done
87 usage()
89 cat << _EOF
90 $progname: [-bCefi] [-c <config_file>] <target> <tmplname>
92 Targets
93 info Show information about <tmplname>.
94 install Build and install package from <tmplname>.
95 list Lists and prints short description about installed packages.
96 remove Remove package completely (unstow and remove from destdir)
97 stow Create symlinks from <tmplname> in master directory.
98 unstow Remove symlinks from <tmplname> in master directory.
100 Options
101 -b Only build the source distribution file(s), without
102 extracting or fetching before.
103 -C Do not remove build directory after successful build.
104 -c Path to global configuration file.
105 If not specified /usr/local/etc/pkgfs.conf is used.
106 -e Only extract the source distribution file(s).
107 -f Only fetch the source distribution file(s).
108 -i Only build and install the binary distribution file(s)
109 into the destdir directory, without stowning.
110 _EOF
111 exit 1
114 check_path()
116 eval local orig="$1"
118 case "$orig" in
122 orig="${orig%/}"
125 orig="$topdir/${orig%/}"
127 esac
129 path_fixed="$orig"
132 run_file()
134 local file="$1"
136 check_path "$file"
137 . $path_fixed
141 # This function merges two GNU info dirs into one and puts the result
142 # into PKGFS_MASTERDIR/share/info/dir.
144 merge_infodir_tmpl()
146 local pkgname="$1"
147 local merge_info_cmd="$PKGFS_MASTERDIR/bin/merge-info"
149 [ -z "$pkgname" ] && return 1
150 [ ! -r "$PKGFS_MASTERDIR/share/info/dir" ] && return 1
151 [ ! -r "$PKGFS_DESTDIR/$pkgname/share/info/dir" ] && return 1
153 $merge_info_cmd -d $PKGFS_MASTERDIR/share/info/dir \
154 $PKGFS_DESTDIR/$pkgname/share/info/dir -o \
155 $PKGFS_MASTERDIR/share/info/dir.new
156 if [ "$?" -ne 0 ]; then
157 echo -n "*** WARNING: there was an error merging info dir from"
158 echo " $pkgname, aborting ***"
159 return 1
162 $mv_cmd -f $PKGFS_MASTERDIR/share/info/dir.new \
163 $PKGFS_MASTERDIR/share/info/dir
167 # Shows info about a template.
169 info_tmpl()
171 local tmpl="$1"
172 if [ -z "$tmpl" -o ! -f "$PKGFS_TEMPLATESDIR/$tmpl.tmpl" ]; then
173 echo -n "*** ERROR: invalid template file \`$tmpl' ***"
174 echo ", aborting ***"
175 exit 1
178 run_file $PKGFS_TEMPLATESDIR/$tmpl.tmpl
180 echo "pkgname: $pkgname"
181 echo "version: $version"
182 for i in "${distfiles}"; do
183 [ -n "$i" ] && echo "distfile: $i"
184 done
185 echo "URL: $url"
186 echo "maintainer: $maintainer"
187 [ -n $checksum ] && echo "checksum: $checksum"
188 echo "build_style: $build_style"
189 echo "short_desc: $short_desc"
190 echo "$long_desc"
191 echo
192 check_build_depends_tmpl $pkgname-$version
193 if [ "$?" -eq 0 ]; then
194 local list="$($db_cmd -V btree $PKGFS_BUILD_DEPS_DB $pkgname)"
195 echo "This package requires the following dependencies to be built:"
196 for i in ${list}; do
197 echo " $i"
198 done
203 # Applies to the build directory the patches specified by a template.
205 apply_tmpl_patches()
207 local patch=
210 # If package needs some patches applied before building,
211 # apply them now.
213 if [ -n "$patch_files" ]; then
214 for i in ${patch_files}; do
215 patch="$PKGFS_TEMPLATESDIR/$i"
216 if [ ! -f "$patch" ]; then
217 echo "*** WARNING: unexistent patch '$i' ***"
218 continue
221 $cp_cmd -f $patch $wrksrc
223 # Try to guess if its a compressed patch.
224 if $(echo $patch|$grep_cmd -q .gz); then
225 $gunzip_cmd $wrksrc/$i
226 patch=${i%%.gz}
227 elif $(echo $patch|$grep_cmd -q .bz2); then
228 $bunzip2_cmd $wrksrc/$i
229 patch=${i%%.bz2}
230 elif $(echo $patch|$grep_cmd -q .diff); then
231 patch=$i
232 else
233 echo "*** WARNING: unknown patch type '$i' ***"
234 continue
237 cd $wrksrc && $patch_cmd < $patch 2>/dev/null
238 if [ "$?" -eq 0 ]; then
239 echo "=> Patch applied: \`$i'."
240 else
241 echo -n "*** ERROR: couldn't apply patch '$i'"
242 echo ", aborting ***"
243 exit 1
245 done
250 # Checks that all required variables specified in the configuration
251 # file are properly working.
253 check_config_vars()
255 local cffound=
257 if [ -z "$config_file_specified" ]; then
258 config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
259 for f in $config_file_paths; do
260 [ -f $f ] && PKGFS_CONFIG_FILE=$f && \
261 cffound=yes && break
262 done
263 if [ -z "$cffound" ]; then
264 echo -n "*** ERROR: config file not specified "
265 echo "and not in default location or current dir ***"
266 exit 1
270 run_file ${PKGFS_CONFIG_FILE}
271 PKGFS_CONFIG_FILE=$path_fixed
273 if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
274 echo -n "*** ERROR: cannot find configuration file: "
275 echo "'$PKGFS_CONFIG_FILE' ***"
276 exit 1
279 local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
280 PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"
282 for f in ${PKGFS_VARS}; do
283 eval val="\$$f"
284 if [ -z "$val" ]; then
285 echo -n "**** ERROR: '$f' not set in configuration "
286 echo "file, aborting ***"
287 exit 1
290 if [ ! -d "$val" ]; then
291 $mkdir_cmd "$val"
292 if [ "$?" -ne 0 ]; then
293 echo -n "*** ERROR: couldn't create '$f'"
294 echo "directory, aborting ***"
295 exit 1
298 done
302 # Resets all vars used by a template.
304 reset_tmpl_vars()
306 local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
307 make_build_args make_install_args build_style \
308 short_desc maintainer long_desc checksum wrksrc \
309 patch_files configure_env make_cmd pkgconfig_override \
310 configure_env make_env run_stuff_before run_stuff_after \
311 run_stuff_before_configure_file run_stuff_before_build_file \
312 run_stuff_before_install_file run_stuff_after_install \
313 run_stuff_after_install_file make_build_target \
314 run_stuff_before_configure_cmd run_stuff_before_build_cmd \
315 run_stuff_before_install_cmd run_stuff_after_install_cmd \
316 make_install_target postinstall_helpers version \
317 ignore_files"
319 for i in ${TMPL_VARS}; do
320 eval unset "$i"
321 done
325 # Checks some vars used in templates and sets $extract_cmd.
327 check_tmpl_vars()
329 local pkg="$1"
330 local dfile=""
332 [ -z "$pkg" ] && return 1
334 # There's nothing of interest if we are a meta template.
336 [ "$build_style" = "meta-template" ] && return 0
338 REQ_VARS="pkgname version extract_sufx url build_style"
340 # Check if required vars weren't set.
341 for i in ${REQ_VARS}; do
342 eval val="\$$i"
343 if [ -z "$val" -o -z "$i" ]; then
344 echo -n "*** ERROR: $i not set (incomplete template"
345 echo " build file), aborting ***"
346 exit 1
348 done
350 if [ -z "$distfiles" ]; then
351 dfile="$pkgname-$version$extract_sufx"
352 elif [ -n "${distfiles}" ]; then
353 dfile="$distfiles$extract_sufx"
354 else
355 echo "*** ERROR unsupported fetch state ***"
356 exit 1
359 dfile="$PKGFS_SRCDISTDIR/$dfile"
361 case "$extract_sufx" in
362 .tar.bz2|.tar.gz|.tgz|.tbz)
363 extract_cmd="$tar_cmd xfz $dfile -C $PKGFS_BUILDDIR"
365 .tar)
366 extract_cmd="$tar_cmd xf $dfile -C $PKGFS_BUILDDIR"
368 .zip)
369 if [ -f "$PKGFS_TMPLHELPDIR/unzip-extraction.sh" ]; then
370 . $PKGFS_TMPLHELPDIR/unzip-extraction.sh
372 # $extract_cmd set by the helper
375 echo -n "*** ERROR: unknown 'extract_sufx' argument in build "
376 echo "file ***"
377 exit 1
379 esac
383 # Verifies that a checksum of a distfile is correct.
385 check_rmd160_cksum()
387 local file="$1"
388 local dfile=
390 [ -z "$file" ] && return 1
392 if [ -z "${distfiles}" ]; then
393 dfile="$pkgname-$version$extract_sufx"
394 elif [ -n "${distfiles}" ]; then
395 dfile="$distfiles$extract_sufx"
396 else
397 dfile="$file$extract_sufx"
400 if [ -z "$checksum" ]; then
401 echo "*** ERROR: checksum unset in template file for \`$pkgname' ***"
402 exit 1
405 origsum="$checksum"
406 dfile="$PKGFS_SRCDISTDIR/$dfile"
407 filesum="$($cksum_cmd $dfile | $awk_cmd '{print $4}')"
408 if [ "$origsum" != "$filesum" ]; then
409 echo "*** WARNING: checksum doesn't match (rmd160) ***"
410 return 1
413 echo "==> RMD160 checksum ok for \`$pkgname-$version'."
417 # Downloads the distfiles for a template from $url.
419 fetch_tmpl_sources()
421 local file=""
422 local file2=""
424 [ -z "$pkgname" ] && return 1
426 # There's nothing of interest if we are a meta template.
428 [ "$build_style" = "meta-template" ] && return 0
430 if [ -z "$distfiles" ]; then
431 file="$pkgname-$version"
432 else
433 file="$distfiles"
436 for f in "$file"; do
437 file2="$f$extract_sufx"
438 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
439 check_rmd160_cksum $f
440 if [ "$?" -eq 0 ]; then
441 if [ -n "$only_fetch" ]; then
442 echo "=> checksum ok"
443 exit 0
445 return 0
449 echo "==> Fetching \`$file2' source tarball."
451 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
452 if [ "$?" -ne 0 ]; then
453 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
454 echo -n "*** ERROR: couldn't fetch '$file2', "
455 echo "aborting ***"
456 else
457 echo -n "*** ERROR: there was an error "
458 echo "fetching '$file2', aborting ***"
460 exit 1
461 else
462 if [ -n "$only_fetch" ]; then
463 exit 0
466 done
470 # Extracts contents of a distfile specified in a template into
471 # the build directory.
473 extract_tmpl_sources()
475 [ -z "$pkgname" ] && return 1
478 # There's nothing of interest if we are a meta template.
480 [ "$build_style" = "meta-template" ] && return 0
482 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
484 $extract_cmd
485 if [ "$?" -ne 0 ]; then
486 echo -n "*** ERROR: there was an error extracting the "
487 echo "distfile, aborting *** "
488 exit 1
491 unset extract_cmd
492 [ -n "$only_extract" ] && exit 0
495 fixup_tmpl_libtool()
497 local lt_file="$wrksrc/libtool"
500 # If package has a libtool file replace it with ours, so that
501 # we use the master directory while relinking, all will be fine
502 # once the package is stowned.
504 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
505 $rm_cmd -f $wrksrc/libtool
506 $rm_cmd -f $wrksrc/ltmain.sh
507 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
508 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
509 $wrksrc/ltmain.sh
510 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
511 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
515 set_build_vars()
517 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
518 LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
519 CFLAGS="$CFLAGS $PKGFS_CFLAGS"
520 CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
521 CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
522 PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
524 export LDFLAGS="$LDFLAGS" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS"
525 export CPPFLAGS="$CPPFLAGS" PKG_CONFIG="$PKG_CONFIG"
528 unset_build_vars()
530 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
534 # Configures, builds and installs a package into the destination
535 # directory.
537 build_tmpl_sources()
539 local pkg="$pkgname-$version"
541 [ -z "$pkgname" -o -z "$version" ] && return 1
543 # There's nothing of interest if we are a meta template.
545 [ "$build_style" = "meta-template" ] && return 0
547 if [ -n "$distfiles" -a -z "$wrksrc" ]; then
548 wrksrc=$PKGFS_BUILDDIR/$distfiles
549 elif [ -z "$wrksrc" ]; then
550 wrksrc=$PKGFS_BUILDDIR/$pkg
551 else
552 wrksrc=$PKGFS_BUILDDIR/$wrksrc
555 if [ ! -d "$wrksrc" ]; then
556 echo "*** ERROR: unexistent build directory, aborting ***"
557 exit 1
560 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
562 # Apply patches if requested by template file
563 apply_tmpl_patches
565 echo "==> Building \`$pkg' (be patient, may take a while)"
567 # Run stuff before configure.
568 for i in "$run_stuff_before"; do
569 if [ "$i" = "configure" ]; then
570 [ -f $run_stuff_before_configure_file ] && \
571 . $run_stuff_before_configure_file
572 [ -n "$run_stuff_before_configure_cmd" ] && \
573 ${run_stuff_before_configure_cmd}
575 done
577 # Export configure_env vars.
578 for f in ${configure_env}; do
579 export "$f"
580 done
583 # Packages using GNU autoconf
585 if [ "$build_style" = "gnu_configure" ]; then
586 cd $wrksrc
587 set_build_vars
589 # Pass consistent arguments to not have unexpected
590 # surprises later.
592 ./configure \
593 --prefix="$PKGFS_MASTERDIR" \
594 --mandir="$PKGFS_DESTDIR/$pkg/man" \
595 --infodir="$PKGFS_DESTDIR/$pkg/share/info" \
596 --sysconfdir="$PKGFS_SYSCONFDIR" \
597 ${configure_args}
600 # Packages using propietary configure scripts.
602 elif [ "$build_style" = "configure" ]; then
603 cd $wrksrc
604 if [ -n "$configure_script" ]; then
605 ./$configure_script ${configure_args}
606 else
607 ./configure ${configure_args}
610 # Packages that are perl modules and use Makefile.PL files.
611 # They are all handled by the helper perl-module.sh.
613 elif [ "$build_style" = "perl_module" ]; then
614 . $PKGFS_TMPLHELPDIR/perl-module.sh
615 perl_module_build $pkgname
618 # Packages with BSD or GNU Makefiles are easy, just skip
619 # the configure stage and proceed.
621 elif [ "$build_style" = "bsd_makefile" -o \
622 "$build_style" = "gnu_makefile" ]; then
624 cd $wrksrc
626 # Unknown build_style type won't work :-)
628 else
629 echo "*** ERROR unknown build_style $build_style, aborting ***"
630 exit 1
633 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
634 echo "*** ERROR building (configure state) \`$pkg' ***"
635 exit 1
638 # unset configure_env vars.
639 for f in ${configure_env}; do
640 unset eval ${f%=*}
641 done
644 # Assume BSD make if make_cmd not set in template.
646 if [ -z "$make_cmd" ]; then
647 make_cmd="/usr/bin/make"
650 # Fixup libtool script if necessary
651 fixup_tmpl_libtool
654 # Run template stuff before building.
656 for i in ${run_stuff_before}; do
657 if [ "$i" = "build" ]; then
658 [ -f $run_stuff_before_build_file ] && \
659 . $run_stuff_before_build_file
660 [ -n "$run_stuff_before_build_cmd" ] && \
661 ${run_stuff_before_build_cmd}
663 done
665 [ -z "$make_build_target" ] && make_build_target=
666 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
668 # Export make_env vars.
669 for f in ${make_env}; do
670 export "$f"
671 done
674 # Build package via make.
676 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
677 if [ "$?" -ne 0 ]; then
678 echo "*** ERROR building (make stage) \`$pkg' ***"
679 exit 1
683 # Run template stuff before installing.
685 for i in ${run_stuff_before}; do
686 if [ "$i" = "install" ]; then
687 [ -f $run_stuff_before_install_file ] && \
688 . $run_stuff_before_install_file
689 [ -n "$run_stuff_before_install_cmd" ] && \
690 ${run_stuff_before_install_cmd}
692 done
694 [ -z "$make_install_target" ] && make_install_target=install
697 # Install package via make.
699 ${make_cmd} ${make_install_args} ${make_install_target} \
700 prefix="$PKGFS_DESTDIR/$pkg"
701 if [ "$?" -ne 0 ]; then
702 echo "*** ERROR instaling \`$pkg' ***"
703 exit 1
706 # Unset make_env vars.
707 for f in ${make_env}; do
708 unset eval ${f%=*}
709 done
712 # Run template stuff after installing.
714 for i in ${run_stuff_after}; do
715 if [ "$i" = "install" ]; then
716 [ -f $run_stuff_after_install_file ] && \
717 . $run_stuff_after_install_file
718 [ -n "$run_stuff_after_install_cmd" ] && \
719 ${run_stuff_after_install_cmd}
721 done
724 # Transform pkg-config files if requested by template.
726 for i in ${pkgconfig_override}; do
727 local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
728 [ -f "$tmpf" ] && \
729 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
730 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
731 pkgconfig_transform_file $tmpf
732 done
734 # Unset build vars.
735 unset_build_vars
737 echo "==> Installed \`$pkg' into $PKGFS_DESTDIR/$pkg."
740 # Remove $wrksrc if -C not specified.
742 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
743 $rm_cmd -rf $wrksrc
744 [ "$?" -eq 0 ] && \
745 echo "=> Removed \`$pkg' build directory."
748 cd $PKGFS_BUILDDIR
752 # Stows a currently installed package, i.e creates the links
753 # on the master directory.
755 stow_tmpl()
757 local pkg="$1"
758 local infodir_pkg="share/info/dir"
759 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
760 local real_xstowargs="$xstow_args"
761 local real_xstow_ignore="$xstow_ignore_files"
763 [ -z "$pkg" ] && return 2
765 if [ -n "$stow_flag" ]; then
766 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
767 run_file $pkg
768 pkg=$pkgname-$version
770 # You cannot stow a meta-template.
772 [ "$build_style" = "meta-template" ] && return 0
775 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
776 merge_infodir_tmpl $pkg
779 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
780 -a -r "$infodir_master" ]; then
781 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
784 if [ -n "$ignore_files" ]; then
785 xstow_ignore_files="$xstow_ignore_files $ignore_files"
788 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
789 -pd-targets $PKGFS_MASTERDIR \
790 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
791 $PKGFS_DESTDIR/$pkg
792 if [ "$?" -ne 0 ]; then
793 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
794 exit 1
795 else
796 echo "==> Created \`$pkg' symlinks into master directory."
799 installed_tmpl_handler register $pkgname $version
802 # Run template postinstall helpers if requested.
804 if [ "$pkgname" != "${pkg%%-$version}" ]; then
805 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
808 for i in ${postinstall_helpers}; do
809 local pihf="$PKGFS_TMPLHELPDIR/$i"
810 [ -f "$pihf" ] && . $pihf
811 done
813 xstow_ignore_files="$real_xstow_ignore"
814 xstow_args="$real_xstowargs"
818 # Unstows a currently stowned package, i.e removes its links
819 # from the master directory.
821 unstow_tmpl()
823 local pkg="$1"
824 local real_xstow_ignore="$xstow_ignore_files"
826 if [ -z "$pkg" ]; then
827 echo "*** ERROR: template wasn't specified? ***"
828 exit 1
831 if [ "$pkg" = "xstow" ]; then
832 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
833 exit 1
836 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
839 # You cannot unstow a meta-template.
841 [ "$build_style" = "meta-template" ] && return 0
843 if [ -n "$ignore_files" ]; then
844 xstow_ignore_files="$xstow_ignore_files $ignore_files"
847 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
848 -D -i-file-in-dir share/info/dir -ignore \
849 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
850 if [ "$?" -ne 0 ]; then
851 exit 1
852 else
853 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
854 echo "==> Removed \`$pkg' symlinks from master directory."
857 installed_tmpl_handler unregister $pkgname $version
859 xstow_ignore_files="$real_xstow_ignore"
863 # Recursive function that founds dependencies in all required
864 # packages.
866 add_dependency_tolist()
868 local curpkg="$1"
870 [ -z "$curpkg" ] && return 1
871 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
873 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
875 # Check if dep already installed.
877 if [ -r "$PKGFS_REGPKG_DB" ]; then
878 check_installed_tmpl $i ${i##[aA-zZ]*-}
880 # If dep is already installed, put it on the
881 # installed deps list and continue.
883 if [ "$?" -eq 0 ]; then
884 installed_deps_list="$i $installed_deps_list"
885 continue
888 deps_list="$i $deps_list"
889 [ -n "$prev_pkg" ] && unset prev_pkg
891 # Check if dependency needs more deps.
893 check_build_depends_tmpl ${i%-[0-9]*.*}
894 if [ "$?" -eq 0 ]; then
895 add_dependency_tolist $i
896 prev_pkg="$i"
899 done
903 # Removes duplicate deps in the installed or not installed list.
905 find_dupdeps_inlist()
907 local action="$1"
908 local tmp_list=
909 local dup=
911 [ -z "$action" ] && return 1
913 case "$action" in
914 installed)
915 list=$installed_deps_list
917 notinstalled)
918 list=$deps_list
921 return 1
923 esac
925 for f in $list; do
926 if [ -z "$tmp_list" ]; then
927 tmp_list="$f"
928 else
929 for i in $tmp_list; do
930 [ "$f" = "$i" ] && dup=yes
931 done
933 [ -z "$dup" ] && tmp_list="$tmp_list $f"
934 unset dup
936 done
938 case "$action" in
939 installed)
940 installed_deps_list="$tmp_list"
942 notinstalled)
943 deps_list="$tmp_list"
946 return 1
948 esac
952 # Installs all dependencies required by a package.
954 install_dependency_tmpl()
956 local pkg="$1"
957 deps_list=
958 installed_deps_list=
960 [ -z "$pkg" ] && return 1
962 doing_deps=true
964 add_dependency_tolist $pkg
965 find_dupdeps_inlist installed
966 find_dupdeps_inlist notinstalled
968 echo "==> Required dependencies for $(basename $pkg):"
969 for i in ${installed_deps_list}; do
970 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
971 echo " $i: found $fpkg."
972 done
974 for i in ${deps_list}; do
975 echo " $i: not installed."
976 done
978 for i in ${deps_list}; do
979 # skip dup deps
980 echo "=> Installing dependency: $i"
981 install_tmpl ${i%-[0-9].*}
982 done
984 unset installed_deps_list
985 unset deps_list
989 # Installs and stows the "xstow" package.
991 install_xstow_tmpl()
993 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
995 reset_tmpl_vars
996 run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
997 check_tmpl_vars $pkgname-$version
998 fetch_tmpl_sources
999 extract_tmpl_sources
1000 build_tmpl_sources
1001 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
1002 stow_tmpl $pkgname-$version
1004 # Continue with origin package that called us.
1006 run_file $origin_tmpl
1010 # Registers or unregisters a package from the db file.
1012 installed_tmpl_handler()
1014 local action="$1"
1015 local pkg="$2"
1016 local version="$3"
1018 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
1019 if [ "$action" = "register" ]; then
1020 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
1021 if [ "$?" -ne 0 ]; then
1022 echo -n "*** ERROR: couldn't register \`$pkg'"
1023 echo " in db file ***"
1024 exit 1
1026 elif [ "$action" = "unregister" ]; then
1027 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
1028 if [ "$?" -ne 0 ]; then
1029 echo -n "*** ERROR: \`$pkg' not registered "
1030 echo "in db file? ***"
1031 exit 1
1033 else
1034 return 1
1039 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
1040 # the minimal required version if there, or 1 otherwise.
1042 check_installed_tmpl()
1044 local pkg="$1"
1045 local reqver="$2"
1046 local iver=
1048 [ -z "$pkg" -o -z "$reqver" ] && return 1
1050 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1052 reqver="$(echo $reqver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1054 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1055 if [ "$?" -eq 0 ]; then
1057 # Package is installed, let's check the version.
1059 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1060 if [ -n "$iver" ]; then
1062 # As shell only supports decimal arith expressions,
1063 # we simply remove anything except the numbers.
1064 # It's not optimal and may fail, but it is enough
1065 # for now.
1067 iver="$(echo $iver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1068 if [ "$iver" -eq "$reqver" \
1069 -o "$iver" -gt "$reqver" ]; then
1070 return 0
1075 return 1
1079 # Checks the build depends db file and returns 0 if pkg has dependencies,
1080 # otherwise returns 1.
1082 check_build_depends_tmpl()
1084 local pkg="$1"
1086 [ -z $pkg ] && return 1
1087 [ ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1089 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1090 return $?
1094 # Installs a pkg by reading its build template file.
1096 install_tmpl()
1098 local pkg=
1099 cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
1100 if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
1101 echo -n "*** ERROR: invalid template file '$cur_tmpl',"
1102 echo " aborting ***"
1103 exit 1
1106 reset_tmpl_vars
1107 run_file $cur_tmpl
1108 pkg="$1-$version"
1111 # If we are the originator package save the path this template in
1112 # other var for future use.
1114 if [ -z "$origin_tmpl" ]; then
1115 origin_tmpl=$path_fixed
1119 # Install xstow if it's not there.
1121 install_xstow_tmpl
1124 # Check vars for current template.
1126 check_tmpl_vars $pkg
1129 # Install dependencies required by this package.
1131 check_build_depends_tmpl $pkg
1132 if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
1133 install_dependency_tmpl $pkg
1135 # At this point all required deps are installed, and
1136 # only remaining is the origin template; install it.
1138 unset doing_deps
1139 reset_tmpl_vars
1140 run_file $origin_tmpl
1141 check_tmpl_vars $pkgname-$version
1144 if [ -n "$only_build" ]; then
1145 build_tmpl_sources
1146 exit 0
1150 # Fetch, extract, stow and reset vars for a template.
1152 fetch_tmpl_sources
1153 extract_tmpl_sources
1154 build_tmpl_sources
1157 # Just announce that meta-template is installed and exit.
1159 if [ "$build_style" = "meta-template" ]; then
1160 installed_tmpl_handler register $pkgname $version
1161 echo "==> Installed meta-template \`$pkg'."
1162 return 0
1166 # Do not stow the pkg if requested.
1168 [ -z "$only_install" ] && stow_tmpl $pkg
1172 # Lists all currently installed packages.
1174 list_tmpls()
1176 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1177 echo "=> No packages registered or missing register db file."
1178 exit 0
1181 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1182 # Run file to get short_desc and print something useful
1183 run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
1184 echo "$i-$version $short_desc"
1185 reset_tmpl_vars
1186 done
1190 # Removes a currently installed package (unstow + removed from destdir).
1192 remove_tmpl()
1194 local pkg="$1"
1196 if [ -z "$pkg" ]; then
1197 echo "*** ERROR: unexistent package, aborting ***"
1198 exit 1
1201 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1202 echo "*** ERROR: cannot find template file ***"
1203 exit 1
1206 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1209 # If it's a meta-template, just unregister it from the db.
1211 if [ "$build_style" = "meta-template" ]; then
1212 installed_tmpl_handler unregister $pkgname $version
1213 [ "$?" -eq 0 ] && \
1214 echo "=> Removed meta-template \`$pkg'."
1215 return $?
1218 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1219 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1220 exit 1
1223 unstow_tmpl $pkg
1224 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1225 return "$?"
1229 # main()
1231 args=$(getopt bCc:efi $*)
1232 [ "$?" -ne 0 ] && usage
1234 set -- $args
1235 while [ "$#" -gt 0 ]; do
1236 case "$1" in
1238 only_build=yes
1241 dontrm_builddir=yes
1244 config_file_specified=yes
1245 PKGFS_CONFIG_FILE="$2"
1246 shift
1249 only_extract=yes
1252 only_fetch=yes
1255 only_install=yes
1258 shift
1259 break
1261 esac
1262 shift
1263 done
1265 [ "$#" -gt 2 ] && usage
1267 target="$1"
1268 if [ -z "$target" ]; then
1269 echo "*** ERROR missing target ***"
1270 usage
1274 # Check configuration vars before anyting else, and set defaults vars.
1276 check_config_vars
1277 set_defvars
1279 # Main switch
1280 case "$target" in
1281 info)
1282 info_tmpl "$2"
1284 install)
1285 install_tmpl "$2"
1287 list)
1288 list_tmpls
1290 remove)
1291 remove_tmpl "$2"
1293 stow)
1294 stow_flag=yes
1295 stow_tmpl "$2"
1297 unstow)
1298 unstow_tmpl "$2"
1301 echo "*** ERROR invalid target '$target' ***"
1302 usage
1303 esac
1305 # Agur
1306 exit 0