~m-grant-prg/swoc/trunk

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
#########################################################################
#									#
# Script ID: ./configure.ac						#
# Author: Copyright (C) 2015-2022  Mark Grant				#
#									#
# Released under the GPLv3 only.					#
# SPDX-License-Identifier: GPL-3.0-only					#
#									#
# Purpose:								#
#	AutoConf script file to configure SWOCServer-C project.		#
#									#
# cmd line syntax:							#
# ./configure	[--enable-atonly=<yes|no> [default: no]]		#
#		[--enable-debug=<yes|no> [default: no]]			#
#		[--enable-distcheckfake=<yes|no> [default: no]]		#
#		[--enable-headercheck=<yes|no> [default: no]]		#
#		[--enable-sparse=<yes|no> [default: no]]		#
#		[--enable-systemd=yes|no> [default: no]]		#
#									#
# Parameters generic description. For specifics to this configure.ac	#
# please see inline comments at the point of definition:-		#
# --enable-atonly							#
#	Used to produce a fully working package just from the AT build	#
#	when the build references a (distro / external) static location	#
#	eg another package's config file in /etc (AOT AT's		#
#	/usr/local/etc), or an externally installed jar file in		#
#	/usr/share/jave (AOT AT's /usr/local/share/java).		#
# --enable-debug							#
#	Used to set things like compiler debug flags.			#
# --enable-distcheckfake						#
#	Enables the replacement of an absolute directory path specified	#
#	in a variable, with a value based on AutoTools standard		#
#	directories. This enables the Makefile.am to use the AT		#
#	variable based value when running distcheck and the absolute	#
#	path for all else. Hence distcheck can now succeed when it	#
#	would normally fail.						#
# --enable-headercheck							#
#	Used to set a flag to output the header file nesting.		#
# --enable-sparse							#
#	Used to compile with the sparse static analyser.		#
#									#
# Layout: (Only relevant sections will exist).				#
# ChangeLog : High level ChangeLog					#
# SECTION  1: Initialisation & Setup					#
# SECTION  2: Architecture, target and host checks			#
# SECTION  3: Command line arguments					#
# SECTION  4: Compiler setup						#
# SECTION  5: Check for programs					#
# SECTION  6: Check for libraries					#
# SECTION  7: Check for header files					#
# SECTION  8: Check for types						#
# SECTION  9: Check for structures					#
# SECTION 10: Check for compiler characteristics			#
# SECTION 11: Check for library functions				#
# SECTION 12: Check for pkg-config files				#
# SECTION 13: Check for system services					#
# SECTION 14: Additional checks & variables				#
# SECTION 15: Specify the output files					#
#									#
#########################################################################

