pkgfs.sh: misc changes to collapse various conditionals into one line.
[pkgfs.git] / pkgfs.sh
blob3ceb3de8ea5824e82486de91d721f1d97dcd2cb1
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" -o ! -r "$PKGFS_MASTERDIR/share/info/dir" \
150 -o ! -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 local patch=
209 # If package needs some patches applied before building,
210 # apply them now.
212 if [ -n "$patch_files" ]; then
213 for i in ${patch_files}; do
214 patch="$PKGFS_TEMPLATESDIR/$i"
215 if [ ! -f "$patch" ]; then
216 echo "*** WARNING: unexistent patch '$i' ***"
217 continue
220 $cp_cmd -f $patch $wrksrc
222 # Try to guess if its a compressed patch.
223 if $(echo $patch|$grep_cmd -q .gz); then
224 $gunzip_cmd $wrksrc/$i
225 patch=${i%%.gz}
226 elif $(echo $patch|$grep_cmd -q .bz2); then
227 $bunzip2_cmd $wrksrc/$i
228 patch=${i%%.bz2}
229 elif $(echo $patch|$grep_cmd -q .diff); then
230 patch=$i
231 else
232 echo "*** WARNING: unknown patch type '$i' ***"
233 continue
236 cd $wrksrc && $patch_cmd < $patch 2>/dev/null
237 if [ "$?" -eq 0 ]; then
238 echo "=> Patch applied: \`$i'."
239 else
240 echo -n "*** ERROR: couldn't apply patch '$i'"
241 echo ", aborting ***"
242 exit 1
244 done
249 # Checks that all required variables specified in the configuration
250 # file are properly working.
252 check_config_vars()
254 local cffound=
256 if [ -z "$config_file_specified" ]; then
257 config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
258 for f in $config_file_paths; do
259 [ -f $f ] && PKGFS_CONFIG_FILE=$f && \
260 cffound=yes && break
261 done
262 if [ -z "$cffound" ]; then
263 echo -n "*** ERROR: config file not specified "
264 echo "and not in default location or current dir ***"
265 exit 1
269 run_file ${PKGFS_CONFIG_FILE}
270 PKGFS_CONFIG_FILE=$path_fixed
272 if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
273 echo -n "*** ERROR: cannot find configuration file: "
274 echo "'$PKGFS_CONFIG_FILE' ***"
275 exit 1
278 local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
279 PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"
281 for f in ${PKGFS_VARS}; do
282 eval val="\$$f"
283 if [ -z "$val" ]; then
284 echo -n "**** ERROR: '$f' not set in configuration "
285 echo "file, aborting ***"
286 exit 1
289 if [ ! -d "$val" ]; then
290 $mkdir_cmd "$val"
291 if [ "$?" -ne 0 ]; then
292 echo -n "*** ERROR: couldn't create '$f'"
293 echo "directory, aborting ***"
294 exit 1
297 done
301 # Resets all vars used by a template.
303 reset_tmpl_vars()
305 local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
306 make_build_args make_install_args build_style \
307 short_desc maintainer long_desc checksum wrksrc \
308 patch_files configure_env make_cmd pkgconfig_override \
309 configure_env make_env run_stuff_before run_stuff_after \
310 run_stuff_before_configure_file run_stuff_before_build_file \
311 run_stuff_before_install_file run_stuff_after_install \
312 run_stuff_after_install_file make_build_target \
313 run_stuff_before_configure_cmd run_stuff_before_build_cmd \
314 run_stuff_before_install_cmd run_stuff_after_install_cmd \
315 make_install_target postinstall_helpers version \
316 ignore_files"
318 for i in ${TMPL_VARS}; do
319 eval unset "$i"
320 done
322 unset_build_vars
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
414 echo "==> RMD160 checksum ok for \`$pkgname-$version'."
418 # Downloads the distfiles for a template from $url.
420 fetch_tmpl_sources()
422 local file=""
423 local file2=""
425 [ -z "$pkgname" ] && return 1
427 # There's nothing of interest if we are a meta template.
429 [ "$build_style" = "meta-template" ] && return 0
431 if [ -z "$distfiles" ]; then
432 file="$pkgname-$version"
433 else
434 file="$distfiles"
437 for f in "$file"; do
438 file2="$f$extract_sufx"
439 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
440 check_rmd160_cksum $f
441 if [ "$?" -eq 0 ]; then
442 if [ -n "$only_fetch" ]; then
443 echo "=> checksum ok"
444 exit 0
446 return 0
450 echo "==> Fetching \`$file2' source tarball."
452 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
453 if [ "$?" -ne 0 ]; then
454 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
455 echo -n "*** ERROR: couldn't fetch '$file2', "
456 echo "aborting ***"
457 else
458 echo -n "*** ERROR: there was an error "
459 echo "fetching '$file2', aborting ***"
461 exit 1
462 else
463 if [ -n "$only_fetch" ]; then
464 exit 0
467 done
471 # Extracts contents of a distfile specified in a template into
472 # the build directory.
474 extract_tmpl_sources()
476 [ -z "$pkgname" ] && return 1
479 # There's nothing of interest if we are a meta template.
481 [ "$build_style" = "meta-template" ] && return 0
483 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
485 $extract_cmd
486 if [ "$?" -ne 0 ]; then
487 echo -n "*** ERROR: there was an error extracting the "
488 echo "distfile, aborting *** "
489 exit 1
492 unset extract_cmd
493 [ -n "$only_extract" ] && exit 0
496 fixup_tmpl_libtool()
498 local lt_file="$wrksrc/libtool"
501 # If package has a libtool file replace it with ours, so that
502 # we use the master directory while relinking, all will be fine
503 # once the package is stowned.
505 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
506 $rm_cmd -f $wrksrc/libtool
507 $rm_cmd -f $wrksrc/ltmain.sh
508 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
509 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
510 $wrksrc/ltmain.sh
511 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
512 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
516 set_build_vars()
518 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
519 LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
520 CFLAGS="$CFLAGS $PKGFS_CFLAGS"
521 CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
522 CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
523 PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
525 export LDFLAGS="$LDFLAGS" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS"
526 export CPPFLAGS="$CPPFLAGS" PKG_CONFIG="$PKG_CONFIG"
529 unset_build_vars()
531 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
535 # Configures, builds and installs a package into the destination
536 # directory.
538 build_tmpl_sources()
540 local pkg="$pkgname-$version"
542 [ -z "$pkgname" -o -z "$version" ] && return 1
544 # There's nothing of interest if we are a meta template.
546 [ "$build_style" = "meta-template" ] && return 0
548 if [ -n "$distfiles" -a -z "$wrksrc" ]; then
549 wrksrc=$PKGFS_BUILDDIR/$distfiles
550 elif [ -z "$wrksrc" ]; then
551 wrksrc=$PKGFS_BUILDDIR/$pkg
552 else
553 wrksrc=$PKGFS_BUILDDIR/$wrksrc
556 if [ ! -d "$wrksrc" ]; then
557 echo "*** ERROR: unexistent build directory: \`$wrksrc' ***"
558 exit 1
561 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
563 # Apply patches if requested by template file
564 apply_tmpl_patches
566 echo "==> Building \`$pkg' (be patient, may take a while)"
568 # Run stuff before configure.
569 for i in "$run_stuff_before"; do
570 if [ "$i" = "configure" ]; then
571 [ -f $run_stuff_before_configure_file ] && \
572 . $run_stuff_before_configure_file
573 [ -n "$run_stuff_before_configure_cmd" ] && \
574 ${run_stuff_before_configure_cmd}
576 done
578 # Export configure_env vars.
579 for f in ${configure_env}; do
580 export "$f"
581 done
584 # Packages using GNU autoconf
586 if [ "$build_style" = "gnu_configure" ]; then
587 cd $wrksrc
588 set_build_vars
590 # Pass consistent arguments to not have unexpected
591 # surprises later.
593 ./configure \
594 --prefix="$PKGFS_MASTERDIR" \
595 --mandir="$PKGFS_DESTDIR/$pkg/man" \
596 --infodir="$PKGFS_DESTDIR/$pkg/share/info" \
597 --sysconfdir="$PKGFS_SYSCONFDIR" \
598 ${configure_args}
601 # Packages using propietary configure scripts.
603 elif [ "$build_style" = "configure" ]; then
604 cd $wrksrc
605 if [ -n "$configure_script" ]; then
606 ./$configure_script ${configure_args}
607 else
608 ./configure ${configure_args}
611 # Packages that are perl modules and use Makefile.PL files.
612 # They are all handled by the helper perl-module.sh.
614 elif [ "$build_style" = "perl_module" ]; then
615 . $PKGFS_TMPLHELPDIR/perl-module.sh
616 perl_module_build $pkgname
619 # Packages with BSD or GNU Makefiles are easy, just skip
620 # the configure stage and proceed.
622 elif [ "$build_style" = "bsd_makefile" -o \
623 "$build_style" = "gnu_makefile" ]; then
625 cd $wrksrc
627 # Unknown build_style type won't work :-)
629 else
630 echo "*** ERROR unknown build_style \`$build_style' ***"
631 exit 1
634 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
635 echo "*** ERROR building (configure state) \`$pkg' ***"
636 exit 1
639 # unset configure_env vars.
640 for f in ${configure_env}; do
641 unset eval ${f%=*}
642 done
645 # Assume BSD make if make_cmd not set in template.
647 if [ -z "$make_cmd" ]; then
648 make_cmd="/usr/bin/make"
651 # Fixup libtool script if necessary
652 fixup_tmpl_libtool
655 # Run template stuff before building.
657 for i in ${run_stuff_before}; do
658 if [ "$i" = "build" ]; then
659 [ -f $run_stuff_before_build_file ] && \
660 . $run_stuff_before_build_file
661 [ -n "$run_stuff_before_build_cmd" ] && \
662 ${run_stuff_before_build_cmd}
664 done
666 [ -z "$make_build_target" ] && make_build_target=
667 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
669 # Export make_env vars.
670 for f in ${make_env}; do
671 export "$f"
672 done
675 # Build package via make.
677 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
678 if [ "$?" -ne 0 ]; then
679 echo "*** ERROR building (make stage) \`$pkg' ***"
680 exit 1
684 # Run template stuff before installing.
686 for i in ${run_stuff_before}; do
687 if [ "$i" = "install" ]; then
688 [ -f $run_stuff_before_install_file ] && \
689 . $run_stuff_before_install_file
690 [ -n "$run_stuff_before_install_cmd" ] && \
691 ${run_stuff_before_install_cmd}
693 done
695 [ -z "$make_install_target" ] && make_install_target=install
698 # Install package via make.
700 ${make_cmd} ${make_install_args} ${make_install_target} \
701 prefix="$PKGFS_DESTDIR/$pkg"
702 if [ "$?" -ne 0 ]; then
703 echo "*** ERROR instaling \`$pkg' ***"
704 exit 1
707 # Unset make_env vars.
708 for f in ${make_env}; do
709 unset eval ${f%=*}
710 done
713 # Run template stuff after installing.
715 for i in ${run_stuff_after}; do
716 if [ "$i" = "install" ]; then
717 [ -f $run_stuff_after_install_file ] && \
718 . $run_stuff_after_install_file
719 [ -n "$run_stuff_after_install_cmd" ] && \
720 ${run_stuff_after_install_cmd}
722 done
725 # Transform pkg-config files if requested by template.
727 for i in ${pkgconfig_override}; do
728 local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
729 [ -f "$tmpf" ] && \
730 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
731 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
732 pkgconfig_transform_file $tmpf
733 done
735 # Unset build vars.
736 unset_build_vars
738 echo "==> Installed \`$pkg' into $PKGFS_DESTDIR."
741 # Remove $wrksrc if -C not specified.
743 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
744 $rm_cmd -rf $wrksrc
745 [ "$?" -eq 0 ] && \
746 echo "=> Removed \`$pkg' build directory."
749 cd $PKGFS_BUILDDIR
753 # Stows a currently installed package, i.e creates the links
754 # on the master directory.
756 stow_tmpl()
758 local pkg="$1"
759 local infodir_pkg="share/info/dir"
760 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
761 local real_xstowargs="$xstow_args"
762 local real_xstow_ignore="$xstow_ignore_files"
764 [ -z "$pkg" ] && return 2
766 if [ -n "$stow_flag" ]; then
767 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
768 run_file $pkg
769 pkg=$pkgname-$version
771 # You cannot stow a meta-template.
773 [ "$build_style" = "meta-template" ] && return 0
776 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
777 merge_infodir_tmpl $pkg
780 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
781 -a -r "$infodir_master" ]; then
782 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
785 if [ -n "$ignore_files" ]; then
786 xstow_ignore_files="$xstow_ignore_files $ignore_files"
789 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
790 -pd-targets $PKGFS_MASTERDIR \
791 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
792 $PKGFS_DESTDIR/$pkg
793 if [ "$?" -ne 0 ]; then
794 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
795 exit 1
796 else
797 echo "==> Created \`$pkg' symlinks into master directory."
800 installed_tmpl_handler register $pkgname $version
803 # Run template postinstall helpers if requested.
805 if [ "$pkgname" != "${pkg%%-$version}" ]; then
806 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
809 for i in ${postinstall_helpers}; do
810 local pihf="$PKGFS_TMPLHELPDIR/$i"
811 [ -f "$pihf" ] && . $pihf
812 done
814 xstow_ignore_files="$real_xstow_ignore"
815 xstow_args="$real_xstowargs"
819 # Unstows a currently stowned package, i.e removes its links
820 # from the master directory.
822 unstow_tmpl()
824 local pkg="$1"
825 local real_xstow_ignore="$xstow_ignore_files"
827 if [ -z "$pkg" ]; then
828 echo "*** ERROR: template wasn't specified? ***"
829 exit 1
832 if [ "$pkg" = "xstow" ]; then
833 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
834 exit 1
837 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
840 # You cannot unstow a meta-template.
842 [ "$build_style" = "meta-template" ] && return 0
844 if [ -n "$ignore_files" ]; then
845 xstow_ignore_files="$xstow_ignore_files $ignore_files"
848 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
849 -D -i-file-in-dir share/info/dir -ignore \
850 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
851 if [ "$?" -ne 0 ]; then
852 exit 1
853 else
854 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
855 echo "==> Removed \`$pkg' symlinks from master directory."
858 installed_tmpl_handler unregister $pkgname $version
860 xstow_ignore_files="$real_xstow_ignore"
864 # Recursive function that founds dependencies in all required
865 # packages.
867 add_dependency_tolist()
869 local curpkg="$1"
871 [ -z "$curpkg" ] && return 1
872 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
874 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
876 # Check if dep already installed.
878 if [ -r "$PKGFS_REGPKG_DB" ]; then
879 check_installed_tmpl $i ${i##[aA-zZ]*-}
881 # If dep is already installed, put it on the
882 # installed deps list and continue.
884 if [ "$?" -eq 0 ]; then
885 installed_deps_list="$i $installed_deps_list"
886 continue
889 deps_list="$i $deps_list"
890 [ -n "$prev_pkg" ] && unset prev_pkg
892 # Check if dependency needs more deps.
894 check_build_depends_tmpl ${i%-[0-9]*.*}
895 if [ "$?" -eq 0 ]; then
896 add_dependency_tolist $i
897 prev_pkg="$i"
900 done
904 # Removes duplicate deps in the installed or not installed list.
906 find_dupdeps_inlist()
908 local action="$1"
909 local tmp_list=
910 local dup=
912 [ -z "$action" ] && return 1
914 case "$action" in
915 installed)
916 list=$installed_deps_list
918 notinstalled)
919 list=$deps_list
922 return 1
924 esac
926 for f in $list; do
927 if [ -z "$tmp_list" ]; then
928 tmp_list="$f"
929 else
930 for i in $tmp_list; do
931 [ "$f" = "$i" ] && dup=yes
932 done
934 [ -z "$dup" ] && tmp_list="$tmp_list $f"
935 unset dup
937 done
939 case "$action" in
940 installed)
941 installed_deps_list="$tmp_list"
943 notinstalled)
944 deps_list="$tmp_list"
947 return 1
949 esac
953 # Installs all dependencies required by a package.
955 install_dependency_tmpl()
957 local pkg="$1"
958 deps_list=
959 installed_deps_list=
961 [ -z "$pkg" ] && return 1
963 doing_deps=true
965 add_dependency_tolist $pkg
966 find_dupdeps_inlist installed
967 find_dupdeps_inlist notinstalled
969 echo "==> Required dependencies for $(basename $pkg):"
970 for i in ${installed_deps_list}; do
971 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
972 echo " $i: found $fpkg."
973 done
975 for i in ${deps_list}; do
976 echo " $i: not installed."
977 done
979 for i in ${deps_list}; do
980 # skip dup deps
981 echo "=> Installing dependency: $i"
982 install_tmpl ${i%-[0-9]*.*}
983 done
985 unset installed_deps_list
986 unset deps_list
990 # Installs and stows the "xstow" package.
992 install_xstow_tmpl()
994 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
996 reset_tmpl_vars
997 run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
998 check_tmpl_vars $pkgname-$version
999 fetch_tmpl_sources
1000 extract_tmpl_sources
1001 build_tmpl_sources
1002 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
1003 stow_tmpl $pkgname-$version
1005 # Continue with origin package that called us.
1007 run_file $origin_tmpl
1011 # Registers or unregisters a package from the db file.
1013 installed_tmpl_handler()
1015 local action="$1"
1016 local pkg="$2"
1017 local version="$3"
1019 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
1021 if [ "$action" = "register" ]; then
1022 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
1023 if [ "$?" -ne 0 ]; then
1024 echo -n "*** ERROR: couldn't register \`$pkg'"
1025 echo " in db file ***"
1026 exit 1
1028 elif [ "$action" = "unregister" ]; then
1029 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
1030 if [ "$?" -ne 0 ]; then
1031 echo -n "*** ERROR: \`$pkg' not registered "
1032 echo "in db file? ***"
1033 exit 1
1035 else
1036 return 1
1041 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
1042 # the minimal required version is there, or 1 otherwise.
1044 check_installed_tmpl()
1046 local pkg="$1"
1047 local reqver="$2"
1048 local iver=
1050 [ -z "$pkg" -o -z "$reqver" ] && return 1
1052 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1054 reqver="$(echo $reqver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1056 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1057 if [ "$?" -eq 0 ]; then
1059 # Package is installed, let's check the version.
1061 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1062 if [ -n "$iver" ]; then
1064 # As shell only supports decimal arith expressions,
1065 # we simply remove anything except the numbers.
1066 # It's not optimal and may fail, but it is enough
1067 # for now.
1069 iver="$(echo $iver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1070 if [ "$iver" -eq "$reqver" \
1071 -o "$iver" -gt "$reqver" ]; then
1072 return 0
1077 return 1
1081 # Checks the build depends db file and returns 0 if pkg has dependencies,
1082 # otherwise returns 1.
1084 check_build_depends_tmpl()
1086 local pkg="$1"
1088 [ -z $pkg -o ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1090 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1091 return $?
1095 # Installs a pkg by reading its build template file.
1097 install_tmpl()
1099 local pkg=
1100 cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
1101 if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
1102 echo -n "*** ERROR: invalid template file '$cur_tmpl',"
1103 echo " aborting ***"
1104 exit 1
1107 reset_tmpl_vars
1108 run_file $cur_tmpl
1109 pkg="$1-$version"
1112 # If we are the originator package save the path this template in
1113 # other var for future use.
1115 if [ -z "$origin_tmpl" ]; then
1116 origin_tmpl=$path_fixed
1120 # Install xstow if it's not there.
1122 install_xstow_tmpl
1125 # Check vars for current template.
1127 check_tmpl_vars $pkg
1130 # Install dependencies required by this package.
1132 check_build_depends_tmpl $pkg
1133 if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
1134 install_dependency_tmpl $pkg
1136 # At this point all required deps are installed, and
1137 # only remaining is the origin template; install it.
1139 unset doing_deps
1140 reset_tmpl_vars
1141 run_file $origin_tmpl
1142 check_tmpl_vars $pkgname-$version
1145 if [ -n "$only_build" ]; then
1146 build_tmpl_sources
1147 exit 0
1151 # Fetch, extract, stow and reset vars for a template.
1153 fetch_tmpl_sources
1154 extract_tmpl_sources
1155 build_tmpl_sources
1158 # Just announce that meta-template is installed and exit.
1160 if [ "$build_style" = "meta-template" ]; then
1161 installed_tmpl_handler register $pkgname $version
1162 echo "==> Installed meta-template \`$pkg'."
1163 return 0
1167 # Do not stow the pkg if requested.
1169 [ -z "$only_install" ] && stow_tmpl $pkg
1173 # Lists all currently installed packages.
1175 list_tmpls()
1177 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1178 echo "=> No packages registered or missing register db file."
1179 exit 0
1182 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1183 # Run file to get short_desc and print something useful
1184 run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
1185 echo "$i-$version $short_desc"
1186 reset_tmpl_vars
1187 done
1191 # Removes a currently installed package (unstow + removed from destdir).
1193 remove_tmpl()
1195 local pkg="$1"
1197 if [ -z "$pkg" ]; then
1198 echo "*** ERROR: unexistent package, aborting ***"
1199 exit 1
1202 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1203 echo "*** ERROR: cannot find template file ***"
1204 exit 1
1207 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1210 # If it's a meta-template, just unregister it from the db.
1212 if [ "$build_style" = "meta-template" ]; then
1213 installed_tmpl_handler unregister $pkgname $version
1214 [ "$?" -eq 0 ] && \
1215 echo "=> Removed meta-template \`$pkg'."
1216 return $?
1219 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1220 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1221 exit 1
1224 unstow_tmpl $pkg
1225 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1226 return "$?"
1230 # main()
1232 args=$(getopt bCc:efi $*)
1233 [ "$?" -ne 0 ] && usage
1235 set -- $args
1236 while [ "$#" -gt 0 ]; do
1237 case "$1" in
1239 only_build=yes
1242 dontrm_builddir=yes
1245 config_file_specified=yes
1246 PKGFS_CONFIG_FILE="$2"
1247 shift
1250 only_extract=yes
1253 only_fetch=yes
1256 only_install=yes
1259 shift
1260 break
1262 esac
1263 shift
1264 done
1266 [ "$#" -gt 2 ] && usage
1268 target="$1"
1269 if [ -z "$target" ]; then
1270 echo "*** ERROR missing target ***"
1271 usage
1275 # Check configuration vars before anyting else, and set defaults vars.
1277 check_config_vars
1278 set_defvars
1280 # Main switch
1281 case "$target" in
1282 info)
1283 info_tmpl "$2"
1285 install)
1286 install_tmpl "$2"
1288 list)
1289 list_tmpls
1291 remove)
1292 remove_tmpl "$2"
1294 stow)
1295 stow_flag=yes
1296 stow_tmpl "$2"
1298 unstow)
1299 unstow_tmpl "$2"
1302 echo "*** ERROR invalid target \`$target' ***"
1303 usage
1304 esac
1306 # Agur
1307 exit 0