Added xauth-1.0.3 and xinit-1.1.0 templates.
[pkgfs.git] / pkgfs.sh
blob5033dd8a4b7a7a9366ae7a97e7e82df9366e996e
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
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 && cffound=yes
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 "$f" ]; 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 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 REQ_VARS="pkgname version extract_sufx url build_style"
336 # Check if required vars weren't set.
337 for i in ${REQ_VARS}; do
338 eval val="\$$i"
339 if [ -z "$val" -o -z "$i" ]; then
340 echo -n "*** ERROR: $i not set (incomplete template"
341 echo " build file), aborting ***"
342 exit 1
344 done
346 if [ -z "$distfiles" ]; then
347 dfile="$pkgname-$version$extract_sufx"
348 elif [ -n "${distfiles}" ]; then
349 dfile="$distfiles$extract_sufx"
350 else
351 echo "*** ERROR unsupported fetch state ***"
352 exit 1
355 dfile="$PKGFS_SRCDISTDIR/$dfile"
357 case "$extract_sufx" in
358 .tar.bz2|.tar.gz|.tgz|.tbz)
359 extract_cmd="$tar_cmd xfz $dfile -C $PKGFS_BUILDDIR"
361 .tar)
362 extract_cmd="$tar_cmd xf $dfile -C $PKGFS_BUILDDIR"
364 .zip)
365 if [ -f "$PKGFS_TMPLHELPDIR/unzip-extraction.sh" ]; then
366 . $PKGFS_TMPLHELPDIR/unzip-extraction.sh
368 # $extract_cmd set by the helper
371 echo -n "*** ERROR: unknown 'extract_sufx' argument in build "
372 echo "file ***"
373 exit 1
375 esac
379 # Verifies that a checksum of a distfile is correct.
381 check_rmd160_cksum()
383 local file="$1"
384 local dfile=
386 [ -z "$file" ] && return 1
388 if [ -z "${distfiles}" ]; then
389 dfile="$pkgname-$version$extract_sufx"
390 elif [ -n "${distfiles}" ]; then
391 dfile="$distfiles$extract_sufx"
392 else
393 dfile="$file$extract_sufx"
396 if [ -z "$checksum" ]; then
397 echo "*** ERROR: checksum unset in template file for \`$pkgname' ***"
398 exit 1
401 origsum="$checksum"
402 dfile="$PKGFS_SRCDISTDIR/$dfile"
403 filesum="$($cksum_cmd $dfile | $awk_cmd '{print $4}')"
404 if [ "$origsum" != "$filesum" ]; then
405 echo "*** WARNING: checksum doesn't match (rmd160) ***"
406 return 1
411 # Downloads the distfiles for a template from $url.
413 fetch_tmpl_sources()
415 local file=""
416 local file2=""
418 [ -z "$pkgname" ] && return 1
420 if [ -z "$distfiles" ]; then
421 file="$pkgname-$version"
422 else
423 file="$distfiles"
426 for f in "$file"; do
427 file2="$f$extract_sufx"
428 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
429 check_rmd160_cksum $f
430 if [ "$?" -eq 0 ]; then
431 if [ -n "$only_fetch" ]; then
432 echo "=> checksum ok"
433 exit 0
435 return 0
439 echo "==> Fetching \`$file2' source tarball"
441 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
442 if [ "$?" -ne 0 ]; then
443 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
444 echo -n "*** ERROR: couldn't fetch '$file2', "
445 echo "aborting ***"
446 else
447 echo -n "*** ERROR: there was an error "
448 echo "fetching '$file2', aborting ***"
450 exit 1
451 else
452 if [ -n "$only_fetch" ]; then
453 exit 0
456 done
460 # Extracts contents of a distfile specified in a template into
461 # the build directory.
463 extract_tmpl_sources()
465 [ -z "$pkgname" ] && return 1
467 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
469 $extract_cmd
470 if [ "$?" -ne 0 ]; then
471 echo -n "*** ERROR: there was an error extracting the "
472 echo "distfile, aborting *** "
473 exit 1
476 unset extract_cmd
477 [ -n "$only_extract" ] && exit 0
480 fixup_tmpl_libtool()
482 local lt_file="$wrksrc/libtool"
485 # If package has a libtool file replace it with ours, so that
486 # we use the master directory while relinking, all will be fine
487 # once the package is stowned.
489 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
490 $rm_cmd -f $wrksrc/libtool
491 $rm_cmd -f $wrksrc/ltmain.sh
492 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
493 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
494 $wrksrc/ltmain.sh
495 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
496 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
501 # Configures, builds and installs a package into the destination
502 # directory.
504 build_tmpl_sources()
506 local pkg="$pkgname-$version"
508 [ -z "$pkgname" -o -z "$version" ] && return 1
510 if [ -n "$distfiles" -a -z "$wrksrc" ]; then
511 wrksrc=$PKGFS_BUILDDIR/$distfiles
512 elif [ -z "$wrksrc" ]; then
513 wrksrc=$PKGFS_BUILDDIR/$pkg
514 else
515 wrksrc=$PKGFS_BUILDDIR/$wrksrc
518 if [ ! -d "$wrksrc" ]; then
519 echo "*** ERROR: unexistent build directory, aborting ***"
520 exit 1
523 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
525 # Apply patches if requested by template file
526 apply_tmpl_patches
528 echo "==> Building \`$pkg' (be patient, may take a while)"
531 # For now, just set them through the environment.
533 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
534 export LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
535 export CFLAGS="$CFLAGS $PKGFS_CFLAGS"
536 export CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
537 export CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
538 export PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
540 # Run stuff before configure.
541 for i in "$run_stuff_before"; do
542 if [ "$i" = "configure" ]; then
543 [ -f $run_stuff_before_configure_file ] && \
544 . $run_stuff_before_configure_file
545 [ -n "$run_stuff_before_configure_cmd" ] && \
546 ${run_stuff_before_configure_cmd}
548 done
551 # Packages using GNU autoconf
553 if [ "$build_style" = "gnu_configure" ]; then
554 for i in ${configure_env}; do
555 [ -n "$i" ] && export $i
556 done
557 cd $wrksrc
559 # Pass consistent arguments to not have unexpected
560 # surprises later.
562 ./configure --prefix="$PKGFS_MASTERDIR" \
563 --mandir="$PKGFS_DESTDIR/$pkg/man" \
564 --infodir="$PKGFS_DESTDIR/$pkg/share/info" \
565 --sysconfdir="$PKGFS_SYSCONFDIR" \
566 ${configure_args}
569 # Packages using propietary configure scripts.
571 elif [ "$build_style" = "configure" ]; then
572 cd $wrksrc
573 if [ -n "$configure_script" ]; then
574 ./$configure_script ${configure_args}
575 else
576 ./configure ${configure_args}
579 # Packages that are perl modules and use Makefile.PL files.
580 # They are all handled by the helper perl-module.sh.
582 elif [ "$build_style" = "perl_module" ]; then
583 . $PKGFS_TMPLHELPDIR/perl-module.sh
584 perl_module_build $pkgname
587 # Packages with BSD or GNU Makefiles are easy, just skip
588 # the configure stage and proceed.
590 elif [ "$build_style" = "bsd_makefile" -o \
591 "$build_style" = "gnu_makefile" ]; then
593 cd $wrksrc
595 # Unknown build_style type won't work :-)
597 else
598 echo "*** ERROR unknown build_style $build_style, aborting ***"
599 exit 1
602 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
603 echo "*** ERROR building (configure state) \`$pkg' ***"
604 exit 1
608 # Assume BSD make if make_cmd not set in template.
610 if [ -z "$make_cmd" ]; then
611 make_cmd="/usr/bin/make"
614 # Fixup libtool script if necessary
615 fixup_tmpl_libtool
618 # Run template stuff before building.
620 for i in ${run_stuff_before}; do
621 if [ "$i" = "build" ]; then
622 [ -f $run_stuff_before_build_file ] && \
623 . $run_stuff_before_build_file
624 [ -n "$run_stuff_before_build_cmd" ] && \
625 ${run_stuff_before_build_cmd}
627 done
629 [ -z "$make_build_target" ] && make_build_target=
632 # Build package via make.
634 ${make_cmd} ${make_build_args} ${make_build_target}
635 if [ "$?" -ne 0 ]; then
636 echo "*** ERROR building (make stage) \`$pkg' ***"
637 exit 1
641 # Run template stuff before installing.
643 for i in ${run_stuff_before}; do
644 if [ "$i" = "install" ]; then
645 [ -f $run_stuff_before_install_file ] && \
646 . $run_stuff_before_install_file
647 [ -n "$run_stuff_before_install_cmd" ] && \
648 ${run_stuff_before_install_cmd}
650 done
652 [ -z "$make_install_target" ] && make_install_target=install
655 # Install package via make.
657 ${make_cmd} ${make_install_args} ${make_install_target} \
658 prefix="$PKGFS_DESTDIR/$pkg"
659 if [ "$?" -ne 0 ]; then
660 echo "*** ERROR instaling \`$pkg' ***"
661 exit 1
665 # Run template stuff after installing.
667 for i in ${run_stuff_after}; do
668 if [ "$i" = "install" ]; then
669 [ -f $run_stuff_after_install_file ] && \
670 . $run_stuff_after_install_file
671 [ -n "$run_stuff_after_install_cmd" ] && \
672 ${run_stuff_after_install_cmd}
674 done
677 # Transform pkg-config files if requested by template.
679 for i in ${pkgconfig_override}; do
680 local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
681 [ -f "$tmpf" ] && \
682 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
683 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
684 pkgconfig_transform_file $tmpf
685 done
688 echo "==> Installed \`$pkg' into $PKGFS_DESTDIR/$pkg."
691 # Once all work has been done, unset compilation vars.
693 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
696 # Remove $wrksrc if -C not specified.
698 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
699 $rm_cmd -rf $wrksrc
700 [ "$?" -eq 0 ] && \
701 echo "=> Removed \`$pkg' build directory."
704 cd $PKGFS_BUILDDIR
708 # Stows a currently installed package, i.e creates the links
709 # on the master directory.
711 stow_tmpl()
713 local pkg="$1"
714 local infodir_pkg="share/info/dir"
715 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
716 local real_xstowargs="$xstow_args"
717 local real_xstow_ignore="$xstow_ignore_files"
719 [ -z "$pkg" ] && return 2
721 if [ -n "$stow_flag" ]; then
722 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
723 run_file $pkg
724 pkg=$pkgname-$version
727 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
728 merge_infodir_tmpl $pkg
731 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
732 -a -r "$infodir_master" ]; then
733 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
736 if [ -n "$ignore_files" ]; then
737 xstow_ignore_files="$xstow_ignore_files $ignore_files"
740 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
741 -pd-targets $PKGFS_MASTERDIR \
742 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
743 $PKGFS_DESTDIR/$pkg
744 if [ "$?" -ne 0 ]; then
745 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
746 exit 1
747 else
748 echo "==> Created \`$pkg' symlinks into master directory."
751 installed_tmpl_handler register $pkgname $version
754 # Run template postinstall helpers if requested.
756 if [ "$pkgname" != "${pkg%%-$version}" ]; then
757 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
760 for i in ${postinstall_helpers}; do
761 local pihf="$PKGFS_TMPLHELPDIR/$i"
762 [ -f "$pihf" ] && . $pihf
763 done
765 xstow_ignore_files="$real_xstow_ignore"
766 xstow_args="$real_xstowargs"
770 # Unstows a currently stowned package, i.e removes its links
771 # from the master directory.
773 unstow_tmpl()
775 local pkg="$1"
776 local real_xstow_ignore="$xstow_ignore_files"
778 if [ -z "$pkg" ]; then
779 echo "*** ERROR: template wasn't specified? ***"
780 exit 1
783 if [ "$pkg" = "xstow" ]; then
784 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
785 exit 1
788 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
790 if [ -n "$ignore_files" ]; then
791 xstow_ignore_files="$xstow_ignore_files $ignore_files"
794 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
795 -D -i-file-in-dir share/info/dir -ignore \
796 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
797 if [ "$?" -ne 0 ]; then
798 exit 1
799 else
800 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
801 echo "==> Removed \`$pkg' symlinks from master directory."
804 installed_tmpl_handler unregister $pkgname $version
806 xstow_ignore_files="$real_xstow_ignore"
810 # Recursive function that founds dependencies in all required
811 # packages.
813 add_dependency_tolist()
815 local curpkg="$1"
817 [ -z "$curpkg" ] && return 1
818 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
820 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*}); do
822 # Check if dep already installed.
824 if [ -r "$PKGFS_REGPKG_DB" ]; then
825 check_installed_tmpl $i ${i##[aA-zZ]*-}
827 # If dep is already installed, put it on the
828 # installed deps list and continue.
830 if [ "$?" -eq 0 ]; then
831 installed_deps_list="$i $installed_deps_list"
832 continue
835 deps_list="$i $deps_list"
836 [ -n "$prev_pkg" ] && unset prev_pkg
838 # Check if dependency needs more deps.
840 check_build_depends_tmpl $i
841 if [ "$?" -eq 0 ]; then
842 add_dependency_tolist $i
843 prev_pkg="$i"
846 done
850 # Removes duplicate deps in the installed or not installed list.
852 find_dupdeps_inlist()
854 local action="$1"
855 local tmp_list=
856 local dup=
858 [ -z "$action" ] && return 1
860 case "$action" in
861 installed)
862 list=$installed_deps_list
864 notinstalled)
865 list=$deps_list
868 return 1
870 esac
872 for f in $list; do
873 if [ -z "$tmp_list" ]; then
874 tmp_list="$f"
875 else
876 for i in $tmp_list; do
877 [ "$f" = "$i" ] && dup=yes
878 done
880 [ -z "$dup" ] && tmp_list="$tmp_list $f"
881 unset dup
883 done
885 case "$action" in
886 installed)
887 installed_deps_list="$tmp_list"
889 notinstalled)
890 deps_list="$tmp_list"
893 return 1
895 esac
899 # Installs all dependencies required by a package.
901 install_dependency_tmpl()
903 local pkg="$1"
904 deps_list=
905 installed_deps_list=
907 [ -z "$pkg" ] && return 1
909 doing_deps=true
911 add_dependency_tolist $pkg
912 find_dupdeps_inlist installed
913 find_dupdeps_inlist notinstalled
915 echo "==> Required dependencies for $(basename $pkg):"
916 for i in ${installed_deps_list}; do
917 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*})"
918 echo " $i: found $fpkg."
919 done
921 for i in ${deps_list}; do
922 echo " $i: not installed."
923 done
925 for i in ${deps_list}; do
926 # skip dup deps
927 echo "=> Installing dependency: $i"
928 install_tmpl ${i%-[0-9]*}
929 done
931 unset installed_deps_list
932 unset deps_list
936 # Installs and stows the "xstow" package.
938 install_xstow_tmpl()
940 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
942 reset_tmpl_vars
943 run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
944 check_tmpl_vars $pkgname-$version
945 fetch_tmpl_sources
946 extract_tmpl_sources
947 build_tmpl_sources
948 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
949 stow_tmpl $pkgname-$version
951 # Continue with origin package that called us.
953 run_file $origin_tmpl
957 # Registers or unregisters a package from the db file.
959 installed_tmpl_handler()
961 local action="$1"
962 local pkg="$2"
963 local version="$3"
965 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
966 if [ "$action" = "register" ]; then
967 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version
968 if [ "$?" -ne 0 ]; then
969 echo -n "*** ERROR: couldn't register stowned \`$pkg'"
970 echo " in db file ***"
971 exit 1
973 elif [ "$action" = "unregister" ]; then
974 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg
975 if [ "$?" -ne 0 ]; then
976 echo -n "*** ERROR: \`$pkg' stowned not registered "
977 echo "in db file? ***"
978 exit 1
980 else
981 return 1
986 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
987 # the minimal required version if there, or 1 otherwise.
989 check_installed_tmpl()
991 local pkg="$1"
992 local reqver="$2"
993 local iver=
995 [ -z "$pkg" -o -z "$reqver" ] && return 1
997 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*}.tmpl
999 reqver="$(echo $reqver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1001 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1002 if [ "$?" -eq 0 ]; then
1004 # Package is installed, let's check the version.
1006 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1007 if [ -n "$iver" ]; then
1009 # As shell only supports decimal arith expressions,
1010 # we simply remove anything except the numbers.
1011 # It's not optimal and may fail, but it is enough
1012 # for now.
1014 iver="$(echo $iver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1015 if [ "$iver" -eq "$reqver" \
1016 -o "$iver" -gt "$reqver" ]; then
1017 return 0
1022 return 1
1026 # Checks the build depends db file and returns 0 if pkg has dependencies,
1027 # otherwise returns 1.
1029 check_build_depends_tmpl()
1031 local pkg="$1"
1033 [ -z $pkg ] && return 1
1034 [ ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1036 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*} 2>&1 >/dev/null
1037 return $?
1041 # Installs a pkg by reading its build template file.
1043 install_tmpl()
1045 local pkg=
1046 cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
1047 if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
1048 echo -n "*** ERROR: invalid template file '$cur_tmpl',"
1049 echo " aborting ***"
1050 exit 1
1053 reset_tmpl_vars
1054 run_file $cur_tmpl
1055 pkg="$1-$version"
1058 # If we are the originator package save the path this template in
1059 # other var for future use.
1061 if [ -z "$origin_tmpl" ]; then
1062 origin_tmpl=$path_fixed
1066 # Install xstow if it's not there.
1068 install_xstow_tmpl
1071 # Check vars for current template.
1073 check_tmpl_vars $pkg
1076 # Install dependencies required by this package.
1078 check_build_depends_tmpl $pkg
1079 if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
1080 install_dependency_tmpl $pkg
1082 # At this point all required deps are installed, and
1083 # only remaining is the origin template; install it.
1085 unset doing_deps
1086 reset_tmpl_vars
1087 run_file $origin_tmpl
1088 check_tmpl_vars $pkgname-$version
1091 if [ -n "$only_build" ]; then
1092 build_tmpl_sources
1093 exit 0
1097 # Fetch, extract, stow and reset vars for a template.
1099 fetch_tmpl_sources
1100 extract_tmpl_sources
1101 build_tmpl_sources
1104 # Do not stow the pkg if requested.
1106 [ -z "$only_install" ] && stow_tmpl $pkg
1110 # Lists all currently installed packages.
1112 list_tmpls()
1114 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1115 echo "=> No packages registered or missing register db file."
1116 exit 0
1119 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1120 # Run file to get short_desc and print something useful
1121 run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
1122 echo "$i-$version $short_desc"
1123 reset_tmpl_vars
1124 done
1128 # Removes a currently installed package (unstow + removed from destdir).
1130 remove_tmpl()
1132 local pkg="$1"
1134 if [ -z "$pkg" ]; then
1135 echo "*** ERROR: unexistent package, aborting ***"
1136 exit 1
1139 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1140 echo "*** ERROR: cannot find template file ***"
1141 exit 1
1144 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1146 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1147 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1148 exit 1
1151 unstow_tmpl $pkg
1152 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1153 return "$?"
1157 # main()
1159 args=$(getopt bCc:efi $*)
1160 [ "$?" -ne 0 ] && usage
1162 set -- $args
1163 while [ "$#" -gt 0 ]; do
1164 case "$1" in
1166 only_build=yes
1169 dontrm_builddir=yes
1172 config_file_specified=yes
1173 PKGFS_CONFIG_FILE="$2"
1174 shift
1177 only_extract=yes
1180 only_fetch=yes
1183 only_install=yes
1186 shift
1187 break
1189 esac
1190 shift
1191 done
1193 [ "$#" -gt 2 ] && usage
1195 target="$1"
1196 if [ -z "$target" ]; then
1197 echo "*** ERROR missing target ***"
1198 usage
1202 # Check configuration vars before anyting else, and set defaults vars.
1204 check_config_vars
1205 set_defvars
1207 # Main switch
1208 case "$target" in
1209 info)
1210 info_tmpl "$2"
1212 install)
1213 install_tmpl "$2"
1215 list)
1216 list_tmpls
1218 remove)
1219 remove_tmpl "$2"
1221 stow)
1222 stow_flag=yes
1223 stow_tmpl "$2"
1225 unstow)
1226 unstow_tmpl "$2"
1229 echo "*** ERROR invalid target '$target' ***"
1230 usage
1231 esac
1233 # Agur
1234 exit 0