#########################################################################
#									#
# ChangeLog								#
#									#
# Date		Author	Version	Description				#
#									#
# 22/06/2015	MG	1.0.1	First release.				#
# 02/07/2015	MG	1.0.2	Remove redundant c++ compiler check.	#
# 06/07/2015	MG	1.0.3	Make libconffile pkg-config check	#
#				display name more meaningful.		#
# 12/07/2015	MG	1.0.4	Change to use libmgesysutils.		#
# 27/09/2015	MG	1.0.5	Add check for txt2manwrap as part of	#
#				changes to conform to AutoTools		#
#				Template version 1.0.4.			#
# 24/10/2015	MG	1.0.6	Restructure and change to conform to	#
#				AutoTools General Template v1.0.6.	#
# 10/05/2016	MG	1.0.7	Move header files to include directory.	#
# 28/05/2016	MG	1.0.8	Modified following introduction of	#
#				libswocserver library.			#
# 13/06/2016	MG	1.0.9	Use new more unique function name in	#
#				AC_SEARCH_LIBS libswocserver library	#
#				check.					#
# 20/09/2016	MG	1.0.10	Add swocserverd daemon directory.	#
# 02/02/2017	MG	1.1.0	Add library check for libmgesysutils.	#
#				Add header checks for above.		#
#				Add enable-debug option.		#
#				Add libmgec library support.		#
#				Add header checks for mgebuffer,	#
#				mgememory and mgemessage.		#
#				Add library and pkg-config check for	#
#				libswoccommon.				#
#				Add command line controlled systemd	#
#				service build.				#
#				Change to empty CFLAGS here and leave	#
#				setting to the Makefile.am's.		#
#				Add header checks for bstree.h & 	#
#				mge-errno.h				#
# 19/05/2017	MG	1.1.1	Add support for temporary header	#
#				directory.				#
#				Add Makefiles for man 5 pages and bash.	#
# 07/06/2017	MG	1.1.2	Add support for temporary libraries.	#
# 09/09/2017	MG	1.1.3	Correct function name in error string	#
#				for AC_SEARCH_LIBs on libswoccommon.	#
# 20/10/2017	MG	1.1.4	Add search for temporary libraries and	#
#				store result in a variable for AutoMake	#
#				consumption.				#
# 12/01/2018	MG	1.1.5	Add SPDX license tags to source files.	#
# 16/01/2018	MG	1.1.6	Correct generation of config.h		#
#				Update minimum AutoTools version.	#
#				Update AC macros and comments.		#
#				Change to manual package version	#
#				numbering on adoption of project /	#
#				sub-project structure.			#
# 05/02/2018	NG	1.1.7	Change AC_CONFIG_SRCDIR to sensible	#
#				value.					#
# 12/02/2018	MG	1.1.8	Make AutoMake use posix format for tar.	#
# 22/03/2018	MG	1.1.9	Add header check for configfile.h	#
# 24/03/2018	MG	1.1.10	Add support for an enable-sparse CLA	#
#				and passing a suitable variable to the	#
#				Makefile.am's.				#
# 10/05/2018	MG	1.1.11	Add check for stdbool.h			#
# 24/06/2018	MG	1.1.12	Build setup.sh in AC_CONFIG_FILES.	#
#				Build swocserverd.conf in		#
#				AC_CONFIG_FILES.			#
#				Build swocserverd.service in		#
#				AC_CONFIG_FILES.			#
#				Remove no longer needed AC_SUBST	#
#				variable pkgversion.			#
#				AC_DEFINE SWOCSERVER_SOURCE_VERSION for	#
#				use in version.c via config.h		#
#				AC_DEFINE SWOCSERVERD_SOURCE_VERSION	#
#				for use in version.c via config.h	#
# 27/06/2018	MG	1.1.13	Do not use AC_CONFIG_FILES for		#
#				substitution of Installation Directory	#
#				Variables. Reverse build of setup.sh to	#
#				the Makefile. Reverse build of		#
#				swocserverd.service to the Makefile.	#
# 13/08/2018	MG	1.1.14	Add support for an enable-headercheck	#
#				CLA and passing a suitable variable to	#
#				the Makefile.am's.			#
#				Build suitable CFLAGS based on compiler	#
#				version.				#
# 23/09/2018	MG	1.1.15	Remove check for deprecated and		#
#				replaced bzero().			#
# 05/10/2018	MG	1.1.16	Add program check for pkg-config.	#
# 25/05/2019	MG	1.2.1	Re-vamp to new standard layout.		#
#				Remove v from package version number.	#
#				Pass bash path to scripts and Makefiles	#
# 01/06/2019	MG	1.2.2	Add AC_CHECK_DECLS([sys_siglist]).	#
#				Add missing function checks.		#
#				Add missing check for sys/socket.h	#
#				Use standard macro AC_HEADER_STDBOOL 	#
#				in preference to AC_CHECK_HEADERS.	#
# 06/11/2019	MG	1.2.3	Add enable-distcheckfake option.	#
#				Add enable option descriptions in this	#
#				file's header.				#
# 27/03/2020	MG	1.2.4	Add check for arpa/inet.h for use with	#
#				inet_ntop().				#
#				Move the cli-prg source into the	#
#				swocclient sub-directory as the		#
#				directory hierarchy needs to be the	#
#				same accross the source tree for	#
#				temporary libraries to work based on	#
#				the search in configure.ac.		#
# 17/06/2020	MG	1.2.5	Remove check for unused program dia.	#
#				Change libssh check to a new function	#
#				used in this project which has replaced	#
#				a function now deprecated.		#
# 29/10/2020	MG	1.2.6	Replace use of sys_siglist[] with	#
#				strsignal() which is the approved way.	#
#				(sys_siglist has been renamed and	#
#				hidden in more recent glibc's). Add	#
#				check for the function.			#
# 09/08/2021	MG	1.2.7	Add enable-atonly CLA and add support	#
#				for doc-base.				#
# 09/10/2021	MG	1.2.8	Capitalise MG_CFLAGS variable.		#
#				Make use of new CPPFLAGS macro.		#
#				Make use of new linker macro.		#
# 11/10/2021	MG	1.2.9	Rationalise header directory to API and	#
#				internal.				#
# 05/11/2021	MG	1.2.10	Add Canonical target for pthread macro.	#
#				Create debug version of flags where	#
#				necessary.				#
# 08/12/2021	MG	1.2.11	If git is available and we are inside a	#
#				git tree then use git describe to add	#
#				further package version information.	#
#				Tighten SPDX tag.			#
#				Add AC_COPYRIGHT macro.			#
# 06/06/2022	MG	1.2.12	Use new namespace enhanced m4 macros.	#
#				New CL option to enable compiler	#
#				analyzer output.			#
# 17/07/2022	MG	1.2.13	Remove portability restrictions for	#
#				Automake. Allows automatic generation	#
#				of source file depends in the doxygen	#
#				Makefile.am				#
# 28/07/2022	MG	1.3.1	Change doc directory to docs to support	#
#				using GitHub Pages for hosting Doxygen	#
#				documentation.				#
#									#
#########################################################################


