aboutsummaryrefslogtreecommitdiffstats
path: root/configure
blob: 6c8eb4bf27e6b50a1471c7c4c5972263c2a896bf (plain)
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
#!/bin/sh

GCC="gcc"
MAKE=""
LEX=""
YACC=""
OS=""

PKG_CONFIG=`which pkg-config`
PERL=`which perl`

LOCALBASE=/usr/local
PREFIX=$LOCALBASE

VERSION=0.0.1
COMPAT_DIR="./compat"

YACC_SRC="cfg_file.y"
LEX_SRC="cfg_file.l"
YACC_OUTPUT="cfg_yacc.c"
LEX_OUTPUT="cfg_lex.c"
CONFIG="config.h"
CACHE="config.cache"

SRCDIR="src"
OBJDIR="src/.obj"
SOURCES="upstream.c cfg_utils.c memcached.c main.c util.c controller.c worker.c fstring.c url.c perl.c protocol.c mem_pool.c filter.c plugins/regexp.c plugins/surbl.c tokenizers/tokenizers.c tokenizers/osb.c statfile.c ${LEX_OUTPUT} ${YACC_OUTPUT}"
MODULES="surbl regexp"

CFLAGS="$CFLAGS -W -Wpointer-arith -Wno-unused-parameter"
CFLAGS="$CFLAGS -Wno-unused-function -Wunused-variable -Wno-sign-compare"
CFLAGS="$CFLAGS -Wunused-value -ggdb -I${LOCALBASE}/include"
CFLAGS="$CFLAGS "
LDFLAGS="$LDFLAGS -L/usr/lib -L${LOCALBASE}/lib"
OPT_FLAGS="-O -pipe -fno-omit-frame-pointer"
DEPS="$SRCDIR/config.h $SRCDIR/cfg_file.h $SRCDIR/memcached.h $SRCDIR/util.h $SRCDIR/main.h $SRCDIR/upstream.h $SRCDIR/fstring.h $SRCDIR/url.h $SRCDIR/perl.h $SRCDIR/mem_pool.h $SRCDIR/protocol.h $SRCDIR/filter.h $SRCDIR/${LEX_OUTPUT} $SRCDIR/${YACC_OUTPUT}"
EXEC=rspamd
USER=postfix
GROUP=postfix
INSTALL="/usr/bin/install -v"
MKDIR="/usr/bin/install -v -d"
MANPATH="${PREFIX}/share/man"
SUBDIRS="test utils"

MAKEFILE="Makefile"
MAKEFILE_IN="Makefile.in"
PERL_MAKEFILE="perl/Makefile.PL.in"
PERLCFLAGS=""
PERLLDFLAGS=""

DEPENDS_FILE=".depends"
OPTS=""

TARGETS="${EXEC}"

check_cache()
{
	type=$1
	name=$2

	line=`fgrep "$type:$name" $CACHE 2>/dev/null`
	if [ $? -eq 0 ] ; then
		result=`echo $line | cut -d ':' -f 3`
		CACHED=`echo $line | cut -d ':' -f 4-`
		if [ "F$result" = "Fyes" ] ; then
			return 0
		elif [ "F$result" = "Fno" ] ; then
			return 1
		else
			return 2
		fi
	else
		return 3
	fi
}

write_cache()
{
	type=$1
	name=$2
	result=$3
	value=$4

	echo "$type:$name:$result:$value" >> $CACHE
}

prepare_config()
{
	echo "#ifndef CONFIG_H_IN" > $SRCDIR/$CONFIG
	echo "#define CONFIG_H_IN" >> $SRCDIR/$CONFIG
	cat "$CONFIG.in" >> $SRCDIR/$CONFIG
}

finish_config()
{
	echo "#endif" >> $SRCDIR/$CONFIG
}

cleanup()
{
	rm -f autotest.c
	rm -f autotest
	INCLUDE=""
}

have_opt()
{
	echo "#define HAVE_$1" >> $SRCDIR/$CONFIG
	OPTS="$OPTS HAVE_$1"
}

