aboutsummaryrefslogtreecommitdiff
path: root/pkgfs.sh
blob: 6625e1727acc4022a24876a3232ff746f6678ae1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
#!/bin/sh
#
# pkgfs - Builds source distribution files and stows/unstows them into
#	  a master directory, thanks to Xstow.
#
#-
# Copyright (c) 2008 Juan Romero Pardines.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#-
#
# TODO
#	- Implement a routine that checks if installed version is sufficient
#	  to satisfy the required dependency... right now it's very prone
#	  to errors and slow.
# 	- Multiple distfiles in a package.
#	- Multiple URLs to download source distribution files.
#	- Support adding filters to templates to avoid creating useless links.
#
# Default path to configuration file, can be overriden
# via the environment or command line.
#
: ${PKGFS_CONFIG_FILE:=/usr/local/etc/pkgfs.conf}

: ${progname:=$(basename $0)}
: ${topdir:=$(/bin/pwd -P 2>/dev/null)}
: ${fetch_cmd:=/usr/bin/ftp -a}
: ${cksum_cmd:=/usr/bin/cksum -a rmd160}
: ${awk_cmd:=/usr/bin/awk}
: ${mkdir_cmd:=/bin/mkdir -p}
: ${tar_cmd:=/usr/bin/tar}
: ${rm_cmd:=/bin/rm}
: ${mv_cmd:=/bin/mv}
: ${cp_cmd:=/bin/cp}
: ${sed_cmd=/usr/bin/sed}
: ${grep_cmd=/usr/bin/grep}
: ${gunzip_cmd:=/usr/bin/gunzip}
: ${bunzip2_cmd:=/usr/bin/bunzip2}
: ${patch_cmd:=/usr/bin/patch}
: ${find_cmd:=/usr/bin/find}
: ${file_cmd:=/usr/bin/file}
: ${ln_cmd:=/bin/ln}
: ${chmod_cmd:=/bin/chmod}
: ${db_cmd:=/usr/bin/db -q}
: ${chmod_cmd:=/bin/chmod}

: ${xstow_args:=-ap}
: ${xstow_ignore_files:=perllocal.pod}	# XXX For now ignore them.

set_defvars()
{
	# Directories
	: ${PKGFS_TEMPLATESDIR:=$PKGFS_DISTRIBUTIONDIR/templates}
	: ${PKGFS_DEPSDIR:=$PKGFS_DISTRIBUTIONDIR/dependencies}
	: ${PKGFS_BUILD_DEPS_DB:=$PKGFS_DEPSDIR/build-depends.db}
	: ${PKGFS_TMPLHELPDIR:=$PKGFS_DISTRIBUTIONDIR/helper-templates}
	: ${PKGFS_REGPKG_DB:=$PKGFS_DESTDIR/.pkgfs-registered-pkgs.db}

	local DDIRS="PKGFS_DEPSDIR PKGFS_TEMPLATESDIR PKGFS_TMPLHELPDIR"
	for i in ${DDIRS}; do
		if [ ! -d "$PKGFS_DEPSDIR" ]; then
			echo "**** ERROR: cannot find $i, aborting ***"
			exit 1
		fi
	done
}

usage()
{
	cat << _EOF
$progname: [-bCefi] [-c <config_file>] <target> <tmplname>

Targets
	info	Show information about <tmplname>.
	install	Build and install package from <tmplname>.
	list	Lists and prints short description about installed packages.
	remove	Remove package completely (unstow and remove from destdir)
	stow	Create symlinks from <tmplname> in master directory.
	unstow	Remove symlinks from <tmplname> in master directory.

Options
	-b	Only build the source distribution file(s), without
		extracting or fetching before.
	-C	Do not remove build directory after successful build.
	-c	Path to global configuration file.
		If not specified /usr/local/etc/pkgfs.conf is used.
	-e	Only extract the source distribution file(s).
	-f	Only fetch the source distribution file(s).
	-i	Only build and install the binary distribution file(s)
		into the destdir directory, without stowning.
_EOF
	exit 1
}

