Added xf86-input-vmmouse-12.5.1 template.
[pkgfs.git] / pkgfs.sh
bloba8240f963ae9b9706c3b8dd00baa1e1710019614
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 && 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 # 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
415 # Downloads the distfiles for a template from $url.
417 fetch_tmpl_sources()
419 local file=""
420 local file2=""
422 [ -z "$pkgname" ] && return 1
424 # There's nothing of interest if we are a meta template.
426 [ "$build_style" = "meta-template" ] && return 0
428 if [ -z "$distfiles" ]; then
429 file="$pkgname-$version"
430 else
431 file="$distfiles"
434 for f in "$file"; do
435 file2="$f$extract_sufx"
436 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
437 check_rmd160_cksum $f
438 if [ "$?" -eq 0 ]; then
439 if [ -n "$only_fetch" ]; then
440 echo "=> checksum ok"
441 exit 0
443 return 0
447 echo "==> Fetching \`$file2' source tarball"
449 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
450 if [ "$?" -ne 0 ]; then
451 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
452 echo -n "*** ERROR: couldn't fetch '$file2', "
453 echo "aborting ***"
454 else
455 echo -n "*** ERROR: there was an error "
456 echo "fetching '$file2', aborting ***"
458 exit 1
459 else
460 if [ -n "$only_fetch" ]; then
461 exit 0
464 done
468 # Extracts contents of a distfile specified in a template into
469 # the build directory.
471 extract_tmpl_sources()
473 [ -z "$pkgname" ] && return 1
476 # There's nothing of interest if we are a meta template.
478 [ "$build_style" = "meta-template" ] && return 0
480 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
482 $extract_cmd
483 if [ "$?" -ne 0 ]; then
484 echo -n "*** ERROR: there was an error extracting the "
485 echo "distfile, aborting *** "
486 exit 1
489 unset extract_cmd
490 [ -n "$only_extract" ] && exit 0
493 fixup_tmpl_libtool()
495 local lt_file="$wrksrc/libtool"
498 # If package has a libtool file replace it with ours, so that
499 # we use the master directory while relinking, all will be fine
500 # once the package is stowned.
502 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
503 $rm_cmd -f $wrksrc/libtool
504 $rm_cmd -f $wrksrc/ltmain.sh
505 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
506 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
507 $wrksrc/ltmain.sh
508 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
509 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
514 # Configures, builds and installs a package into the destination
515 # directory.
517 build_tmpl_sources()
519 local pkg="$pkgname-$version"
521 [ -z "$pkgname" -o -z "$version" ] && return 1
523 # There's nothing of interest if we are a meta template.
525 [ "$build_style" = "meta-template" ] && return 0
527 if [ -n "$distfiles" -a -z "$wrksrc" ]; then
528 wrksrc=$PKGFS_BUILDDIR/$distfiles
529 elif [ -z "$wrksrc" ]; then
530 wrksrc=$PKGFS_BUILDDIR/$pkg
531 else
532 wrksrc=$PKGFS_BUILDDIR/$wrksrc
535 if [ ! -d "$wrksrc" ]; then
536 echo "*** ERROR: unexistent build directory, aborting ***"
537 exit 1
540 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
542 # Apply patches if requested by template file
543 apply_tmpl_patches
545 echo "==> Building \`$pkg' (be patient, may take a while)"
548 # For now, just set them through the environment.
550 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
551 export LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
552 export CFLAGS="$CFLAGS $PKGFS_CFLAGS"
553 export CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
554 export CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
555 export PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
557 # Run stuff before configure.
558 for i in "$run_stuff_before"; do
559 if [ "$i" = "configure" ]; then
560 [ -f $run_stuff_before_configure_file ] && \
561 . $run_stuff_before_configure_file
562 [ -n "$run_stuff_before_configure_cmd" ] && \
563 ${run_stuff_before_configure_cmd}
565 done
568 # Packages using GNU autoconf
570 if [ "$build_style" = "gnu_configure" ]; then
571 for i in ${configure_env}; do
572 [ -n "$i" ] && export $i
573 done
574 cd $wrksrc
576 # Pass consistent arguments to not have unexpected
577 # surprises later.
579 ./configure --prefix="$PKGFS_MASTERDIR" \
580 --mandir="$PKGFS_DESTDIR/$pkg/man" \
581 --infodir="$PKGFS_DESTDIR/$pkg/share/info" \
582 --sysconfdir="$PKGFS_SYSCONFDIR" \
583 ${configure_args}
586 # Packages using propietary configure scripts.
588 elif [ "$build_style" = "configure" ]; then
589 cd $wrksrc
590 if [ -n "$configure_script" ]; then
591 ./$configure_script ${configure_args}
592 else
593 ./configure ${configure_args}
596 # Packages that are perl modules and use Makefile.PL files.
597 # They are all handled by the helper perl-module.sh.
599 elif [ "$build_style" = "perl_module" ]; then
600 . $PKGFS_TMPLHELPDIR/perl-module.sh
601 perl_module_build $pkgname
604 # Packages with BSD or GNU Makefiles are easy, just skip
605 # the configure stage and proceed.
607 elif [ "$build_style" = "bsd_makefile" -o \
608 "$build_style" = "gnu_makefile" ]; then
610 cd $wrksrc
612 # Unknown build_style type won't work :-)
614 else
615 echo "*** ERROR unknown build_style $build_style, aborting ***"
616 exit 1
619 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
620 echo "*** ERROR building (configure state) \`$pkg' ***"
621 exit 1
625 # Assume BSD make if make_cmd not set in template.
627 if [ -z "$make_cmd" ]; then
628 make_cmd="/usr/bin/make"
631 # Fixup libtool script if necessary
632 fixup_tmpl_libtool
635 # Run template stuff before building.
637 for i in ${run_stuff_before}; do
638 if [ "$i" = "build" ]; then
639 [ -f $run_stuff_before_build_file ] && \
640 . $run_stuff_before_build_file
641 [ -n "$run_stuff_before_build_cmd" ] && \
642 ${run_stuff_before_build_cmd}
644 done
646 [ -z "$make_build_target" ] && make_build_target=
647 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
649 # Build package via make.
651 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
652 if [ "$?" -ne 0 ]; then
653 echo "*** ERROR building (make stage) \`$pkg' ***"
654 exit 1
658 # Run template stuff before installing.
660 for i in ${run_stuff_before}; do
661 if [ "$i" = "install" ]; then
662 [ -f $run_stuff_before_install_file ] && \
663 . $run_stuff_before_install_file
664 [ -n "$run_stuff_before_install_cmd" ] && \
665 ${run_stuff_before_install_cmd}
667 done
669 [ -z "$make_install_target" ] && make_install_target=install
672 # Install package via make.
674 ${make_cmd} ${make_install_args} ${make_install_target} \
675 prefix="$PKGFS_DESTDIR/$pkg"
676 if [ "$?" -ne 0 ]; then
677 echo "*** ERROR instaling \`$pkg' ***"
678 exit 1
682 # Run template stuff after installing.
684 for i in ${run_stuff_after}; do
685 if [ "$i" = "install" ]; then
686 [ -f $run_stuff_after_install_file ] && \
687 . $run_stuff_after_install_file
688 [ -n "$run_stuff_after_install_cmd" ] && \
689 ${run_stuff_after_install_cmd}
691 done
694 # Transform pkg-config files if requested by template.
696 for i in ${pkgconfig_override}; do
697 local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
698 [ -f "$tmpf" ] && \
699 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
700 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
701 pkgconfig_transform_file $tmpf
702 done
705 echo "==> Installed \`$pkg' into $PKGFS_DESTDIR/$pkg."
708 # Once all work has been done, unset compilation vars.
710 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
713 # Remove $wrksrc if -C not specified.
715 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
716 $rm_cmd -rf $wrksrc
717 [ "$?" -eq 0 ] && \
718 echo "=> Removed \`$pkg' build directory."
721 cd $PKGFS_BUILDDIR
725 # Stows a currently installed package, i.e creates the links
726 # on the master directory.
728 stow_tmpl()
730 local pkg="$1"
731 local infodir_pkg="share/info/dir"
732 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
733 local real_xstowargs="$xstow_args"
734 local real_xstow_ignore="$xstow_ignore_files"
736 [ -z "$pkg" ] && return 2
738 if [ -n "$stow_flag" ]; then
739 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
740 run_file $pkg
741 pkg=$pkgname-$version
743 # You cannot stow a meta-template.
745 [ "$build_style" = "meta-template" ] && return 0
748 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
749 merge_infodir_tmpl $pkg
752 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
753 -a -r "$infodir_master" ]; then
754 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
757 if [ -n "$ignore_files" ]; then
758 xstow_ignore_files="$xstow_ignore_files $ignore_files"
761 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
762 -pd-targets $PKGFS_MASTERDIR \
763 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
764 $PKGFS_DESTDIR/$pkg
765 if [ "$?" -ne 0 ]; then
766 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
767 exit 1
768 else
769 echo "==> Created \`$pkg' symlinks into master directory."
772 installed_tmpl_handler register $pkgname $version
775 # Run template postinstall helpers if requested.
777 if [ "$pkgname" != "${pkg%%-$version}" ]; then
778 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
781 for i in ${postinstall_helpers}; do
782 local pihf="$PKGFS_TMPLHELPDIR/$i"
783 [ -f "$pihf" ] && . $pihf
784 done
786 xstow_ignore_files="$real_xstow_ignore"
787 xstow_args="$real_xstowargs"
791 # Unstows a currently stowned package, i.e removes its links
792 # from the master directory.
794 unstow_tmpl()
796 local pkg="$1"
797 local real_xstow_ignore="$xstow_ignore_files"
799 if [ -z "$pkg" ]; then
800 echo "*** ERROR: template wasn't specified? ***"
801 exit 1
804 if [ "$pkg" = "xstow" ]; then
805 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
806 exit 1
809 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
812 # You cannot unstow a meta-template.
814 [ "$build_style" = "meta-template" ] && return 0
816 if [ -n "$ignore_files" ]; then
817 xstow_ignore_files="$xstow_ignore_files $ignore_files"
820 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
821 -D -i-file-in-dir share/info/dir -ignore \
822 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
823 if [ "$?" -ne 0 ]; then
824 exit 1
825 else
826 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
827 echo "==> Removed \`$pkg' symlinks from master directory."
830 installed_tmpl_handler unregister $pkgname $version
832 xstow_ignore_files="$real_xstow_ignore"
836 # Recursive function that founds dependencies in all required
837 # packages.
839 add_dependency_tolist()
841 local curpkg="$1"
843 [ -z "$curpkg" ] && return 1
844 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
846 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
848 # Check if dep already installed.
850 if [ -r "$PKGFS_REGPKG_DB" ]; then
851 check_installed_tmpl $i ${i##[aA-zZ]*-}
853 # If dep is already installed, put it on the
854 # installed deps list and continue.
856 if [ "$?" -eq 0 ]; then
857 installed_deps_list="$i $installed_deps_list"
858 continue
861 deps_list="$i $deps_list"
862 [ -n "$prev_pkg" ] && unset prev_pkg
864 # Check if dependency needs more deps.
866 check_build_depends_tmpl ${i%-[0-9]*.*}
867 if [ "$?" -eq 0 ]; then
868 add_dependency_tolist $i
869 prev_pkg="$i"
872 done
876 # Removes duplicate deps in the installed or not installed list.
878 find_dupdeps_inlist()
880 local action="$1"
881 local tmp_list=
882 local dup=
884 [ -z "$action" ] && return 1
886 case "$action" in
887 installed)
888 list=$installed_deps_list
890 notinstalled)
891 list=$deps_list
894 return 1
896 esac
898 for f in $list; do
899 if [ -z "$tmp_list" ]; then
900 tmp_list="$f"
901 else
902 for i in $tmp_list; do
903 [ "$f" = "$i" ] && dup=yes
904 done
906 [ -z "$dup" ] && tmp_list="$tmp_list $f"
907 unset dup
909 done
911 case "$action" in
912 installed)
913 installed_deps_list="$tmp_list"
915 notinstalled)
916 deps_list="$tmp_list"
919 return 1
921 esac
925 # Installs all dependencies required by a package.
927 install_dependency_tmpl()
929 local pkg="$1"
930 deps_list=
931 installed_deps_list=
933 [ -z "$pkg" ] && return 1
935 doing_deps=true
937 add_dependency_tolist $pkg
938 find_dupdeps_inlist installed
939 find_dupdeps_inlist notinstalled
941 echo "==> Required dependencies for $(basename $pkg):"
942 for i in ${installed_deps_list}; do
943 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
944 echo " $i: found $fpkg."
945 done
947 for i in ${deps_list}; do
948 echo " $i: not installed."
949 done
951 for i in ${deps_list}; do
952 # skip dup deps
953 echo "=> Installing dependency: $i"
954 install_tmpl ${i%-[0-9].*}
955 done
957 unset installed_deps_list
958 unset deps_list
962 # Installs and stows the "xstow" package.
964 install_xstow_tmpl()
966 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
968 reset_tmpl_vars
969 run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
970 check_tmpl_vars $pkgname-$version
971 fetch_tmpl_sources
972 extract_tmpl_sources
973 build_tmpl_sources
974 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
975 stow_tmpl $pkgname-$version
977 # Continue with origin package that called us.
979 run_file $origin_tmpl
983 # Registers or unregisters a package from the db file.
985 installed_tmpl_handler()
987 local action="$1"
988 local pkg="$2"
989 local version="$3"
991 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
992 if [ "$action" = "register" ]; then
993 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
994 if [ "$?" -ne 0 ]; then
995 echo -n "*** ERROR: couldn't register \`$pkg'"
996 echo " in db file ***"
997 exit 1
999 elif [ "$action" = "unregister" ]; then
1000 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
1001 if [ "$?" -ne 0 ]; then
1002 echo -n "*** ERROR: \`$pkg' not registered "
1003 echo "in db file? ***"
1004 exit 1
1006 else
1007 return 1
1012 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
1013 # the minimal required version if there, or 1 otherwise.
1015 check_installed_tmpl()
1017 local pkg="$1"
1018 local reqver="$2"
1019 local iver=
1021 [ -z "$pkg" -o -z "$reqver" ] && return 1
1023 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1025 reqver="$(echo $reqver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1027 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1028 if [ "$?" -eq 0 ]; then
1030 # Package is installed, let's check the version.
1032 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1033 if [ -n "$iver" ]; then
1035 # As shell only supports decimal arith expressions,
1036 # we simply remove anything except the numbers.
1037 # It's not optimal and may fail, but it is enough
1038 # for now.
1040 iver="$(echo $iver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
1041 if [ "$iver" -eq "$reqver" \
1042 -o "$iver" -gt "$reqver" ]; then
1043 return 0
1048 return 1
1052 # Checks the build depends db file and returns 0 if pkg has dependencies,
1053 # otherwise returns 1.
1055 check_build_depends_tmpl()
1057 local pkg="$1"
1059 [ -z $pkg ] && return 1
1060 [ ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1062 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1063 return $?
1067 # Installs a pkg by reading its build template file.
1069 install_tmpl()
1071 local pkg=
1072 cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
1073 if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
1074 echo -n "*** ERROR: invalid template file '$cur_tmpl',"
1075 echo " aborting ***"
1076 exit 1
1079 reset_tmpl_vars
1080 run_file $cur_tmpl
1081 pkg="$1-$version"
1084 # If we are the originator package save the path this template in
1085 # other var for future use.
1087 if [ -z "$origin_tmpl" ]; then
1088 origin_tmpl=$path_fixed
1092 # Install xstow if it's not there.
1094 install_xstow_tmpl
1097 # Check vars for current template.
1099 check_tmpl_vars $pkg
1102 # Install dependencies required by this package.
1104 check_build_depends_tmpl $pkg
1105 if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
1106 install_dependency_tmpl $pkg
1108 # At this point all required deps are installed, and
1109 # only remaining is the origin template; install it.
1111 unset doing_deps
1112 reset_tmpl_vars
1113 run_file $origin_tmpl
1114 check_tmpl_vars $pkgname-$version
1117 if [ -n "$only_build" ]; then
1118 build_tmpl_sources
1119 exit 0
1123 # Fetch, extract, stow and reset vars for a template.
1125 fetch_tmpl_sources
1126 extract_tmpl_sources
1127 build_tmpl_sources
1130 # Just announce that meta-template is installed and exit.
1132 if [ "$build_style" = "meta-template" ]; then
1133 installed_tmpl_handler register $pkgname $version
1134 echo "==> Installed meta-template \`$pkg'."
1135 return 0
1139 # Do not stow the pkg if requested.
1141 [ -z "$only_install" ] && stow_tmpl $pkg
1145 # Lists all currently installed packages.
1147 list_tmpls()
1149 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1150 echo "=> No packages registered or missing register db file."
1151 exit 0
1154 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1155 # Run file to get short_desc and print something useful
1156 run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
1157 echo "$i-$version $short_desc"
1158 reset_tmpl_vars
1159 done
1163 # Removes a currently installed package (unstow + removed from destdir).
1165 remove_tmpl()
1167 local pkg="$1"
1169 if [ -z "$pkg" ]; then
1170 echo "*** ERROR: unexistent package, aborting ***"
1171 exit 1
1174 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1175 echo "*** ERROR: cannot find template file ***"
1176 exit 1
1179 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1182 # If it's a meta-template, just unregister it from the db.
1184 if [ "$build_style" = "meta-template" ]; then
1185 installed_tmpl_handler unregister $pkgname $version
1186 [ "$?" -eq 0 ] && \
1187 echo "=> Removed meta-template \`$pkg'."
1188 return $?
1191 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1192 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1193 exit 1
1196 unstow_tmpl $pkg
1197 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1198 return "$?"
1202 # main()
1204 args=$(getopt bCc:efi $*)
1205 [ "$?" -ne 0 ] && usage
1207 set -- $args
1208 while [ "$#" -gt 0 ]; do
1209 case "$1" in
1211 only_build=yes
1214 dontrm_builddir=yes
1217 config_file_specified=yes
1218 PKGFS_CONFIG_FILE="$2"
1219 shift
1222 only_extract=yes
1225 only_fetch=yes
1228 only_install=yes
1231 shift
1232 break
1234 esac
1235 shift
1236 done
1238 [ "$#" -gt 2 ] && usage
1240 target="$1"
1241 if [ -z "$target" ]; then
1242 echo "*** ERROR missing target ***"
1243 usage
1247 # Check configuration vars before anyting else, and set defaults vars.
1249 check_config_vars
1250 set_defvars
1252 # Main switch
1253 case "$target" in
1254 info)
1255 info_tmpl "$2"
1257 install)
1258 install_tmpl "$2"
1260 list)
1261 list_tmpls
1263 remove)
1264 remove_tmpl "$2"
1266 stow)
1267 stow_flag=yes
1268 stow_tmpl "$2"
1270 unstow)
1271 unstow_tmpl "$2"
1274 echo "*** ERROR invalid target '$target' ***"
1275 usage
1276 esac
1278 # Agur
1279 exit 0