check_compiler()
{	
	echo -n "Testing for gcc: "
	check_cache "compiler" "gcc"
	if [ $? -eq 0 ] ; then
		GCC=$CACHED
		echo "found(cached) -> $GCC"
		return 0
	fi

	GCC=`PATH="$PATH:$PREFIX/bin:$LOCALBASE/bin" which gcc`
	if [ -x $GCC ] ; then
		echo "found -> $GCC"
		write_cache "compiler" "gcc" "yes" "$GCC"
		return 0
	else
		echo "not found"
		exit 1
	fi
}

check_make()
{	
	echo -n "Testing for make: "
	check_cache "make" "make"
	if [ $? -eq 0 ] ; then
		MAKE=$CACHED
		echo "found(cached) -> $MAKE"
		return 0
	fi
	MAKE=`PATH="$PATH:$PREFIX/bin:$LOCALBASE/bin" which make`
	if [ -x $MAKE ] ; then
		echo "found -> $MAKE"
		write_cache "make" "make" "yes" "$MAKE"
		return 0
	else
		echo "not found"
		exit 1
	fi

}

check_lex()
{
	echo -n "Testing for lex: "
	check_cache "binary" "lex"
	if [ $? -eq 0 ] ; then
		LEX=$CACHED
		echo "found(cached) -> $LEX"
		return 0
	fi

	LEX=`PATH="$PATH:$PREFIX/bin:$LOCALBASE/bin" which lex`
	if [ -x $LEX ] ; then
		echo "found -> $LEX"
		write_cache "binary" "lex" "yes" "$LEX"
		return 0
	else
		echo "not found"
		exit 1
	fi

}

check_yacc()
{
	echo -n "Testing for yacc: "
	check_cache "binary" "yacc"
	if [ $? -eq 0 ] ; then
		YACC=$CACHED
		echo "found(cached) -> $YACC"
		return 0
	fi

	YACC=`PATH="$PATH:$PREFIX/bin:$LOCALBASE/bin" which yacc`
	echo -n "Testing for yacc: "
	if [ -x $YACC ] ; then
		echo "found -> $YACC"
		write_cache "binary" "yacc" "yes" "$YACC"
		return 0
	else
		echo "not found"
		exit 1
	fi

}

check_util()
{
	echo -n "Testing for $1: "
	check_cache "binary" "$1"
	if [ $? -eq 0 ] ; then
		echo "found(cached) -> $CACHED"
		return 0
	fi
	UTIL=`PATH="$PATH:$PREFIX/bin:$LOCALBASE/bin" which $1`
	if [ -x $UTIL ] ; then
		echo "found -> $UTIL"
		write_cache "binary" "$1" "yes" "$UTIL"
		return 0
	else
		echo "not found"
		exit 1
	fi

}