#########################################
#					#
# SECTION  1: Initialisation & Setup	#
#					#
#########################################

AC_COPYRIGHT([Copyright (C) 2015-2022  Mark Grant
	      This script is free to copy or distribute under the terms set out
	      in the package license. See COPYING for details.])

AC_REVISION($Revision: 1.3.1 $)

AC_PREREQ(2.69)

#
# The following macro uses git, if available, to provide versioning information.
# The logic is as follows:-
# If git is not available, is not initialised or no commit has been made, then
# the local variable pkg_version will be used.
# If git is available but no Tag has been created then just a commit identifier
# will be used.
# If git is available and a tag has been created then the most recent tag, plus
# number of commits since the tag, plus a commit identifier will be used.
# The package version in ac init will be used to create an output variable which
# can be used by ac output and / or the sed setup below to substitute bookmarks
# in source files.
#
# Set pkg_version for a default hard-coded version number.
m4_define(pkg_version, 1.6.3+rc1)

AC_INIT([Server Wait On Client Package], m4_esyscmd([ \
	git describe --always 1>/dev/null 2>/dev/null \
	&& sed --help 1>/dev/null 2>/dev/null \
	&& git describe --always | sed 's/v//' | tr -d '\n' \
	|| echo pkg_version | tr -d '\n']),
	[m.grant.prg@gmail.com], [swoc])

AC_MSG_NOTICE(initialisation and setup)

AC_SUBST(package_version, $PACKAGE_VERSION)

AC_DEFINE(COMMON_SOURCE_VERSION, "1.1.13", \
	Define to the version of source code for the common library.)

AC_SUBST(common_lib_version_info, "3:5:0")

AC_DEFINE(CLIENT_SOURCE_VERSION, "1.1.10", \
	Define to the version of source code for the client library.)

AC_SUBST(client_lib_version_info, "2:3:0")

AC_DEFINE(SERVER_SOURCE_VERSION, "1.1.12", \
	Define to the version of source code for the server library.)

AC_SUBST(server_lib_version_info, "3:3:0")

AC_DEFINE(SWOCCLIENT_SOURCE_VERSION, "1.1.8", \
	Define to the version of source code for the client program.)

AC_DEFINE(SWOCSERVER_SOURCE_VERSION, "1.1.9", \
	Define to the version of source code for the server program.)

AC_DEFINE(SWOCSERVERD_SOURCE_VERSION, "1.0.12", \
	Define to the version of source code for the server daemon.)

#
# Canonical targets are required by AX_PTHREAD.
#
AC_CANONICAL_TARGET

#
# Passes the options to all am files. Puts objects in the sub-directory
# containing the source. Forces tar to use --format=posix in make dist.
# This also checks :-	BSD install	make variable set	mkdir -p
#			gawk
#
AM_INIT_AUTOMAKE(-Wall -Wno-portability -Werror subdir-objects tar-pax)

#
# Standard macros will be automatically placed in m4.
# Programmer defined macros should be placed in, say, m4extra which allows us
# to exclude the standard macros from being git tracked. This other directory
# must be specified as an include option in the Makefile.am ACLOCAL_AMFLAGS
# e.g.
# ACLOCAL_AMFLAGS = -I m4 -I m4extra
#
AC_CONFIG_MACRO_DIR(m4)


#########################################
#					#
# SECTION  3: Command line arguments	#
#					#
#########################################

AC_MSG_NOTICE(processing command line arguments)

#
# The following allows for an enable-analyzer=yes/no command line option.
# The conditional ANALAYZER is made available to the makefiles.
#
AC_ARG_ENABLE(analyzer,
	AS_HELP_STRING([--enable-analyzer], [build to display analyzer output]),
	[case "${enableval}" in
		yes) analyzer=true ;;
		no)  analyzer=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} in enable-analyzer);;
	esac],analyzer=false)