check_path()
{
	eval local orig="$1"

	case "$orig" in
	/)
		;;
	/*)
		orig="${orig%/}"
		;;
	*)
		orig="$topdir/${orig%/}"
		;;
	esac

	path_fixed="$orig"
}

run_file()
{
	local file="$1"

	check_path "$file"
	. $path_fixed
}

#
# This function merges two GNU info dirs into one and puts the result
# into PKGFS_MASTERDIR/share/info/dir.
#
merge_infodir_tmpl()
{
	local pkgname="$1"
	local merge_info_cmd="$PKGFS_MASTERDIR/bin/merge-info"

	[ -z "$pkgname" ] && return 1
	[ ! -r "$PKGFS_MASTERDIR/share/info/dir" ] && return 1
	[ ! -r "$PKGFS_DESTDIR/$pkgname/share/info/dir" ] && return 1

	$merge_info_cmd -d $PKGFS_MASTERDIR/share/info/dir 	\
		$PKGFS_DESTDIR/$pkgname/share/info/dir -o 	\
		$PKGFS_MASTERDIR/share/info/dir.new
	if [ "$?" -ne 0 ]; then
		echo -n "*** WARNING: there was an error merging info dir from"
		echo " $pkgname, aborting ***"
		return 1
	fi

	$mv_cmd -f $PKGFS_MASTERDIR/share/info/dir.new \
		$PKGFS_MASTERDIR/share/info/dir
}

#
# Shows info about a template.
#
info_tmpl()
{
	local tmpl="$1"
	if [ -z "$tmpl" -o ! -f "$PKGFS_TEMPLATESDIR/$tmpl.tmpl" ]; then
		echo -n "*** ERROR: invalid template file \`$tmpl' ***"
		echo ", aborting ***"
		exit 1
	fi

	run_file $PKGFS_TEMPLATESDIR/$tmpl.tmpl

	echo "pkgname:	$pkgname"
	echo "version:	$version"
	for i in "${distfiles}"; do
		[ -n "$i" ] && echo "distfile:	$i"
	done
	echo "URL:		$url"
	echo "maintainer:	$maintainer"
	[ -n $checksum ] && echo "checksum:	$checksum"
	echo "build_style:	$build_style"
	echo "short_desc:	$short_desc"
	echo "$long_desc"
	echo
	check_build_depends_tmpl $pkgname
	if [ "$?" -eq 0 ]; then
		local list="$($db_cmd -V btree $PKGFS_BUILD_DEPS_DB $pkgname)"
		echo "This package requires the following dependencies to be built:"
		for i in ${list}; do
			echo " $i"
		done
	fi
}

#
# Applies to the build directory the patches specified by a template.
#
apply_tmpl_patches()
{
	if [ -z "$PKGFS_TEMPLATESDIR" ]; then
		echo -n "*** WARNING: PKGFS_TEMPLATESDIR is not set, "
		echo "won't apply patches ***"
		return 1
	fi

	#
	# If package needs some patches applied before building,
	# apply them now.
	#
	if [ -n "$patch_files" ]; then
		for i in ${patch_files}; do
			patch="$PKGFS_TEMPLATESDIR/$i"
			if [ ! -f "$patch" ]; then
				echo "*** WARNING: unexistent patch '$i' ***"
				continue
			fi

			# Try to guess if its a compressed patch.
			if $(echo $patch|$grep_cmd -q .gz); then
				$gunzip_cmd $patch
				patch=${patch%%.gz}
			elif $(echo $patch|$grep_cmd -q .bz2); then
				$bunzip2_cmd $patch
				patch=${patch%%.bz2}
			elif $(echo $patch|$grep_cmd -q .diff); then
				# nada
			else
				echo "*** WARNING: unknown patch type '$i' ***"
				continue
			fi

			cd $wrksrc && $patch_cmd < $patch 2>/dev/null
			if [ "$?" -eq 0 ]; then
				echo "=> Patch applied: \`$i'."
			else
				echo -n "*** ERROR: couldn't apply patch '$i'"
				echo ", aborting ***"
				exit 1
			fi
		done
	fi
}

#
# Checks that all required variables specified in the configuration
# file are properly working.
#
check_config_vars()
{
	local cffound=

	if [ -z "$config_file_specified" ]; then
		config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
		for f in $config_file_paths; do
			[ -f $f ] && PKGFS_CONFIG_FILE=$f && cffound=yes
		done
		if [ -z "$cffound" ]; then
			echo -n "*** ERROR: config file not specified "
			echo "and not in default location or current dir ***"
			exit 1
		fi
	fi

	run_file ${PKGFS_CONFIG_FILE}
	PKGFS_CONFIG_FILE=$path_fixed

	if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
		echo -n "*** ERROR: cannot find configuration file: "
		echo	"'$PKGFS_CONFIG_FILE' ***"
		exit 1
	fi

	local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
			  PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"

	for f in ${PKGFS_VARS}; do
		eval val="\$$f"
		if [ -z "$val" ]; then
			echo -n "**** ERROR: '$f' not set in configuration "
			echo "file, aborting ***"
			exit 1
		fi

		if [ ! -d "$f" ]; then
			$mkdir_cmd "$val"
			if [ "$?" -ne 0 ]; then
				echo -n "*** ERROR: couldn't create '$f'"
				echo "directory, aborting ***"
				exit 1
			fi
		fi
	done
}

#
# Resets all vars used by a template.
#
reset_tmpl_vars()
{
	local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
			make_build_args make_install_args build_style	\
			short_desc maintainer long_desc checksum wrksrc	\
			patch_files configure_env make_cmd pkgconfig_override \
			run_stuff_before run_stuff_after \
			run_stuff_before_configure_file run_stuff_before_build_file \
			run_stuff_before_install_file run_stuff_after_install \
			make_build_target make_install_target \
			postinstall_helpers version"

	for i in ${TMPL_VARS}; do
		eval unset "$i"
	done
}

#
# Checks some vars used in templates and sets $extract_cmd.
#
check_tmpl_vars()
{
	local pkg="$1"
	local dfile=""

	[ -z "$pkg" ] && return 1

	REQ_VARS="pkgname version extract_sufx url build_style"

	# Check if required vars weren't set.
	for i in ${REQ_VARS}; do
		eval val="\$$i"
		if [ -z "$val" -o -z "$i" ]; then
			echo -n "*** ERROR: $i not set (incomplete template"
			echo	" build file), aborting ***"
			exit 1
		fi
	done

	if [ -z "$distfiles" ]; then
		dfile="$pkgname-$version$extract_sufx"
	elif [ -n "${distfiles}" ]; then
		dfile="$distfiles$extract_sufx"
	else
		echo "*** ERROR unsupported fetch state ***"
		exit 1
	fi

	dfile="$PKGFS_SRCDISTDIR/$dfile"

	case "$extract_sufx" in
	.tar.bz2|.tar.gz|.tgz|.tbz)
		extract_cmd="$tar_cmd xfz $dfile -C $PKGFS_BUILDDIR"
		;;
	.tar)
		extract_cmd="$tar_cmd xf $dfile -C $PKGFS_BUILDDIR"
		;;
	.zip)
		if [ -f "$PKGFS_TMPLHELPDIR/unzip-extraction.sh" ]; then
			. $PKGFS_TMPLHELPDIR/unzip-extraction.sh
		fi
		# $extract_cmd set by the helper
		;;
	*)
		echo -n "*** ERROR: unknown 'extract_sufx' argument in build "
		echo	"file ***"
		exit 1
		;;
	esac
}

#
# Verifies that a checksum of a distfile is correct.
#
check_rmd160_cksum()
{
	local file="$1"
	local dfile=

	[ -z "$file" ] && return 1

	if [ -z "${distfiles}" ]; then
		dfile="$pkgname-$version$extract_sufx"
	elif [ -n "${distfiles}" ]; then
		dfile="$distfiles$extract_sufx"
	else
		dfile="$file$extract_sufx"
	fi

	if [ -z "$checksum" ]; then
		echo "*** ERROR: checksum unset in template file for \`$pkgname' ***"
		exit 1
	fi

	origsum="$checksum"
	dfile="$PKGFS_SRCDISTDIR/$dfile"
	filesum="$($cksum_cmd $dfile | $awk_cmd '{print $4}')"
	if [ "$origsum" != "$filesum" ]; then
		echo "*** WARNING: checksum doesn't match (rmd160) ***"
		return 1
	fi
}

#
# Downloads the distfiles for a template from $url.
#
fetch_tmpl_sources()
{
	local file=""
	local file2=""

	[ -z "$pkgname" ] && return 1

	if [ -z "$distfiles" ]; then
		file="$pkgname-$version"
	else
		file="$distfiles"
	fi

	for f in "$file"; do
		file2="$f$extract_sufx"
		if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
			check_rmd160_cksum $f
			if [ "$?" -eq 0 ]; then
				if [ -n "$only_fetch" ]; then
					echo "=> checksum ok"
					exit 0
				fi
				return 0
			fi
		fi

		echo "==> Fetching \`$file2' source tarball"

		cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
		if [ "$?" -ne 0 ]; then
			if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
				echo -n "*** ERROR: couldn't fetch '$file2', "
				echo	"aborting ***"
			else
				echo -n "*** ERROR: there was an error "
				echo	"fetching '$file2', aborting ***"
			fi
			exit 1
		else
			if [ -n "$only_fetch" ]; then
				exit 0
			fi
		fi
	done
}

#
# Extracts contents of a distfile specified in a template into
# the build directory.
#
extract_tmpl_sources()
{
	[ -z "$pkgname" ] && return 1

	echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."

	$extract_cmd
	if [ "$?" -ne 0 ]; then
		echo -n "*** ERROR: there was an error extracting the "
		echo "distfile, aborting *** "
		exit 1
	fi

	unset extract_cmd
	[ -n "$only_extract" ] && exit 0
}

fixup_tmpl_libtool()
{
	local lt_file="$wrksrc/libtool"

	#
	# If package has a libtool file replace it with ours, so that
	# we use the master directory while relinking, all will be fine
	# once the package is stowned.
	#
	if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
		$rm_cmd -f $wrksrc/libtool
		$rm_cmd -f $wrksrc/ltmain.sh
		$ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
		$ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
			 $wrksrc/ltmain.sh
	elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
		$ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
	fi
}

#
# Configures, builds and installs a package into the destination
# directory.
#
build_tmpl_sources()
{
	local pkg="$pkgname-$version"

	[ -z "$pkgname" -o -z "$version" ] && return 1

	if [ -n "$distfiles" -a -z "$wrksrc" ]; then
		wrksrc=$PKGFS_BUILDDIR/$distfiles
	elif [ -z "$wrksrc" ]; then
		wrksrc=$PKGFS_BUILDDIR/$pkg
	else
		wrksrc=$PKGFS_BUILDDIR/$wrksrc
	fi

	if [ ! -d "$wrksrc" ]; then
		echo "*** ERROR: unexistent build directory, aborting ***"
		exit 1
	fi

	export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"

	# Apply patches if requested by template file
	apply_tmpl_patches

	echo "==> Building \`$pkg' (be patient, may take a while)"

	#
	# For now, just set them through the environment.
	#
	LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
	export LDFLAGS="-L$PKGFS_DESTDIR/$pkg/lib $LDFLAGS"
	export CFLAGS="$CFLAGS $PKGFS_CFLAGS"
	export CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
	export CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
	export PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"

	# Run stuff before configure.
	for i in "$run_stuff_before"; do
		if [ "$i" = "configure" ]; then
			local bcf="$PKGFS_TEMPLATESDIR/$run_stuff_before_configure_file"
			[ -f $bcf ] && . $bcf
		fi
	done

	#
	# Packages using GNU autoconf
	#
	if [ "$build_style" = "gnu_configure" ]; then
		for i in ${configure_env}; do
			[ -n "$i" ] && export $i
		done
		cd $wrksrc
		#
		# Pass consistent arguments to not have unexpected
		# surprises later.
		#
		./configure	--prefix="$PKGFS_MASTERDIR"		\
				--mandir="$PKGFS_DESTDIR/$pkg/man"	\
				--infodir="$PKGFS_DESTDIR/$pkg/share/info" \
				--sysconfdir="$PKGFS_SYSCONFDIR" \
				${configure_args}

	#
	# Packages using propietary configure scripts.
	#
	elif [ "$build_style" = "configure" ]; then
		cd $wrksrc
		if [ -n "$configure_script" ]; then
			./$configure_script ${configure_args}
		else
			./configure ${configure_args}
		fi
	#
	# Packages that are perl modules and use Makefile.PL files.
	# They are all handled by the helper perl-module.sh.
	#
	elif [ "$build_style" = "perl_module" ]; then
		. $PKGFS_TMPLHELPDIR/perl-module.sh
		perl_module_build $pkgname

	#
	# Packages with BSD or GNU Makefiles are easy, just skip
	# the configure stage and proceed.
	#
	elif [ "$build_style" = "bsd_makefile" -o \
	       "$build_style" = "gnu_makefile" ]; then

	       cd $wrksrc
	#
	# Unknown build_style type won't work :-)
	#
	else
		echo "*** ERROR unknown build_style $build_style, aborting ***"
		exit 1
	fi

	if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
		echo "*** ERROR building (configure state) \`$pkg' ***"
		exit 1
	fi

	#
	# Assume BSD make if make_cmd not set in template.
	#
	if [ -z "$make_cmd" ]; then
		make_cmd="/usr/bin/make"
	fi

	# Fixup libtool script if necessary
	fixup_tmpl_libtool

	#
	# Run template stuff before building.
	#
	for i in ${run_stuff_before}; do
		if [ "$i" = "build" ]; then
			local bbf="$PKGFS_TEMPLATESDIR/$run_stuff_before_build_file"
			[ -f $bbf ] && . $bbf
		fi
	done

	[ -z "$make_build_target" ] && make_build_target=

	#
	# Build package via make.
	#
	${make_cmd} ${make_build_args} ${make_build_target}
	if [ "$?" -ne 0 ]; then
		echo "*** ERROR building (make stage) \`$pkg' ***"
		exit 1
	fi

	#
	# Run template stuff before installing.
	#
	for i in ${run_stuff_before}; do
		if [ "$i" = "install" ]; then
			local bif="$PKGFS_TEMPLATESDIR/$run_stuff_before_install_file"
			[ -f $bif ] && . $bif
		fi
	done

	[ -z "$make_install_target" ] && make_install_target=install

	#
	# Install package via make.
	#
	${make_cmd} ${make_install_args} ${make_install_target} \
		prefix="$PKGFS_DESTDIR/$pkg"
	if [ "$?" -ne 0 ]; then
		echo "*** ERROR instaling \`$pkg' ***"
		exit 1
	fi

	#
	# Run template stuff after installing.
	#
	for i in ${run_stuff_after}; do
		if [ "$i" = "install" ]; then
			local aif="$PKGFS_TEMPLATESDIR/$run_stuff_after_install_file"
			[ -f $aif ] && . $aif
		fi
	done

	#
	# Transform pkg-config files if requested by template.
	#
	for i in ${pkgconfig_override}; do
		local tmpf="$PKGFS_DESTDIR/$pkg/lib/pkgconfig/$i"
		[ -f "$tmpf" ] && \
			[ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
			. $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
			pkgconfig_transform_file $tmpf
	done


	echo "==> Installed \`$pkg' into $PKGFS_DESTDIR/$pkg."

	#
	# Once all work has been done, unset compilation vars.
	#
	unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG

	#
	# Remove $wrksrc if -C not specified.
	#
	if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
		$rm_cmd -rf $wrksrc
		[ "$?" -eq 0 ] && \
			echo "=> Removed \`$pkg' build directory."
	fi

	cd $PKGFS_BUILDDIR
}

#
# Stows a currently installed package, i.e creates the links
# on the master directory.
#
stow_tmpl()
{
	local pkg="$1"
	local infodir_pkg="share/info/dir"
	local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
	local real_xstowargs="$xstow_args"

	[ -z "$pkg" ] && return 2

	if [ -n "$stow_flag" ]; then
		pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
		run_file $pkg
		pkg=$pkgname-$version
	fi

	if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
		merge_infodir_tmpl $pkg
	fi

	if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
	     -a -r "$infodir_master" ]; then
		xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
	fi

	$PKGFS_XSTOW_CMD -ignore ${xstow_ignore_files} ${xstow_args} \
		-pd-targets $PKGFS_MASTERDIR \
		-dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
		$PKGFS_DESTDIR/$pkg
	if [ "$?" -ne 0 ]; then
		echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
		exit 1
	else
		echo "==> Created \`$pkg' symlinks into master directory."
	fi

	xstow_args="$real_xstowargs"

	installed_tmpl_handler register $pkgname $version

	#
	# Run template postinstall helpers if requested.
	#
	if [ "$pkgname" != "${pkg%%-$version}" ]; then
		run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
	fi

	for i in ${postinstall_helpers}; do
		local pihf="$PKGFS_TMPLHELPDIR/$i"
		[ -f "$pihf" ] && . $pihf
	done
}

#
# Unstows a currently stowned package, i.e removes its links
# from the master directory.
#
unstow_tmpl()
{
	local pkg="$1"

	if [ -z "$pkg" ]; then
		echo "*** ERROR: template wasn't specified? ***"
		exit 1
	fi

	if [ "$pkg" = "xstow" ]; then
		echo "*** INFO: You aren't allowed to unstow \`$pkg'."
		exit 1
	fi

	run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl

	$PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
		-D -i-file-in-dir share/info/dir -ignore ${xstow_ignore_files} \
		$PKGFS_DESTDIR/$pkgname-$version
	if [ "$?" -ne 0 ]; then
		exit 1
	else
		$rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
		echo "==> Removed \`$pkg' symlinks from master directory."
	fi

	installed_tmpl_handler unregister $pkgname $version
}

#
# Recursive function that founds dependencies in all required
# packages.
#
add_dependency_tolist()
{
	local curpkg="$1"

	[ -z "$curpkg" ] && return 1
	[ -n "$prev_pkg" ] && curpkg=$prev_pkg

	for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*}); do
		#
		# Check if dep already installed.
		#
		if [ -r "$PKGFS_REGPKG_DB" ]; then
			check_installed_tmpl $i ${i##[aA-zZ]*-}
			#
			# If dep is already installed, put it on the
			# installed deps list and continue.
			#
			if [ "$?" -eq 0 ]; then
				installed_deps_list="$i $installed_deps_list"
				continue
			fi

			deps_list="$i $deps_list"
			[ -n "$prev_pkg" ] && unset prev_pkg
			#
			# Check if dependency needs more deps.
			#
			check_build_depends_tmpl $i
			if [ "$?" -eq 0 ]; then
				add_dependency_tolist $i
				prev_pkg="$i"
			fi
		fi
	done
}

#
# Removes duplicate deps in the installed or not installed list.
#
find_dupdeps_inlist()
{
	local action="$1"
	local tmp_list=
	local dup=

	[ -z "$action" ] && return 1

	case "$action" in
	installed)
		list=$installed_deps_list
		;;
	notinstalled)
		list=$deps_list
		;;
	*)
		return 1
		;;
	esac

	for f in $list; do
		if [ -z "$tmp_list" ]; then
			tmp_list="$f"
		else
			for i in $tmp_list; do
				[ "$f" = "$i" ] && dup=yes
			done

			[ -z "$dup" ] && tmp_list="$tmp_list $f"
			unset dup
		fi
	done

	case "$action" in
	installed)
		installed_deps_list="$tmp_list"
		;;
	notinstalled)
		deps_list="$tmp_list"
		;;
	*)
		return 1
		;;
	esac
}

#
# Installs all dependencies required by a package.
#
install_dependency_tmpl()
{
	local pkg="$1"
	deps_list=
	installed_deps_list=

	[ -z "$pkg" ] && return 1

	doing_deps=true

	add_dependency_tolist $pkg
	find_dupdeps_inlist installed
	find_dupdeps_inlist notinstalled

	echo "==> Required dependencies for $(basename $pkg):"
	for i in ${installed_deps_list}; do
		fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*})"
		echo "	$i: found $fpkg."
	done

	for i in ${deps_list}; do
		echo "	$i: not installed."
	done

	for i in ${deps_list}; do
		# skip dup deps
		echo "=> Installing dependency: $i"
		install_tmpl ${i%-[0-9]*}
	done

	unset installed_deps_list
	unset deps_list
}

#
# Installs and stows the "xstow" package.
#
install_xstow_tmpl()
{
	[ -x "$PKGFS_XSTOW_CMD" ] && return 0

	reset_tmpl_vars
	run_file "$PKGFS_TEMPLATESDIR/xstow.tmpl"
	check_tmpl_vars $pkgname-$version
	fetch_tmpl_sources
	extract_tmpl_sources
	build_tmpl_sources
	PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
	stow_tmpl $pkgname-$version
	#
	# Continue with origin package that called us.
	#
	run_file $origin_tmpl
}

#
# Registers or unregisters a package from the db file.
#
installed_tmpl_handler()
{
	local action="$1"
	local pkg="$2"
	local version="$3"

	[ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
	if [ "$action" = "register" ]; then
		$db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version
		if [ "$?" -ne  0 ]; then
			echo -n "*** ERROR: couldn't register stowned \`$pkg'"
			echo " in db file ***"
			exit 1
		fi
	elif [ "$action" = "unregister" ]; then
		$db_cmd -d btree $PKGFS_REGPKG_DB $pkg
		if [ "$?" -ne 0 ]; then
			echo -n "*** ERROR: \`$pkg' stowned not registered "
			echo "in db file? ***"
			exit 1
		fi
	else
		return 1
	fi
}

#
# Checks the registered pkgs db file and returns 0 if a pkg that satisfies
# the minimal required version if there, or 1 otherwise.
#
check_installed_tmpl()
{
	local pkg="$1"
	local reqver="$2"
	local iver=

	[ -z "$pkg" -o -z "$reqver" ] && return 1

	run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*}.tmpl

	reqver="$(echo $reqver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"

	$db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
	if [ "$?" -eq 0 ]; then
		#
		# Package is installed, let's check the version.
		#
		iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
		if [ -n "$iver" ]; then
			#
			# As shell only supports decimal arith expressions,
			# we simply remove anything except the numbers.
			# It's not optimal and may fail, but it is enough
			# for now.
			#
			iver="$(echo $iver | $sed_cmd 's|\.||g;s|[aA-zZ]||g')"
			if [ "$iver" -eq "$reqver" \
			     -o "$iver" -gt "$reqver" ]; then
			     return 0
			fi
		fi
	fi

	return 1
}

#
# Checks the build depends db file and returns 0 if pkg has dependencies,
# otherwise returns 1.
#
check_build_depends_tmpl()
{
	local pkg="$1"

	[ -z $pkg ] && return 1
	[ ! -r $PKGFS_BUILD_DEPS_DB ] && return 1

	$db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*} 2>&1 >/dev/null
	return $?
}

#
# Installs a pkg by reading its build template file.
#
install_tmpl()
{
	local pkg=
	cur_tmpl="$PKGFS_TEMPLATESDIR/$1.tmpl"
	if [ -z "$cur_tmpl" -o ! -f "$cur_tmpl" ]; then
		echo -n "*** ERROR: invalid template file '$cur_tmpl',"
		echo " aborting ***"
		exit 1
	fi

	reset_tmpl_vars
	run_file $cur_tmpl
	pkg="$1-$version"

	#
	# If we are the originator package save the path this template in
	# other var for future use.
	#
	if [ -z "$origin_tmpl" ]; then
		origin_tmpl=$path_fixed
	fi

	#
	# Install xstow if it's not there.
	#
	install_xstow_tmpl

	#
	# Check vars for current template.
	#
	check_tmpl_vars $pkg

	#
	# Install dependencies required by this package.
	#
	check_build_depends_tmpl $pkg
	if [ "$?" -eq 0 -a -z "$doing_deps" ]; then
		install_dependency_tmpl $pkg
		#
		# At this point all required deps are installed, and
		# only remaining is the origin template; install it.
		#
		unset doing_deps
		reset_tmpl_vars
		run_file $origin_tmpl
		check_tmpl_vars $pkgname-$version
	fi

	if [ -n "$only_build" ]; then
		build_tmpl_sources
		exit 0
	fi

	#
	# Fetch, extract, stow and reset vars for a template.
	#
	fetch_tmpl_sources
	extract_tmpl_sources
	build_tmpl_sources

	#
	# Do not stow the pkg if requested.
	#
	[ -z "$only_install" ] && stow_tmpl $pkg
}

#
# Lists all currently installed packages.
#
list_tmpls()
{
	if [ ! -r "$PKGFS_REGPKG_DB" ]; then
		echo "=> No packages registered or missing register db file."
		exit 0
	fi

	for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
		# Run file to get short_desc and print something useful
		run_file ${PKGFS_TEMPLATESDIR}/$i.tmpl
		echo "$i-$version	$short_desc"
		reset_tmpl_vars
	done
}

#
# Removes a currently installed package (unstow + removed from destdir).
#
remove_tmpl()
{
	local pkg="$1"

	if [ -z "$pkg" ]; then
		echo "*** ERROR: unexistent package, aborting ***"
		exit 1
	fi

	if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
		echo "*** ERROR: cannot find template file ***"
		exit 1
	fi

	run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl

	if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
		echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
		exit 1
	fi

	unstow_tmpl $pkg
	$rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
	return "$?"
}

#
# main()
#
args=$(getopt bCc:efi $*)
[ "$?" -ne 0 ] && usage

set -- $args
while [ "$#" -gt 0 ]; do
	case "$1" in
	-b)
		only_build=yes
		;;
	-C)
		dontrm_builddir=yes
		;;
	-c)
		config_file_specified=yes
		PKGFS_CONFIG_FILE="$2"
		shift
		;;
	-e)
		only_extract=yes
		;;
	-f)
		only_fetch=yes
		;;
	-i)
		only_install=yes
		;;
	--)
		shift
		break
		;;
	esac
	shift
done

[ "$#" -gt 2 ] && usage

target="$1"
if [ -z "$target" ]; then
	echo "*** ERROR missing target ***"
	usage
fi

#
# Check configuration vars before anyting else, and set defaults vars.
#
check_config_vars
set_defvars

# Main switch
case "$target" in
info)
	info_tmpl "$2"
	;;
install)
	install_tmpl "$2"
	;;
list)
	list_tmpls
	;;
remove)
	remove_tmpl "$2"
	;;
stow)
	stow_flag=yes
	stow_tmpl "$2"
	;;
unstow)
	unstow_tmpl "$2"
	;;
*)
	echo "*** ERROR invalid target '$target' ***"
	usage
esac

# Agur
exit 0