check_function()
{
	FUNCTION=$1
	while [ $# -ne 0 -a -n $2 ] ; do 
		shift
		if [ "F$INCLUDE" = "F" ] ; then
			INCLUDE="$1"
		else
			INCLUDE="$INCLUDE $1"
		fi
	done
	echo -n "Testing for $FUNCTION: "
	echo >> config.log
	echo "Testing for $FUNCTION: " >> config.log
	check_cache "function" "$FUNCTION"
	if [ $? -eq 0 ] ; then
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		return 0
	fi
	echo "#include <sys/types.h>" > autotest.c
	if [ "F$INCLUDE" != "F" ] ; then
		for inc in $INCLUDE ; do
			echo "#include \"$inc\"" >> autotest.c
		done
	fi
	echo "#include <stdlib.h>" >> autotest.c
	echo "int main (int argc, char **argv) { $FUNCTION; return 0; }" >> autotest.c
	echo "$GCC $PTHREAD_CFLAGS $CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS  autotest.c" >>config.log
	$GCC $PTHREAD_CFLAGS $CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS  autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		cleanup
		echo "-> OK" >> config.log
		write_cache "function" "$FUNCTION" "yes" ""
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		cleanup
		write_cache "function" "$FUNCTION" "no" ""
		return 1
	fi
}

check_include()
{
	INCLUDE="$1"
	echo -n "Testing for $INCLUDE: "
	echo >> config.log
	echo "Testing for $INCLUDE: " >> config.log
	check_cache "include" "$INCLUDE"
	if [ $? -eq 0 ] ; then
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		have_opt $CACHED
		return 0
	fi
	echo "#include <sys/types.h>" > autotest.c
	echo "#include \"$INCLUDE\"" >> autotest.c
	echo "#include <stdlib.h>" >> autotest.c
	echo "int main (int argc, char **argv) { return 0; }" >> autotest.c
	echo "$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS autotest.c" >>config.log
	$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		echo "-> OK" >> config.log
		_CFLAG=`echo "$INCLUDE" | sed -e 's/[./]/_/g' | tr '[:lower:]' '[:upper:]'`
		have_opt $_CFLAG
		write_cache "include" "$INCLUDE" "yes" "$_CFLAG"
		cleanup
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		write_cache "include" "$INCLUDE" "no" ""
		cleanup
		return 1
	fi
}

check_macro()
{
	MACRO=$1
	while [ $# -ne 1 -a -n $1 ] ; do 
		shift
		if [ "F$INCLUDE" = "F" ] ; then
			INCLUDE="$1"
		else
			INCLUDE="$INCLUDE $1"
		fi
	done
	echo -n "Testing for $MACRO: "
	echo >> config.log
	echo "Testing for $MACRO: " >> config.log
	check_cache "macro" "$MACRO"
	if [ $? -eq 0 ] ; then
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		return 0
	fi
	echo "#include <sys/types.h>" > autotest.c
	for inc in $INCLUDE ; do
		echo "#include \"$inc\"" >> autotest.c
	done
	echo "#include \"${INCLUDE}\"" >> autotest.c
	echo "#include <stdlib.h>" >> autotest.c
	echo "int main (int argc, char **argv) {" >>autotest.c
	echo "#ifndef $MACRO" >>autotest.c
	echo "#error \"$MACRO not defined\"" >>autotest.c 
	echo "#endif" >> autotest.c
	echo "}" >>autotest.c
	echo "$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS autotest.c" >> config.log
	$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		echo "-> OK" >> config.log
		write_cache "macro" "$MACRO" "yes" ""
		cleanup
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		write_cache "macro" "$MACRO" "no" ""
		cleanup
		return 1
	fi
}


check_lib()
{
	LIB=$1
	while [ $# -ne 1 -a -n $1 ] ; do 
		shift
		if [ "F$INCLUDE" = "F" ] ; then
			INCLUDE="$1"
		else
			INCLUDE="$INCLUDE $1"
		fi
	done
	echo -n "Testing for lib$LIB: "
	echo >> config.log
	echo "Testing for lib$LIB: " >> config.log
	check_cache "lib" "$LIB"
	if [ $? -eq 0 ] ; then
		LIBS="$LIBS -l$LIB"
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		return 0
	fi
	echo "#include <sys/types.h>" > autotest.c
	if [ "F$INCLUDE" != "F" ] ; then
		for inc in $INCLUDE ; do
			echo "#include \"$inc\"" >> autotest.c
		done
	fi
	echo "#include <stdlib.h>" >> autotest.c
	echo "int main (int argc, char **argv) { return 0; }" >> autotest.c
	echo "$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS -l$LIB $PTHREAD_LDFLAGS autotest.c" >>config.log
	$GCC $CFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS -l$LIB $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		LIBS="$LIBS -l$LIB"
		echo "-> OK" >> config.log
		write_cache "lib" "$LIB" "yes" ""
		cleanup
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		write_cache "lib" "$LIB" "no" ""
		cleanup
		return 1
	fi
}

check_package()
{
	PACKAGE=$1
	PLIBS=`$PKG_CONFIG --libs $PACKAGE`
	PCFLAGS=`$PKG_CONFIG --cflags $PACKAGE`
	while [ $# -ne 1 -a -n $1 ] ; do 
		shift
		if [ "F$INCLUDE" = "F" ] ; then
			INCLUDE="$1"
		else
			INCLUDE="$INCLUDE $1"
		fi
	done
	echo -n "Testing for $PACKAGE: "
	echo >> config.log
	echo "Testing for $PACKAGE: " >> config.log
	check_cache "package" "$PACKAGE"
	if [ $? -eq 0 ] ; then
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		_p_cflags=`echo $CACHED | cut -d ':' -f 1`
		_p_libs=`echo $CACHED | cut -d ':' -f 2`
		LIBS="$LIBS $_p_libs"
		CFLAGS="$CFLAGS $_p_cflags"
		return 0
	fi
	echo "#include <sys/types.h>" > autotest.c
	if [ "F$INCLUDE" != "F" ] ; then
		for inc in $INCLUDE ; do
			echo "#include \"$inc\"" >> autotest.c
		done
	fi
	echo "#include <stdlib.h>" >> autotest.c
	echo "int main (int argc, char **argv) { return 0; }" >> autotest.c
	echo "$GCC $CFLAGS $PCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PLIBS $PTHREAD_LDFLAGS autotest.c" >>config.log
	$GCC $CFLAGS $PCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PLIBS $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		LIBS="$LIBS $PLIBS"
		CFLAGS="$CFLAGS $PCFLAGS"
		echo "-> OK" >> config.log
		write_cache "package" "$PACKAGE" "yes" "$PCFLAGS:$PLIBS"
		cleanup
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		write_cache "package" "$PACKAGE" "no" ""
		cleanup
		return 1
	fi

}

check_perl()
{
	echo -n "Testing for $PERL: "
	echo >> config.log
	echo "Testing for $PERL: " >> config.log
	check_cache "perl" "$PERL"
	if [ $? -eq 0 ] ; then
		echo "-> OK (cached)"
		echo "-> OK (cached)" >> config.log
		_p_cflags=`echo $CACHED | cut -d ':' -f 1`
		_p_libs=`echo $CACHED | cut -d ':' -f 2`
		PERLLDFLAGS="$LIBS $_p_libs"
		PERLCFLAGS="$CFLAGS $_p_cflags"
		return 0
	fi

	if [ ! -x $PERL ] ; then
		echo "not found"
		echo "-> FAILED" >> config.log
		cleanup
		return 1
	fi
	PERLCFLAGS=`$PERL -MExtUtils::Embed -e ccopts`
	PERLLDFLAGS=`$PERL -MExtUtils::Embed -e ldopts`

	echo "#include <stdlib.h>" > autotest.c
	echo "#include <EXTERN.h>" >> autotest.c
	echo "#include <perl.h>" >> autotest.c
	echo "int main (int argc, char **argv) { PERL_SYS_INIT3(&argc,&argv,&env); return 0; }" >> autotest.c
	echo "$GCC $CFLAGS $PERLCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PERLLDFLAGS $PTHREAD_LDFLAGS autotest.c" >>config.log
	$GCC $CFLAGS $PERLCFLAGS $PTHREAD_CFLAGS -o autotest $LDFLAGS $LIBS $PERLLDFLAGS $PTHREAD_LDFLAGS autotest.c >>config.log 2>&1
	if [ $? -eq 0 ] ; then 
		echo "found"
		echo "-> OK" >> config.log
		cleanup
		write_cache "perl" "$PERL" "yes" "$PERLCFLAGS:$PERLLDFLAGS"
		return 0
	else
		echo "not found"
		echo "-> FAILED" >> config.log
		echo "Failed program was:" >> config.log
		cat autotest.c >> config.log
		write_cache "perl" "$PERL" "no" ""
		cleanup
		return 1
	fi

}

check_os()
{
	_OS=`uname -s`
	case "$_OS" in
		FreeBSD*)
				OS="freebsd"
				CFLAGS="${CFLAGS}"
				INSTALL="/usr/bin/install -C -S -v"
				MKDIR="/usr/bin/install -d -v"
				MANPATH="${PREFIX}/man" 
				echo "#define FREEBSD" >> $SRCDIR/$CONFIG ;;
		Linux*)	OS="linux" CFLAGS="${CFLAGS} -D_GNU_SOURCE"  
				echo "#define LINUX" >> $SRCDIR/$CONFIG ;;
		Solaris*)	OS="solaris" CFLAGS="${CFLAGS}"
				echo "#define SOLARIS" >> $SRCDIR/$CONFIG ;;
		*)		OS="unknown"  ;;
	esac
}