AM_CONDITIONAL(ANALYZER, test x$analyzer = xtrue)

if test "x${analyzer}" = xtrue; then
	AC_MSG_NOTICE(building to display compiler analyzer output)
fi

#
# The following allows for an enable-atonly=yes/no command line option. The
# conditional ATONLY is made available to the makefiles.
# This allows for a workaround where AutoTools normally has a prefix of
# /usr/local but anything pre-installed would normally be in /usr.
#
AC_ARG_ENABLE(atonly,
	AS_HELP_STRING([--enable-atonly], \
			[build for AT only testing or installation]),
	[case "${enableval}" in
		yes) atonly=true ;;
		no)  atonly=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} for --enable-atonly) ;;
	esac],atonly=false)
AM_CONDITIONAL(ATONLY, test x$atonly = xtrue)

if test "x${atonly}" = xtrue; then
	AC_MSG_NOTICE(building for AT only testing or installing)
	AC_SUBST(docbaseloc,"/usr/share/doc-base")
else
	AC_SUBST(docbaseloc,"$datarootdir/doc-base")
fi

#
# The following allows for an enable-debug=yes/no command line option. The
# conditional DEBUG is made available to the makefiles.
#
AC_ARG_ENABLE(debug,
	AS_HELP_STRING([--enable-debug], [compile for debugging]),
	[case "${enableval}" in
		yes) debug=true ;;
		no)  debug=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;;
	esac],debug=false)
AM_CONDITIONAL(DEBUG, test x$debug = xtrue)

#
# CFLAGS is usually set to -g -O2, but for debugging we will probably
# want -g -Og and for production we will probably want -O2. So empty CFLAGS here
# and leave the selection to the Makefile.am's.
#
: ${CFLAGS=""}

if test "x${debug}" = xtrue; then
	AC_MSG_NOTICE(build in debug mode)
fi

#
# If a file location has to be fixed, e.g. /etc/foo.conf and never
# /usr/local/etc/foo.conf, then make distcheck will fail. Setting this flag
# allows make distcheck to locate the file in an AT standard directory-based
# location.
# eg /usr/local/etc/foo.conf.
# and hence succeed.
#
AC_ARG_ENABLE([distcheckfake],
	AS_HELP_STRING([--enable-distcheckfake], [enable running of distcheck]),
	[case "${enableval}" in
		yes) distcheckfake=true ;;
		no)  distcheckfake=false ;;
		*) AC_MSG_ERROR([bad value ${enableval} for \
			--enable-distcheckfake]) ;;
	esac],[distcheckfake=false])
AM_CONDITIONAL([DISTCHECKFAKE], [test x$distcheckfake = xtrue])

if test "x${distcheckfake}" = xtrue; then
	AC_MSG_NOTICE([using workarounds to facilitate distcheck])
else
	AC_MSG_NOTICE([using distcheck incompatible setup])
fi

#
# The following allows for an enable-headercheck=yes/no command line option.
# The conditional HEADERCHECK is made available to the makefiles.
#
AC_ARG_ENABLE(headercheck,
	AS_HELP_STRING([--enable-headercheck], [build to display header stack]),
	[case "${enableval}" in
		yes) headercheck=true ;;
		no)  headercheck=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} in enable-headercheck);;
	esac],headercheck=false)
AM_CONDITIONAL(HEADERCHECK, test x$headercheck = xtrue)

if test "x${headercheck}" = xtrue; then
	AC_MSG_NOTICE(building to display include stack depth)
fi

#
# The following allows for an enable-sparse=yes/no command line option. The
# conditional SPARSE is made available to the makefiles.
#
AC_ARG_ENABLE(sparse,
	AS_HELP_STRING([--enable-sparse], [build with sparse]),
	[case "${enableval}" in
		yes) sparse=true ;;
		no)  sparse=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} for --enable-sparse);;
	esac],sparse=false)
AM_CONDITIONAL(SPARSE, test x$sparse = xtrue)

if test "x${sparse}" = xtrue; then
	AC_MSG_NOTICE(building using sparse)
fi

#
# The following allows for an enable-systemd=yes/no command line option. The
# conditional SYSD is made available to the makefiles.
#
AC_ARG_ENABLE(systemd,
	AS_HELP_STRING([--enable-systemd], [build for systemd]),
	[case "${enableval}" in
		yes) systemd=true ;;
		no)  systemd=false ;;
		*) AC_MSG_ERROR(bad value ${enableval} for --enable-systemd);;
	esac],systemd=false)
AM_CONDITIONAL(SYSD, test x$systemd = xtrue)

