Introduce the make_env var for templates, and other related changes.
[pkgfs.git] / pkgfs.sh
blob4b8394a07f4dd82a15f1389a6ab397d81cc1b6e4
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 if [ ! -d "$PKGFS_DEPSDIR" ]; then
80 echo "**** ERROR: cannot find $i, aborting ***"
81 exit 1
83 done
86 usage()
88 cat << _EOF
89 $progname: [-bCefi] [-c <config_file>] <target> <tmplname>
91 Targets
92 info Show information about <tmplname>.
93 install Build and install package from <tmplname>.
94 list Lists and prints short description about installed packages.
95 remove Remove package completely (unstow and remove from destdir)
96 stow Create symlinks from <tmplname> in master directory.
97 unstow Remove symlinks from <tmplname> in master directory.
99 Options
100 -b Only build the source distribution file(s), without
101 extracting or fetching before.
102 -C Do not remove build directory after successful build.
103 -c Path to global configuration file.
104 If not specified /usr/local/etc/pkgfs.conf is used.
105 -e Only extract the source distribution file(s).
106 -f Only fetch the source distribution file(s).
107 -i Only build and install the binary distribution file(s)
108 into the destdir directory, without stowning.
109 _EOF
110 exit 1
113 check_path()
115 eval local orig="$1"
117 case "$orig" in
121 orig="${orig%/}"
124 orig="$topdir/${orig%/}"
126 esac
128 path_fixed="$orig"
131 run_file()
133 local file="$1"
135 check_path "$file"
136 . $path_fixed
140 # This function merges two GNU info dirs into one and puts the result
141 # into PKGFS_MASTERDIR/share/info/dir.
143 merge_infodir_tmpl()
145 local pkgname="$1"
146 local merge_info_cmd="$PKGFS_MASTERDIR/bin/merge-info"
148 [ -z "$pkgname" ] && return 1
149 [ ! -r "$PKGFS_MASTERDIR/share/info/dir" ] && return 1
150 [ ! -r "$PKGFS_DESTDIR/$pkgname/share/info/dir" ] && return 1
152 $merge_info_cmd -d $PKGFS_MASTERDIR/share/info/dir \
153 $PKGFS_DESTDIR/$pkgname/share/info/dir -o \
154 $PKGFS_MASTERDIR/share/info/dir.new
155 if [ "$?" -ne 0 ]; then
156 echo -n "*** WARNING: there was an error merging info dir from"
157 echo " $pkgname, aborting ***"
158 return 1
161 $mv_cmd -f $PKGFS_MASTERDIR/share/info/dir.new \
162 $PKGFS_MASTERDIR/share/info/dir
166 # Shows info about a template.
168 info_tmpl()
170 local tmpl="$1"
171 if [ -z "$tmpl" -o ! -f "$PKGFS_TEMPLATESDIR/$tmpl.tmpl" ]; then
172 echo -n "*** ERROR: invalid template file \`$tmpl' ***"
173 echo ", aborting ***"
174 exit 1
177 run_file $PKGFS_TEMPLATESDIR/$tmpl.tmpl
179 echo "pkgname: $pkgname"
180 echo "version: $version"
181 for i in "${distfiles}"; do
182 [ -n "$i" ] && echo "distfile: $i"
183 done
184 echo "URL: $url"
185 echo "maintainer: $maintainer"
186 [ -n $checksum ] && echo "checksum: $checksum"
187 echo "build_style: $build_style"
188 echo "short_desc: $short_desc"
189 echo "$long_desc"
190 echo
191 check_build_depends_tmpl $pkgname-$version
192 if [ "$?" -eq 0 ]; then
193 local list="$($db_cmd -V btree $PKGFS_BUILD_DEPS_DB $pkgname)"
194 echo "This package requires the following dependencies to be built:"
195 for i in ${list}; do
196 echo " $i"
197 done
202 # Applies to the build directory the patches specified by a template.
204 apply_tmpl_patches()
206 if [ -z "$PKGFS_TEMPLATESDIR" ]; then
207 echo -n "*** WARNING: PKGFS_TEMPLATESDIR is not set, "
208 echo "won't apply patches ***"
209 return 1
213 # If package needs some patches applied before building,
214 # apply them now.
216 if [ -n "$patch_files" ]; then
217 for i in ${patch_files}; do
218 patch="$PKGFS_TEMPLATESDIR/$i"
219 if [ ! -f "$patch" ]; then
220 echo "*** WARNING: unexistent patch '$i' ***"
221 continue
224 # Try to guess if its a compressed patch.
225 if $(echo $patch|$grep_cmd -q .gz); then
226 $gunzip_cmd $patch
227 patch=${patch%%.gz}
228 elif $(echo $patch|$grep_cmd -q .bz2); then
229 $bunzip2_cmd $patch
230 patch=${patch%%.bz2}
231 elif $(echo $patch|$grep_cmd -q .diff); then
232 # nada
233 else
234 echo "*** WARNING: unknown patch type '$i' ***"
235 continue
238 cd $wrksrc && $patch_cmd < $patch 2>/dev/null
239 if [ "$?" -eq 0 ]; then
240 echo "=> Patch applied: \`$i'."
241 else
242 echo -n "*** ERROR: couldn't apply patch '$i'"
243 echo ", aborting ***"
244 exit 1
246 done
251 # Checks that all required variables specified in the configuration
252 # file are properly working.
254 check_config_vars()
256 local cffound=
258 if [ -z "$config_file_specified" ]; then
259 config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
260 for f in $config_file_paths; do
261 [ -f $f ] && PKGFS_CONFIG_FILE=$f && \
262 cffound=yes && break
263 done
264 if [ -z "$cffound" ]; then
265 echo -n "*** ERROR: config file not specified "
266 echo "and not in default location or current dir ***"
267 exit 1
271 run_file ${PKGFS_CONFIG_FILE}
272 PKGFS_CONFIG_FILE=$path_fixed
274 if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
275 echo -n "*** ERROR: cannot find configuration file: "
276 echo "'$PKGFS_CONFIG_FILE' ***"
277 exit 1
280 local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
281 PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"
283 for f in ${PKGFS_VARS}; do
284 eval val="\$$f"
285 if [ -z "$val" ]; then
286 echo -n "**** ERROR: '$f' not set in configuration "
287 echo "file, aborting ***"
288 exit 1
291 if [ ! -d "$f" ]; then
292 $mkdir_cmd "$val"
293 if [ "$?" -ne 0 ]; then
294 echo -n "*** ERROR: couldn't create '$f'"
295 echo "directory, aborting ***"
296 exit 1
299 done
303 # Resets all vars used by a template.
305 reset_tmpl_vars()
307 local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
308 make_build_args make_install_args build_style \
309 short_desc maintainer long_desc checksum wrksrc \
310 patch_files configure_env make_cmd pkgconfig_override \
311 configure_env make_env run_stuff_before run_stuff_after \
312 run_stuff_before_configure_file run_stuff_before_build_file \
313 run_stuff_before_install_file run_stuff_after_install \
314 run_stuff_after_install_file make_build_target \
315 run_stuff_before_configure_cmd run_stuff_before_build_cmd \
316 run_stuff_before_install_cmd run_stuff_after_install_cmd \
317 make_install_target postinstall_helpers version \
318 ignore_files"
320 for i in ${TMPL_VARS}; do
321 eval unset "$i"
322 done
326 # Checks some vars used in templates and sets $extract_cmd.
328 check_tmpl_vars()
330 local pkg="$1"
331 local dfile=""
333 [ -z "$pkg" ] && return 1
335 # There's nothing of interest if we are a meta template.
337 [ "$build_style" = "meta-template" ] && return 0
339 REQ_VARS="pkgname version extract_sufx url build_style"
341 # Check if required vars weren't set.
342 for i in ${REQ_VARS}; do
343 eval val="\$$i"
344 if [ -z "$val" -o -z "$i" ]; then
345 echo -n "*** ERROR: $i not set (incomplete template"
346 echo " build file), aborting ***"
347 exit 1
349 done
351 if [ -z "$distfiles" ]; then
352 dfile="$pkgname-$version$extract_sufx"
353 elif [ -n "${distfiles}" ]; then
354 dfile="$distfiles$extract_sufx"
355 else
356 echo "*** ERROR unsupported fetch state ***"
357 exit 1
360 dfile="$PKGFS_SRCDISTDIR/$dfile"
362 case "$extract_sufx" in
363 .tar.bz2|.tar.gz|.tgz|.tbz)
364 extract_cmd="$tar_cmd xfz $dfile -C $PKGFS_BUILDDIR"
366 .tar)
367 extract_cmd="$tar_cmd xf $dfile -C $PKGFS_BUILDDIR"
369 .zip)
370 if [ -f "$PKGFS_TMPLHELPDIR/unzip-extraction.sh" ]; then
371 . $PKGFS_TMPLHELPDIR/unzip-extraction.sh
373 # $extract_cmd set by the helper
376 echo -n "*** ERROR: unknown 'extract_sufx' argument in build "
377 echo "file ***"
378 exit 1
380 esac
384 # Verifies that a checksum of a distfile is correct.
386 check_rmd160_cksum()
388 local file="$1"
389 local dfile=
391 [ -z "$file" ] && return 1
393 if [ -z "${distfiles}" ]; then
394 dfile="$pkgname-$version$extract_sufx"
395 elif [ -n "${distfiles}" ]; then
396 dfile="$distfiles$extract_sufx"
397 else
398 dfile="$file$extract_sufx"
401 if [ -z "$checksum" ]; then
402 echo "*** ERROR: checksum unset in template file for \`$pkgname' ***"
403 exit 1
406 origsum="$checksum"
407 dfile="$PKGFS_SRCDISTDIR/$dfile"
408 filesum="$($cksum_cmd $dfile | $awk_cmd '{print $4}')"
409 if [ "$origsum" != "$filesum" ]; then
410 echo "*** WARNING: checksum doesn't match (rmd160) ***"
411 return 1
416 # Downloads the distfiles for a template from $url.
418 fetch_tmpl_sources()
420 local file=""
421 local file2=""
423 [ -z "$pkgname" ] && return 1
425 # There's nothing of interest if we are a meta template.
427 [ "$build_style" = "meta-template" ] && return 0
429 if [ -z "$distfiles" ]; then
430 file="$pkgname-$version"
431 else
432 file="$distfiles"
435 for f in "$file"; do
436 file2="$f$extract_sufx"
437 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
438 check_rmd160_cksum $f
439 if [ "$?" -eq 0 ]; then
440 if [ -n "$only_fetch" ]; then
441 echo "=> checksum ok"
442 exit 0
444 return 0
448 echo "==> Fetching \`$file2' source tarball"
450 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
451 if [ "$?" -ne 0 ]; then
452 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
453 echo -n "*** ERROR: couldn't fetch '$file2', "
454 echo "aborting ***"
455 else
456 echo -n "*** ERROR: there was an error "
457 echo "fetching '$file2', aborting ***"
459 exit 1
460 else
461 if [ -n "$only_fetch" ]; then
462 exit 0
465 done
469 # Extracts contents of a distfile specified in a template into
470 # the build directory.
472 extract_tmpl_sources()
474 [ -z "$pkgname" ] && return 1
477 # There's nothing of interest if we are a meta template.
479 [ "$build_style" = "meta-template" ] && return 0
481 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
483 $extract_cmd
484 if [ "$?" -ne 0 ]; then
485 echo -n "*** ERROR: there was an error extracting the "
486 echo "distfile, aborting *** "
487 exit 1
490 unset extract_cmd
491 [ -n "$only_extract" ] && exit 0
494 fixup_tmpl_libtool()
496 local lt_file="$wrksrc/libtool"
499 # If package has a libtool file replace it with ours, so that
500 # we use the master directory while relinking, all will be fine
501 # once the package is stowned.
503 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
504 $rm_cmd -f $wrksrc/libtool
505 $rm_cmd -f $wrksrc/ltmain.sh
506 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
507 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
508 $wrksrc/ltmain.sh
509 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
510 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
514 set_build_vars()
516 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
517 LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
518 CFLAGS="$CFLAGS $PKGFS_CFLAGS"
519 CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
520 CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
521 PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
523 export LDFLAGS="$LDFLAGS" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS"
524 export CPPFLAGS="$CPPFLAGS" PKG_CONFIG="$PKG_CONFIG"
527 unset_build_vars()
529 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
533 # Configures, builds and installs a package into the destination
534 # directory.
536 build_tmpl_sources()
538 local pkg="$pkgname-$version"
540 [ -z "$pkgname" -o -z "$version" ] && return 1
542 # There's nothing of interest if we are a meta template.
544 [ "$build_style" = "meta-template" ] && return 0
546 if [ -n "$distfiles" -a -z "$wrksrc" ]; then
547 wrksrc=$PKGFS_BUILDDIR/$distfiles
548 elif [ -z "$wrksrc" ]; then
549 wrksrc=$PKGFS_BUILDDIR/$pkg
550 else
551 wrksrc=$PKGFS_BUILDDIR/$wrksrc
554 if [ ! -d "$wrksrc" ]; then
555 echo "*** ERROR: unexistent build directory, aborting ***"
556 exit 1
559 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
561 # Apply patches if requested by template file
562 apply_tmpl_patches
564 echo "==> Building \`$pkg' (be patient, may take a while)"
566 # Run stuff before configure.
567 for i in "$run_stuff_before"; do
568 if [ "$i" = "configure" ]; then
569 [ -f $run_stuff_before_configure_file ] && \
570 . $run_stuff_before_configure_file
571 [ -n "$run_stuff_before_configure_cmd" ] && \
572 ${run_stuff_before_configure_cmd}
574 done
576 # Export configure_env vars.
577 for f in ${configure_env}; do
578 export "$f"
579 done
582 # Packages using GNU autoconf
584 if [ "$build_style" = "gnu_configure" ]; then
585 cd $wrksrc
586 set_build_vars
588 # Pass consistent arguments to not have unexpected
589 # surprises later.
591 ./configure \
592 --prefix="$PKGFS_MASTERDIR" \
593 --mandir="$PKGFS_DESTDIR/$pkg/man" \
594 --infodir="$PKGFS_DESTDIR/$pkg/share/info" \
595 --sysconfdir="$PKGFS_SYSCONFDIR" \
596 ${configure_args}
599 # Packages using propietary configure scripts.
601 elif [ "$build_style" = "configure" ]; then
602 cd $wrksrc
603 if [ -n "$configure_script" ]; then
604 ./$configure_script ${configure_args}
605 else
606 ./configure ${configure_args}
609 # Packages that are perl modules and use Makefile.PL files.
610 # They are all handled by the helper perl-module.sh.
612 elif [ "$build_style" = "perl_module" ]; then
613 . $PKGFS_TMPLHELPDIR/perl-module.sh
614 perl_module_build $pkgname
617 # Packages with BSD or GNU Makefiles are easy, just skip
618 # the configure stage and proceed.
620 elif [ "$build_style" = "bsd_makefile" -o \
621 "$build_style" = "gnu_makefile" ]; then
623 cd $wrksrc
625 # Unknown build_style type won't work :-)
627 else
628 echo "*** ERROR unknown build_style $build_style, aborting ***"
629 exit 1
632 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
633 echo "*** ERROR building (configure state) \`$pkg' ***"
634 exit 1
637 # unset configure_env vars.
638 for f in ${configure_env}; do
639 unset eval ${f%=*}
640 done
643 # Assume BSD make if make_cmd not set in template.
645 if [ -z "$make_cmd" ]; then
646 make_cmd="/usr/bin/make"
649 # Fixup libtool script if necessary
650 fixup_tmpl_libtool
653 # Run template stuff before building.
655 for i in ${run_stuff_before}; do
656 if [ "$i" = "build" ]; then
657 [ -f $run_stuff_before_build_file ] && \
658 . $run_stuff_before_build_file
659 [ -n "$run_stuff_before_build_cmd" ] && \
660 ${run_stuff_before_build_cmd}
662 done
664 [ -z "$make_build_target" ] && make_build_target=
665 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
667 # Export make_env vars.
668 for f in ${make_env}; do
669 export "$f"
670 done
673 # Build package via make.
675 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
676 if [ "$?" -ne 0 ]; then
677 echo "*** ERROR building (make stage) \`$pkg' ***"
678 exit 1
682 # Run template stuff before installing.
684 for i in ${run_stuff_before}; do
685 if [ "$i" = "install" ]; then
686 [ -f $run_stuff_before_install_file ] && \
687 . $run_stuff_before_install_file
688 [ -n "$run_stuff_before_install_cmd" ] && \
689 ${run_stuff_before_install_cmd}
691 done
693 [ -z "$make_install_target" ] && make_install_target=install
696 # Install package via make.
698 ${make_cmd} ${make_install_args} ${make_install_target} \
699 prefix="$PKGFS_DESTDIR/$pkg"
700 if [ "$?" -ne 0 ]; then
701 echo "*** ERROR instaling \`$pkg' ***"
702 exit 1
705 # Unset make_env vars.
706 for f in ${make_env}; do
707 unset eval ${f%=*}
708 done
711 # Run template stuff after installing.
713 for i in ${run_stuff_after}; do
714 if [ "$i" = "install" ]; then
715 [ -f $run_stuff_after_install_file ] && \
716 . $run_stuff_after_install_file
717 [ -n "$run_stuff_after_install_cmd" ] && \
718 ${run_stuff_after_install_cmd}
720 done
723 # Transform pkg-config files if requested by template.
725 for i in ${pkgconfig_override}; do
726 local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
727 [ -f "$tmpf" ] && \
728 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
729 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
730 pkgconfig_transform_file $tmpf
731 done
733 # Unset build vars.
734 unset_build_vars
736 echo "==> Installed \`$pkg' into $PKGFS_DESTDIR/$pkg."
739 # Remove $wrksrc if -C not specified.
741 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
742 $rm_cmd -rf $wrksrc
743 [ "$?" -eq 0 ] && \
744 echo "=> Removed \`$pkg' build directory."
747 cd $PKGFS_BUILDDIR
751 # Stows a currently installed package, i.e creates the links
752 # on the master directory.
754 stow_tmpl()
756 local pkg="$1"
757 local infodir_pkg="share/info/dir"
758 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
759 local real_xstowargs="$xstow_args"
760 local real_xstow_ignore="$xstow_ignore_files"
762 [ -z "$pkg" ] && return 2
764 if [ -n "$stow_flag" ]; then
765 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
766 run_file $pkg
767 pkg=$pkgname-$version
769 # You cannot stow a meta-template.
771 [ "$build_style" = "meta-template" ] && return 0
774 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
775 merge_infodir_tmpl $pkg
778 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
779 -a -r "$infodir_master" ]; then
780 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
783 if [ -n "$ignore_files" ]; then
784 xstow_ignore_files="$xstow_ignore_files $ignore_files"
787 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
788 -pd-targets $PKGFS_MASTERDIR \
789 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
790 $PKGFS_DESTDIR/$pkg
791 if [ "$?" -ne 0 ]; then
792 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
793 exit 1
794 else
795 echo "==> Created \`$pkg' symlinks into master directory."
798 installed_tmpl_handler register $pkgname $version
801 # Run template postinstall helpers if requested.
803 if [ "$pkgname" != "${pkg%%-$version}" ]; then
804 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
807 for i in ${postinstall_helpers}; do
808 local pihf="$PKGFS_TMPLHELPDIR/$i"
809 [ -f "$pihf" ] && . $pihf
810 done
812 xstow_ignore_files="$real_xstow_ignore"
813 xstow_args="$real_xstowargs"
817 # Unstows a currently stowned package, i.e removes its links
818 # from the master directory.
820 unstow_tmpl()
822 local pkg="$1"
823 local real_xstow_ignore="$xstow_ignore_files"
825 if [ -z "$pkg" ]; then
826 echo "*** ERROR: template wasn't specified? ***"
827 exit 1
830 if [ "$pkg" = "xstow" ]; then
831 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
832 exit 1
835 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
838 # You cannot unstow a meta-template.
840 [ "$build_style" = "meta-template" ] && return 0
842 if [ -n "$ignore_files" ]; then
843 xstow_ignore_files="$xstow_ignore_files $ignore_files"
846 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
847 -D -i-file-in-dir share/info/dir -ignore \
848 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
849 if [ "$?" -ne 0 ]; then
850 exit 1
851 else
852 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
853 echo "==> Removed \`$pkg' symlinks from master directory."
856 installed_tmpl_handler unregister $pkgname $version
858 xstow_ignore_files="$real_xstow_ignore"
862 # Recursive function that founds dependencies in all required
863 # packages.
865 add_dependency_tolist()
867 local curpkg="$1"
869 [ -z "$curpkg" ] && return 1
870 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
872 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
874 # Check if dep already installed.
876 if [ -r "$PKGFS_REGPKG_DB" ]; then
877 check_installed_tmpl $i ${i##[aA-zZ]*-}
879 # If dep is already installed, put it on the
880 # installed deps list and continue.
882 if [ "$?" -eq 0 ]; then
883 installed_deps_list="$i $installed_deps_list"
884 continue
887 deps_list="$i $deps_list"
888 [ -n "$prev_pkg" ] && unset prev_pkg
890 # Check if dependency needs more deps.
892 check_build_depends_tmpl ${i%-[0-9]*.*}
893 if [ "$?" -eq 0 ]; then
894 add_dependency_tolist $i
895 prev_pkg="$i"
898 done
902 # Removes duplicate deps in the installed or not installed list.
904 find_dupdeps_inlist()
906 local action="$1"
907 local tmp_list=
908 local dup=
910 [ -z "$action" ] && return 1
912 case "$action" in
913 installed)
914 list=$installed_deps_list
916 notinstalled)
917 list=$deps_list
920 return 1
922 esac
924 for f in $list; do
925 if [ -z "$tmp_list" ]; then
926 tmp_list="$f"
927 else
928 for i in $tmp_list; do
929 [ "$f" = "$i" ] && dup=yes
930 done
932 [ -z "$dup" ] && tmp_list="$tmp_list $f"
933 unset dup
935 done
937 case "$action" in
938 installed)
939 installed_deps_list="$tmp_list"
941 notinstalled)
942 deps_list="$tmp_list"
945 return 1
947 esac
951 # Installs all dependencies required by a package.
953 install_dependency_tmpl()
955 local pkg="$1"
956 deps_list=
957 installed_deps_list=
959 [ -z "$pkg" ] && return 1
961 doing_deps=true
963 add_dependency_tolist $pkg
964 find_dupdeps_inlist installed
965 find_dupdeps_inlist notinstalled
967 echo "==> Required dependencies for $(basename $pkg):"
968 for i in ${installed_deps_list}; do
969 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
970 echo " $i: found $fpkg."
971 done
973 for i in ${deps_list}; do
974 echo " $i: not installed."
975 done
977 for i in ${deps_list}; do
978 # skip dup deps
979 echo "=> Installing dependency: $i"
980 install_tmpl ${i%-[0-9].*}
981 done
983 unset installed_deps_list
984 unset deps_list
988 # Installs and stows the "xstow" package.
990 install_xstow_tmpl()
992 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
994 reset_tmpl_vars
995 run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
996 check_tmpl_vars $pkgname-$version
997 fetch_tmpl_sources
998 extract_tmpl_sources
999 build_tmpl_sources
1000 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
1001 stow_tmpl $pkgname-$version
1003 # Continue with origin package that called us.
1005 run_file $origin_tmpl
1009 # Registers or unregisters a package from the db file.
1011 installed_tmpl_handler()
1013 local action="$1"
1014 local pkg="$2"
1015 local version="$3"
1017 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
1018 if [ "$action" = "register" ]; then
1019 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
1020 if [ "$?" -ne 0 ]; then
1021 echo -n "*** ERROR: couldn't register \`$pkg'"
1022 echo " in db file ***"
1023 exit 1
1025 elif [ "$action" = "unregister" ]; then
1026 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
1027 if [ "$?" -ne 0 ]; then
1028 echo -n "*** ERROR: \`$pkg' not registered "
1029 echo "in db file? ***"
1030 exit 1
1032 else
1033 return 1
1038 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
1039 # the minimal required version if there, or 1 otherwise.
1041 check_installed_tmpl()
1043 local pkg="$1"
1044 local reqver="$2"
1045 local iver=
1047 [ -z "$pkg" -o -z "$reqver" ] && return 1
1049 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1051 reqver="$(echo $reqver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1053 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1054 if [ "$?" -eq 0 ]; then
1056 # Package is installed, let's check the version.
1058 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1059 if [ -n "$iver" ]; then
1061 # As shell only supports decimal arith expressions,
1062 # we simply remove anything except the numbers.
1063 # It's not optimal and may fail, but it is enough
1064 # for now.
1066 iver="$(echo $iver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1067 if [ "$iver" -eq "$reqver" \
1068 -o "$iver" -gt "$reqver" ]; then
1069 return 0
1074 return 1
1078 # Checks the build depends db file and returns 0 if pkg has dependencies,
1079 # otherwise returns 1.
1081 check_build_depends_tmpl()
1083 local pkg="$1"
1085 [ -z $pkg ] && return 1
1086 [ ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1088 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1089 return $?
1093 # Installs a pkg by reading its build template file.
1095 install_tmpl()
1097 local pkg=
1098 cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
1099 if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
1100 echo -n "*** ERROR: invalid template file '$cur_tmpl',"
1101 echo " aborting ***"
1102 exit 1
1105 reset_tmpl_vars
1106 run_file $cur_tmpl
1107 pkg="$1-$version"
1110 # If we are the originator package save the path this template in
1111 # other var for future use.
1113 if [ -z "$origin_tmpl" ]; then
1114 origin_tmpl=$path_fixed
1118 # Install xstow if it's not there.
1120 install_xstow_tmpl
1123 # Check vars for current template.
1125 check_tmpl_vars $pkg
1128 # Install dependencies required by this package.
1130 check_build_depends_tmpl $pkg
1131 if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
1132 install_dependency_tmpl $pkg
1134 # At this point all required deps are installed, and
1135 # only remaining is the origin template; install it.
1137 unset doing_deps
1138 reset_tmpl_vars
1139 run_file $origin_tmpl
1140 check_tmpl_vars $pkgname-$version
1143 if [ -n "$only_build" ]; then
1144 build_tmpl_sources
1145 exit 0
1149 # Fetch, extract, stow and reset vars for a template.
1151 fetch_tmpl_sources
1152 extract_tmpl_sources
1153 build_tmpl_sources
1156 # Just announce that meta-template is installed and exit.
1158 if [ "$build_style" = "meta-template" ]; then
1159 installed_tmpl_handler register $pkgname $version
1160 echo "==> Installed meta-template \`$pkg'."
1161 return 0
1165 # Do not stow the pkg if requested.
1167 [ -z "$only_install" ] && stow_tmpl $pkg
1171 # Lists all currently installed packages.
1173 list_tmpls()
1175 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1176 echo "=> No packages registered or missing register db file."
1177 exit 0
1180 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1181 # Run file to get short_desc and print something useful
1182 run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
1183 echo "$i-$version $short_desc"
1184 reset_tmpl_vars
1185 done
1189 # Removes a currently installed package (unstow + removed from destdir).
1191 remove_tmpl()
1193 local pkg="$1"
1195 if [ -z "$pkg" ]; then
1196 echo "*** ERROR: unexistent package, aborting ***"
1197 exit 1
1200 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1201 echo "*** ERROR: cannot find template file ***"
1202 exit 1
1205 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1208 # If it's a meta-template, just unregister it from the db.
1210 if [ "$build_style" = "meta-template" ]; then
1211 installed_tmpl_handler unregister $pkgname $version
1212 [ "$?" -eq 0 ] && \
1213 echo "=> Removed meta-template \`$pkg'."
1214 return $?
1217 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1218 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1219 exit 1
1222 unstow_tmpl $pkg
1223 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1224 return "$?"
1228 # main()
1230 args=$(getopt bCc:efi $*)
1231 [ "$?" -ne 0 ] && usage
1233 set -- $args
1234 while [ "$#" -gt 0 ]; do
1235 case "$1" in
1237 only_build=yes
1240 dontrm_builddir=yes
1243 config_file_specified=yes
1244 PKGFS_CONFIG_FILE="$2"
1245 shift
1248 only_extract=yes
1251 only_fetch=yes
1254 only_install=yes
1257 shift
1258 break
1260 esac
1261 shift
1262 done
1264 [ "$#" -gt 2 ] && usage
1266 target="$1"
1267 if [ -z "$target" ]; then
1268 echo "*** ERROR missing target ***"
1269 usage
1273 # Check configuration vars before anyting else, and set defaults vars.
1275 check_config_vars
1276 set_defvars
1278 # Main switch
1279 case "$target" in
1280 info)
1281 info_tmpl "$2"
1283 install)
1284 install_tmpl "$2"
1286 list)
1287 list_tmpls
1289 remove)
1290 remove_tmpl "$2"
1292 stow)
1293 stow_flag=yes
1294 stow_tmpl "$2"
1296 unstow)
1297 unstow_tmpl "$2"
1300 echo "*** ERROR invalid target '$target' ***"
1301 usage
1302 esac
1304 # Agur
1305 exit 0