check_user()
{
	_user=$1
	echo -n "Checking for user $_user: "
	grep $_user /etc/passwd > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		echo "found"
		return 0
	else
		echo "not found"
		return 1
	fi
}

check_group()
{
	_group=$1
	echo -n "Checking for group $_group: "
	grep $_group /etc/group > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		echo "found"
		return 0
	else
		echo "not found"
		return 1
	fi
}

check_depends()
{
	ifdep=""
	depends=""
	if [ ! -f $DEPENDS_FILE ] ; then
		return 0
	fi

	while read line ; do
		echo $line | grep '#if' > /dev/null
		if [ $? -eq 0 ] ; then
			ifdep=`echo $line | sed -e 's/^#if \([A-Za-z0-9_]*\)$/\1/'`
		elif [ "F$ifdep" != "F" ] ; then
			echo $line | grep "#endif" > /dev/null
			if [ $? -eq 0 ] ; then
				ifdep=""
			else
				echo $OPTS | grep $ifdep > /dev/null
				if [ $? -eq 0 ] ; then
					depends="$depends $line"
				fi
			fi
		else
			depends="$depends $line"
		fi
	done < $DEPENDS_FILE
	
	obj_depends=`echo $depends | sed -e 's/\([^ ]\{1,\}\)\/\([^/]\{1,\}\).c/\2.o/g'`
	echo "DEPENDS=$depends" >> $MAKEFILE
	echo "OBJ_DEPENDS=$obj_depends" >> $MAKEFILE
	for _dep in $depends ; do
		_obj_dep=`echo $_dep | sed -e 's/\([^ ]\{1,\}\)\/\([^/]\{1,\}\).c/\2.o/g'`
		cat >> $MAKEFILE.dep << END
${_obj_dep}: ${_dep}
	\$(CC) \$(OPT_FLAGS) \$(CFLAGS) \$(PTHREAD_CFLAGS) -o ${_obj_dep} -c ${_dep}

END

	done
}