if test "x${systemd}" = xtrue; then
	AC_MSG_NOTICE(building for systemd)
fi


#################################
#				#
# SECTION  4: Compiler setup	#
#				#
#################################

AC_MSG_NOTICE(compiler setup)

# Set language for compilation tests.
AC_LANG(C)

#
# Checks the compiler to use from either a standard list or one to use for
# sparse, stores it in the CC variable and checks its capabilities.
#
if test "x$sparse" = "xtrue" ; then
	compiler_list="cgcc gcc cc"
else
	compiler_list="gcc cc"
fi
AC_PROG_CC($compiler_list)

#
# GNULIB macro. Place just after AC_PROG_CC.
# Also checks for ar, grep, egrep, ranlib and how to run the cpp.
#
gl_EARLY

#
# Checks the c pre-processor to use, stores it in the cpp variable and checks
# its capabilities.
#
AC_PROG_CPP

#
# Checks the c++ compiler to use, stores it in the cxx variable and checks its
# capabilities. (Required by ltmain.sh).
#
AC_PROG_CXX

# GNULIB gl_INIT macro. Must appear after other checks which may affect the
# compiler invocation.
gl_INIT

# Must use this macro if the ar archiver is used. ar is mostly now used for
# static libraries.
AM_PROG_AR

# Check for libtool minimum version.
LT_PREREQ(2.4.6)

# Turn on shared libraries.
# Also checks for fgrep, linker capabilities, ranlib, sed.
# Also checks for:-	ANSI C header files	sys/types.h	sys/stat.h
#			stdlib.h	string.h	memory.h
#			strings.h	inttypes.h	stdint.h
#			unistd.h	dlfcn.h
LT_INIT


#########################################
#					#
# SECTION  5: Check for programs	#
#					#
#########################################

AC_MSG_NOTICE(checking for basic programs that have AutoConf checks)

#
# The small set of tools you can expect to find on any machine.
#
# awk		date		grep		od
# basename	diff		join		rm
# cat		dirname		ln		rmdir
# cc		egrep		ls		sed
# chgrp		expr		mkdir		sleep
# chown		expr (‘|’)	mkfifo		sort
# chmod		expr (‘:’)	mknod		tar
# cmp		fgrep		mktemp		touch
# cp		find		mv		tr
#
# (For further information including limitations to the above, see Section 11.15
# in the GNU AutoConf Manual).
#
# If using am init automake, gl early or lt init the following programs are
# automatically checked:-
#
# am init automake
#	BSD install	mkdir -p	gawk
#
# gl early
#	ar	grep	egrep	ranlib
#
# lt init
#	fgrep	ranlib	sed
#
# Basic and AutoTools program checks.
#
AC_PROG_AWK
AC_PROG_EGREP
AC_PROG_FGREP
AC_PROG_GREP
AC_PROG_LN_S
AC_PROG_MKDIR_P
AC_PROG_SED

AC_MSG_NOTICE(checking for some standard AutoTools required programs)
AC_PROG_INSTALL

AC_MSG_NOTICE(checking for particular programs required by this build)
AC_CHECK_PROG(DOT, dot, yes, no)
if test "x$DOT" = "xno" ; then
	AC_MSG_ERROR(dot not found)
fi

AC_CHECK_PROG(DOXYGEN, doxygen, yes, no)
if test "x$DOXYGEN" = "xno" ; then
	AC_MSG_ERROR(doxygen not found)
fi

AC_CHECK_PROG(TAR, tar, yes, no)
if test "x$TAR" = "xno" ; then
	AC_MSG_ERROR(tar not found)
fi

AC_CHECK_PROG(TXT2MAN, txt2man, yes, no)
if test "x$TXT2MAN" = "xno" ; then
   	AC_MSG_ERROR(txt2man not found)
fi

AC_CHECK_PROG(TXT2MANWRAP, txt2manwrap, yes, no)
if test "x$TXT2MANWRAP" = "xno" ; then
   	AC_MSG_ERROR(txt2manwrap not found)
fi

AC_CHECK_PROG(PKGCONFIG, pkg-config, yes, no)
if test "x$PKGCONFIG" = "xno" ; then
	AC_MSG_ERROR(pkg-config not found)
fi

AC_MSG_NOTICE(checking for particular programs required at build and run time)
AC_PATH_PROG([BASH], [bash], [no])
if test "x$BASH" = "xno" ; then
	AC_MSG_ERROR([bash not found])
fi