write_modules()
{
	# Write modules init function
	echo "#ifndef MODULES_H" > $SRCDIR/modules.h
	echo "#include \"config.h\"" >> $SRCDIR/modules.h
	echo "#include \"modules.h\"" > $SRCDIR/modules.c
	echo "module_t modules[] = {" >> $SRCDIR/modules.c;
	modules_num=0
	for m in $MODULES ; do
		echo "{\"${m}\", ${m}_module_init, ${m}_module_config, ${m}_module_reconfig}," >> $SRCDIR/modules.c
		echo "int ${m}_module_init(struct config_file *cfg, struct module_ctx **ctx);" >> $SRCDIR/modules.h
		echo "int ${m}_module_config(struct config_file *cfg);" >> $SRCDIR/modules.h
		echo "int ${m}_module_reconfig(struct config_file *cfg);" >> $SRCDIR/modules.h
		modules_num=`expr $modules_num + 1`
	done
	echo "};" >> $SRCDIR/modules.c
	echo "#endif" >> $SRCDIR/modules.h
	echo "#define MODULES_NUM $modules_num" >> $SRCDIR/$CONFIG
	SOURCES="$SOURCES modules.c"

}

write_perl()
{
	# Write to perl Makefile
	sed -e "s|%%libs%%|${LDFLAGS} ${LIBS}|" < $PERL_MAKEFILE > .pl_tmp
	sed -e "s|%%include%%|${CFLAGS}|" < .pl_tmp > .pl2_tmp
	PERL_MAKEFILE=`echo ${PERL_MAKEFILE} | sed -e 's|.in$||'`
	mv .pl2_tmp $PERL_MAKEFILE
	rm -f .pl_tmp .pl2_tmp
	CURDIR=`pwd`
	cd `dirname ${PERL_MAKEFILE}`
	$PERL Makefile.PL
	cd $CURDIR
}

write_subdirs()
{
	# Write subdirs makefiles
	clean_target="clean-subdirs: "
	dist_clean_target="dist-clean-subdirs: "
	for sub in $SUBDIRS ; do
		cp $MAKEFILE $sub/$MAKEFILE
		saved_pwd=`pwd`
		old_objs=`echo $OBJECTS | sed -e 's/\([^. ]*\.o\)/..\/%%OBJDIR%%\/\1/g' | sed -e "s,%%OBJDIR%%,${OBJDIR},g"`
		old_srcs=`echo $SOURCES | sed -e 's/\([^. ]*\.c\)/..\/%%SRCDIR%%\/\1/g' | sed -e "s,%%SRCDIR%%,${SRCDIR},g"`
		cd $sub
		sub_src="`echo *.c`"
		sub_obj="`echo $sub_src | sed -e 's/\.c/\.o/g'`"
		echo "SOURCES=$sub_src" >> $MAKEFILE
		echo "OBJECTS=$sub_obj" >> $MAKEFILE
		check_depends
		cat Makefile.in >> $MAKEFILE
		if [ -f $MAKEFILE.dep ] ; then
			cat $MAKEFILE.dep >> $MAKEFILE
			rm -f $MAKEFILE.dep
		fi
		for _sub_src in $sub_src ; do
			_sub_obj=`echo $_sub_src | sed -e 's/\.c/\.o/g'`
			cat >> $MAKEFILE << END
${_sub_obj}: ${_sub_src}
	\$(CC) \$(OPT_FLAGS) \$(CFLAGS) \$(PTHREAD_CFLAGS) -o ${_sub_obj} -c ${_sub_src}

END
		done
		cd $saved_pwd
		dist_clean_target="$dist_clean_target ${sub}-dist-clean"
		clean_target="$clean_target ${sub}-clean"
	done

	# Write IN file
	cat $MAKEFILE_IN >> $MAKEFILE

	# Process clean targets for all subdirs
	echo $dist_clean_target >> $MAKEFILE
	echo $clean_target >> $MAKEFILE
	for sub in $SUBDIRS ; do
		cat >> $MAKEFILE << END
${sub}-clean:
	cd ${sub} && make clean && cd ..
${sub}-dist-clean:
	cd ${sub} && make dist-clean && cd ..
${sub}:
	cd ${sub} && make && cd ..

END
	done
}

write_result()
{
	echo "Compiler: $GCC" >> config.log 
	echo "Make: $MAKE" >> config.log 
	echo "Sources: $SOURCES" >> config.log
	echo "Cflags: $CFLAGS" >> config.log
	echo "Ldflags: $LDFLAGS" >> config.log
	echo "Libs: $LIBS" >> config.log
	echo "#define RVERSION \"${VERSION}\"" >> $SRCDIR/$CONFIG
	echo "#define HASH_COMPAT" >> $SRCDIR/$CONFIG

	write_modules
	for _s in $SOURCES ; do
		_o=`echo $_s | awk -F '/' '{split($NF, A, "."); print "%%OBJDIR%%/"A[1]".o"}' | sed -e "s,%%OBJDIR%%,${OBJDIR},"`
		OBJECTS="$OBJECTS $_o"
		SRC_OBJ="$SRC_OBJ $_s#$_o"
	done
	write_perl
	# Make CFLAGS more readable
	CFLAGS="$CFLAGS $PERLCFLAGS"
	LIBS="$LIBS $PERLLDFLAGS"
	CFLAGS=`echo $CFLAGS | tr [:space:] \\\n | sed -e 's/$/ \\\/' | sed -e '$,$s/\\\//' | sort -r -k2 | uniq`
	LDFLAGS=`echo $LDFLAGS | tr [:space:] \\\n | sed -e 's/$/ \\\/' | sed -e '$,$s/\\\//' | sort -r -k2 | uniq`
	LIBS=`echo $LIBS | tr [:space:] \\\n | sed -e 's/$/ \\\/' | sed -e '$,$s/\\\//' | sort -r -k2 | uniq`
	cat > $MAKEFILE << END
# This is ${EXEC} Makefile
# For options edit Makefile.in, this file is autogenerated by configure

CC=$GCC
# Optimization flags
OPT_FLAGS=$OPT_FLAGS
# Compile time flags
CFLAGS=$CFLAGS
# Link time flags
LDFLAGS=$LDFLAGS
# Libraries to link
LIBS=$LIBS
# Directories for sources and objects
SRCDIR=$SRCDIR
OBJDIR=$OBJDIR
# ${EXEC} sources
SOURCES=$SOURCES
# ${EXEC} objects 
OBJECTS=$OBJECTS
# Version of product
VERSION=$VERSION
# Detected operation system
OS=$OS
# Lex and yacc executables
LEX=$LEX
YACC=$YACC
# Pthread specific flags
PTHREAD_CFLAGS=$PTHREAD_CFLAGS
PTHREAD_LDFLAGS=$PTHREAD_LDFLAGS
# Prefix to install
PREFIX=$PREFIX
# Where local libs and includes are located
LOCALBASE=$LOCALBASE
# Install commands
INSTALL=$INSTALL
MKDIR=$MKDIR
# Executable name
EXEC=$EXEC
# User and group
RSPAMD_USER=$USER
RSPAMD_GROUP=$GROUP
# All target dependenses
TARGETS=$TARGETS $SUBDIRS
# Common dependenses
DEPS=$DEPS
# Path to install manual page
MANPATH=$MANPATH
# Subdirectories
SUBDIRS=$SUBDIRS

END

	write_subdirs

	# Write build targets to makefile
	cat >> $MAKEFILE << END
${EXEC}: \$(OBJECTS)
	\$(CC) \$(PTHREAD_LDFLAGS) \$(LDFLAGS) \$(OBJECTS) \$(LIBS) -o \$(EXEC)
END
	for o in $SRC_OBJ ; do
		SO=`echo $o | cut -d '#' -f 1`
		O=`echo $o | cut -d '#' -f 2`
		cat >> $MAKEFILE << END
${O}: \$(DEPS) ${SRCDIR}/${SO}
	\$(CC) \$(OPT_FLAGS) \$(CFLAGS) \$(PTHREAD_CFLAGS) -o ${O} -c ${SRCDIR}/${SO}

END
	done
	cat >> $MAKEFILE << END
${SRCDIR}/${LEX_OUTPUT}: \$(SRCDIR)/cfg_file.h \$(SRCDIR)/${LEX_SRC} \$(SRCDIR)/${YACC_OUTPUT}
	\$(LEX) -o\$(SRCDIR)/${LEX_OUTPUT} \$(SRCDIR)/${LEX_SRC}

${SRCDIR}/${YACC_OUTPUT}: \$(SRCDIR)/cfg_file.h \$(SRCDIR)/${YACC_SRC}
	\$(YACC) -d -o \$(SRCDIR)/${YACC_OUTPUT} \$(SRCDIR)/${YACC_SRC}
END
}