AC_MSG_NOTICE(checking for particular programs required at run time)
AC_CHECK_PROG(GETOPT, getopt, yes, no)
if test "x$GETOPT" = "xno" ; then
	AC_MSG_WARN(getopt not found)
fi


#########################################
#					#
# SECTION  6: Check for libraries	#
#					#
#########################################

AC_MSG_NOTICE(checking for libraries required by this application)

AC_SEARCH_LIBS(parsefile, mgesysutils, , \
	AC_MSG_ERROR(function parsefile not found in libmgesysutils))
AC_SEARCH_LIBS(add_bst_node, mgec, , \
	AC_MSG_ERROR(function add_bst_node not found in libmgec))
AC_SEARCH_LIBS(ssh_get_server_publickey, ssh, , \
	AC_MSG_ERROR(function ssh_get_server_publickey not found in libssh))


#########################################
#					#
# SECTION  7: Check for header files	#
#					#
#########################################

AC_MSG_NOTICE(checking for header files required by this application)

AC_CHECK_HEADERS(stdio.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(stdlib.h, , AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(stdarg.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(sysexits.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(signal.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(limits.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(netinet/in.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(netdb.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(sys/socket.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(arpa/inet.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(sys/stat.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(sys/types.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(string.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(ctype.h, , AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(unistd.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(syslog.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(errno.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(getopt.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(libssh/libssh.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(portability.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(mgebuffer.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(mgememory.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(mgemessage.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(bstree.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(mge-errno.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(configfile.h, [], AC_MSG_ERROR(header not found))
AC_CHECK_HEADERS(remsyslog.h, , AC_MSG_ERROR(header not found))

AC_HEADER_STDBOOL


#################################
#				#
# SECTION  8: Check for types	#
#				#
#################################

AC_MSG_NOTICE(checking for types required by this application)

AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T


#########################################################
#							#
# SECTION 10: Check for compiler characteristics	#
#							#
#########################################################

AC_MSG_NOTICE(checking for compiler characteristics)

# Check that the compiler recognizes the restrict keyword.
AC_C_RESTRICT

# Check for threading libraries, compiler and linker flags.
AX_PTHREAD(, AC_MSG_ERROR(thread information not found))

# Build suitable pre-processor, compiler and linker flags.
MG_BUILD_COMPILER_VERSION_CPPFLAGS(MG_CPPFLAGS, MG_DEBUG_CPPFLAGS)
MG_BUILD_COMPILER_VERSION_CFLAGS(MG_CFLAGS, MG_DEBUG_CFLAGS)
MG_BUILD_COMPILER_VERSION_ANALYZER_CFLAGS(MG_ANALYZER_CFLAGS)
MG_BUILD_LDFLAGS(MG_LDFLAGS)


#################################################
#						#
# SECTION 11: Check for library functions	#
#						#
#################################################

AC_MSG_NOTICE(checking for GNU compatible library functions required)

AC_FUNC_FORK
AC_FUNC_MALLOC
AC_CHECK_FUNCS(getpass memset socket strchr strerror strncasecmp strsignal)
AC_CHECK_FUNC(strtol)


#################################################
#						#
# SECTION 12: Check for pkg-config files	#
#						#
#################################################

AC_MSG_NOTICE(checking for pkg-config files)

PKG_CHECK_MODULES(LIBMGESYSUTILS, libmgesysutils, [], \
		AC_MSG_ERROR(pkg-config file not found))
PKG_CHECK_MODULES(LIBMGEC, libmgec, [], \
		AC_MSG_ERROR(pkg-config file not found))
PKG_CHECK_MODULES(LIBSSH, libssh, [], \
		AC_MSG_ERROR(pkg-config file not found))


#################################################
#						#
# SECTION 14: Additional checks and variables	#
#						#
#################################################

AC_MSG_NOTICE(processing additional checks and variables)

#
# Now set up a sed sequence to substitute bookmarks in script files with either
# standard GNU Directory Variables, or with programmer-defined variables set up
# using an AC_SUBST macro above. This sed sequence will be invoked at
# programmer discretion at sub-directory Makefile.am level.
# ----------------------
#
# prefix	/usr/local (typically)
#	exec_prefix	${prefix}
#		bindir	${exec_prefix}/bin	user executables
#		sbindir	${exec_prefix}/sbin	system admin executables
#		libexecdir	${exec_prefix}/libexec	program executables
#		libdir	${exec_prefix}/lib	object code libraries
#	sysconfdir	 ${prefix}/etc	read-only single-machine data
#	sharedstatedir	${prefix}/com	modifiable architecture-independent data
#	localstatedir	${prefix}/var	modifiable single-machine data
#	runstatedir	${localstatedir}/run
#	includedir	${prefix}/include	C header files
#	oldincludedir	$usr/include	C header files
#	datarootdir	${prefix}/share	read-only arch.-independent data root
#		localedir	${datarootdir}/locale	locale-dependent data
#		datadir	${datarootdir}	read-only architecture-independent data
#		mandir	${datarootdir}/man	man documentation
#		infodir	${datarootdir}/info	info documentation
#		docdir	${datarootdir}/doc/${PACKAGE}	documentation root
#			htmldir	${docdir}	html documentation
#			dvidir	${docdir}	dvi documentation
#			pdfdir	${docdir}	pdf documentation
#			psdir	${docdir}	ps documentation
#		lispdir	${datarootdir}/emacs/site-lisp
#	pkgdatadir	${datarootdir}/${PACKAGE}
#	pkgincludedir	${includedir}/${PACKAGE}
#	pkglibdir	${libdir}/${PACKAGE}
#	pkglibexecdir	${libexecdir}/${PACKAGE}
#
AC_SUBST([edit], ["$SED \
		-e 's|@bashlocation[@]|\$(BASH)|g' \
		-e 's|@package_version[@]|\$(package_version)|g' \
		-e 's|@docbaseloc[@]|\$(docbaseloc)|g' \
		-e 's|@prefix[@]|\$(prefix)|g' \
		-e 's|@exec_prefix[@]|\$(exec_prefix)|g' \
		-e 's|@bindir[@]|\$(bindir)|g' \
		-e 's|@sbindir[@]|\$(sbindir)|g' \
		-e 's|@libexecdir[@]|\$(libexecdir)|g' \
		-e 's|@libdir[@]|\$(libdir)|g' \
		-e 's|@sysconfdir[@]|\$(sysconfdir)|g' \
		-e 's|@sharedstatedir[@]|\$(sharedstatedir)|g' \
		-e 's|@localstatedir[@]|\$(localstatedir)|g' \
		-e 's|@runstatedir[@]|\$(runstatedir)|g' \
		-e 's|@includedir[@]|\$(includedir)|g' \
		-e 's|@oldincludedir[@]|\$(oldincludedir)|g' \
		-e 's|@datarootdir[@]|\$(datarootdir)|g' \
		-e 's|@localedir[@]|\$(localedir)|g' \
		-e 's|@datadir[@]|\$(datadir)|g' \
		-e 's|@mandir[@]|\$(mandir)|g' \
		-e 's|@infodir[@]|\$(infodir)|g' \
		-e 's|@docdir[@]|\$(docdir)|g' \
		-e 's|@htmldir[@]|\$(htmldir)|g' \
		-e 's|@dvidir[@]|\$(dvidir)|g' \
		-e 's|@pdfdir[@]|\$(pdfdir)|g' \
		-e 's|@psdir[@]|\$(psdir)|g' \
		-e 's|@lispdir[@]|\$(lispdir)|g' \
		-e 's|@pkgdatadir[@]|\$(pkgdatadir)|g' \
		-e 's|@pkgincludedir[@]|\$(pkgincludedir)|g' \
		-e 's|@pkglibdir[@]|\$(pkglibdir)|g' \
		-e 's|@pkglibexecdir[@]|\$(pkglibexecdir)|g'"])

#
# This creates an AutoConf substitution variable for temporary header files.
# Temporary headers might be copies of -dev package header files brought into a
# library package for development and modification. Whilst there it should be
# used. When finished it must be moved back to the -dev package for release.
# This variable and the inc-tmp/Makefile.am allow for this conditional use and
# even conditional presence in inc-tmp.
# The use of `dirname $0` to get the configure invocation directory is critical
# for parallel builds and make distcheck as the relative path always stays the
# same but configure may be invoked from different directories.
#
AC_SUBST(tmpheaders)

for entry in `dirname $0`/src/prg/c/inc-tmp/*.h
do
	if test -f "$entry" ;then
		tmpheaders=$tmpheaders" "$(basename "$entry")
	fi
done

#
# This creates an AutoConf substitution variable for temporary libraries.
# Temporary libraries are development copies of libraries from other packages
# which are being tested / modified in conjunction with this package.
# This variable can be consumed in the AutoMake XXX_LDADD line.
# The use of `dirname $0` to get the configure invocation directory is critical
# for parallel builds and make distcheck as the relative path always stays the
# same but configure may be invoked from different directories.
#
AC_SUBST(tmplibraries)

for direntry in `dirname $0`/src/prg/c/lib-tmp/*
do
	if test -d "$direntry" ;then
		for entry in `dirname $0`\
/src/prg/c/lib-tmp/$(basename "$direntry")/*.la
		do
			if test -f "$entry" ;then
				tmplibraries=$tmplibraries\
" \$(srcdir)/../../../lib-tmp/"$(basename "$direntry")"/"$(basename "$entry")
			fi
		done
	fi
done

#
# Setup substitution variables for use in the pkg-config file.
#
# The Common library.
AC_SUBST(com_pc_requires)
# LIBMGEC is a Requires AOT Requires.private because it exposes mge_errno.
com_pc_requires="libmgec"

# The Requires.private field
AC_SUBST(com_pc_requires_private)
com_pc_requires_private="libmgesysutils"

# The Client library.
AC_SUBST(cli_pc_requires)
cli_pc_requires="libmgec"

AC_SUBST(cli_pc_requires_private)
cli_pc_requires_private="libmgesysutils libswoccommon"

# The Server library.
AC_SUBST(srv_pc_requires)
srv_pc_requires="libmgec"

AC_SUBST(srv_pc_requires_private)
srv_pc_requires_private="libswoccommon"

# Check a source file to ensure no command line --srcdir typo.
AC_CONFIG_SRCDIR(src/prg/c/src/srv-prg/swocserver/version.c)


#########################################
#					#
# SECTION 15: Specify the output files	#
#					#
#########################################

AC_MSG_NOTICE(processing output files)

#
# Non-Makefile files to generate.
# AC_CONFIG_FILES should not be used to substitute AutoConf Installation
# Directory Variables, (bindir and friends), except in Makefiles and pkg-config
# files. See GNU AutoConf manual section 4.8.2 Installation Directory Variables.
#
AC_CONFIG_FILES(docs/doxygen/Doxyfile)
AC_CONFIG_FILES(src/conf/etc/com-lib/swoc.conf)
AC_CONFIG_FILES(src/conf/etc/srv-prg/swocserverd.conf)
AC_CONFIG_FILES(src/prg/c/pkg-config/cli-dev/libswocclient.pc)
AC_CONFIG_FILES(src/prg/c/pkg-config/com-dev/libswoccommon.pc)
AC_CONFIG_FILES(src/prg/c/pkg-config/srv-dev/libswocserver.pc)

# Causes AC_OUTPUT to create a c preprocessor file with #define statements about
# the configuration.
AC_CONFIG_HEADERS(src/prg/c/gen/inc/config.h)


# Project Makefiles to create.
AC_CONFIG_FILES(Makefile
		docs/doxygen/Makefile
		src/conf/etc/com-lib/Makefile
		src/conf/etc/srv-prg/Makefile
		src/man/3/cli-dev/Makefile
		src/man/3/com-dev/messages/Makefile
		src/man/3/com-dev/ssh/Makefile
		src/man/3/com-dev/tcp/Makefile
		src/man/3/com-dev/validateconfig/Makefile
		src/man/3/srv-dev/Makefile
		src/man/5/com-lib/Makefile
		src/man/5/srv-prg/Makefile
		src/man/8/cli-prg/Makefile
		src/man/8/srv-prg/Makefile
		src/prg/bash/com-lib/Makefile
		src/prg/bash/srv-prg/Makefile
		src/prg/c/gen/lib/Makefile
		src/prg/c/inc/api/Makefile
		src/prg/c/inc/internal/Makefile
		src/prg/c/inc-tmp/Makefile
		src/prg/c/lib-tmp/Makefile
		src/prg/c/pkg-config/cli-dev/Makefile
		src/prg/c/pkg-config/com-dev/Makefile
		src/prg/c/pkg-config/srv-dev/Makefile
		src/prg/c/src/cli-lib/libswocclient/Makefile
		src/prg/c/src/cli-lib/test-optionproc/Makefile
		src/prg/c/src/cli-prg/swocclient/Makefile
		src/prg/c/src/com-lib/libswoccommon/Makefile
		src/prg/c/src/com-lib/test-messages/Makefile
		src/prg/c/src/com-lib/test-ssh/Makefile
		src/prg/c/src/com-lib/test-tcp/Makefile
		src/prg/c/src/com-lib/test-validate/Makefile
		src/prg/c/src/srv-lib/libswocserver/Makefile
		src/prg/c/src/srv-lib/test-optionproc/Makefile
		src/prg/c/src/srv-prg/swocserver/Makefile
		src/prg/c/src/srv-prg/swocserverd/Makefile
		src/systemd/srv-prg/Makefile)

AC_OUTPUT