for option
do
    case "$option" in
        -*=*) value=`echo "$option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
           *) value="" ;;
    esac

    case "$option" in
        --help)                          help=yes                  					;;
		--prefix)						 PREFIX=$value								;;
		--user)							 USER=$value								;;
		--group)						 GROUP=$value								;;
		--enable-debug)					 CFLAGS="$CFLAGS -DWITH_DEBUG" OPT_FLAGS=""	;;
		--enable-opt)                    OPT_FLAGS="-O3 -pipe"						;;
		--add-module)					 SOURCES="$SOURCES $value"
										 if [ ! -f $value ] ; then
										 	echo "Cannot find $value, please check path"
											exit 1
										 fi
										 modline=`grep '/***MODULE:' $value`
										 if [ F"$modline" = "F" ] ; then
										 	echo "Cannot find /***MODULE line in $value, please check syntax"
											exit 1
										 fi

										 name=`echo $modline | sed 's/.*MODULE:\([^: ]*\).*/\1/'`
										 MODULES="$MODULES $name"							;;
		--disable-module)				 SOURCES=`echo $SOURCES | sed -e "s/${value}//g"`
										 if [ ! -f $value ] ; then
										 	echo "Cannot find $value, please check path"
											exit 1
										 fi
										 modline=`grep '/***MODULE:' $value`
										 if [ F"$modline" = "F" ] ; then
										 	echo "Cannot find /***MODULE line in $value, please check syntax"
											exit 1
										 fi

										 name=`echo $modline | sed 's/.*MODULE:\([^: ]*\).*/\1/'`
										 MODULES=`echo $MODULES | sed -e "s/${name}//g"`		;;
		*)
            echo "$0: error: invalid option \"$option\""
            exit 1
        ;;
    esac
done

if [ "F$help" = "Fyes" ] ; then
	cat << END

  --help                             this message

  --prefix=PATH                      set the installation prefix
  --enable-debug                     turn on extra debug messages
  --enable-opt                       turn on extra optimization
  --add-module						 add additional C module
  --disable-module                   do not include C module
  --user=USER                        set user to use
  --group=GROUP                      set group to use
END
	exit 1
fi

CFLAGS="$CFLAGS -I$PREFIX/include"
LDFLAGS="$LDFLAGS -L$PREFIX/lib"

echo "Starting configure for rmilter" >config.log
echo $0 $@ >> config.log

prepare_config
check_compiler
check_make
check_lex
check_yacc
check_util "sed"
check_util "awk"
check_util "cut"
check_os

check_lib "event" "event.h"
if [ $? -eq 1 ] ; then
	echo "Libevent not found, check config.log for details"
	exit 1
fi

check_lib "m"
check_lib "md"
if [ $? -eq 1 ] ; then
	cp $COMPAT_DIR/md5.c $SRCDIR
	cp $COMPAT_DIR/md5.h $SRCDIR
	SOURCES="$SOURCES md5.c"
	DEPS="$DEPS $SRCDIR/md5.h"
	have_opt "OWN_MD5"
fi

check_lib "util"
if [ $? -eq 0 ] ; then
	have_opt "LIBUTIL"
fi

check_function "pidfile_open" "sys/param.h" "libutil.h"
if [ $? -eq 0 ] ; then
	have_opt "PIDFILE"
fi

check_function "strlcpy" "string.h"
if [ $? -eq 1 ] ; then
	cp $COMPAT_DIR/strlcpy.c $SRCDIR
	cp $COMPAT_DIR/strlcpy.h $SRCDIR
	SOURCES="$SOURCES strlcpy.c"
	DEPS="$DEPS $SRCDIR/strlcpy.h"
	have_opt "STRLCPY_H"
fi
check_function "bzero" "string.h"
check_function "srandomdev"
if [ $? -eq 0 ] ; then
	have_opt "SRANDOMDEV"
fi

check_function "sched_yield" "sched.h"
if [ $? -eq 0 ] ; then
	have_opt "SCHED_YIELD"
fi

check_function "nanosleep" "time.h"
if [ $? -eq 0 ] ; then
	have_opt "NANOSLEEP"
fi


check_function "getpagesize" "unistd.h"
if [ $? -eq 0 ] ; then
	have_opt "GETPAGESIZE"
fi

check_function "setproctitle" "unistd.h"
if [ $? -eq 0 ] ; then
	have_opt "SETPROCTITLE"
fi

check_include "endian.h"
check_include "libutil.h"
check_include "machine/endian.h"
check_include "sys/time.h"
check_include "time.h"
check_include "stdint.h"
if [ $? -eq 1 ] ; then
	check_include "inttypes.h"
fi
check_include "strlcpy.h"
check_include "md5.h"
check_include "sys/queue.h"
if [ $? -eq 1 ] ; then 
	cp $COMPAT_DIR/queue.h $SRCDIR
	DEPS="$DEPS $SRCDIR/queue.h"
fi
check_macro "SLIST_FOREACH_SAFE" "sys/queue.h"
if [ $? -eq 1 ] ; then 
	cp $COMPAT_DIR/queue.h $SRCDIR
	DEPS="$DEPS $SRCDIR/queue.h"
	have_opt "OWN_QUEUE_H"
fi

check_macro "PATH_MAX" "limits.h"
if [ $? -eq 1 ] ; then
	check_macro "MAXPATHLEN" "sys/param.h"
	if [ $? -eq 1 ] ; then
		have_opt "MAXPATHLEN"
		echo "#define MAXPATHLEN 4096" >> $SRCDIR/$CONFIG
	else
		have_opt "MAXPATHLEN"
	fi
else
	have_opt "PATH_MAX"
fi

check_macro "MAP_SHARED" "sys/mman.h"
if [ $? -eq 1 ] ; then
	echo "Shared memory extension to mmap not found, this platform is not supported"
	exit 1
fi

check_macro "MAP_ANON" "sys/mman.h"
if [ $? -eq 0 ] ; then
	have_opt "MMAP_ANON"
else
	if [ -f "/dev/zero" ] ; then
		have_opt "MMAP_ZERO"
	else
		echo "Anon memory extension to mmap not found and /dev/zero not exists, this platform is not supported"
		exit 1
	fi
fi

check_package "glib-2.0" "glib.h"
check_package "gmime-2.0" "gmime/gmime.h"
if [ $? -eq 1 ] ; then
	echo "Package gmime-2.0 not found, check config.log for details"
	exit 1
fi

check_perl
if [ $? -eq 1 ] ; then
	echo "Perl or libperl not found, check config.log for details"
	exit 1
fi

check_group $GROUP
if [ $? -ne 0 ] ; then 
	TARGETS="$TARGETS creategroup"
fi
check_user $USER
if [ $? -ne 0 ] ; then 
	TARGETS="$TARGETS createuser"
fi
write_result
finish_config