1#! /bin/sh
2:
3# Copyright (c) 1992-1999, S.R. van den Berg, The Netherlands
4#$Id: initmake,v 1.71 2000/09/28 01:23:03 guenther Exp $
5
6if test -z "$IFS"
7then IFS=" \
8	\
9
10"
11  export IFS
12fi
13
14PATH=.:$PATH
15BSHELL=$1
16
17O="cd ."
18if $O
19then
20:
21else
22  echo "Your BSHELL=$BSHELL seems to be severely handicapped."
23  case "$BSHELL" in
24     *zsh*) ;;
25     *) echo "(If I wouldn't know better, I'd say you're trying to sell"
26	echo "me an incompatible zsh :-)" ;;
27  esac
28  echo "Please edit the Makefile and try setting BSHELL to a shell which"
29  echo "is more backwards compatible with a Bourne, Korn or POSIX shell."
30  exit 1
31fi
32
33shift; MSHELL=$1
34shift; RM="$1"
35shift; MV="$1"
36shift; LN="$1"
37shift; SEARCHLIBS="$1"
38shift; LIBPATHS="$1"
39shift; DEVNULL=$1
40shift; MAKE="$1"
41shift; O=$1
42shift; CC="$1"
43shift; CFLAGS1="$1"
44shift; LDFLAGS1="$1"
45shift; BINSS="$1"
46shift; MANS1S="$1"
47shift; MANS5S="$1"
48shift; SUBDIRS="$1"
49shift; VISIBLE_BINDIR="$1"
50shift; STRIP="$1"
51
52test 1 != $# &&
53 echo "Don't start this script directly, use \`make init'" && exit 1
54
55test -z "$MSHELL" || SHELL=$MSHELL
56case "$SHELL" in
57  *sh*)
58     case "$SHELL" in
59	*[zc]sh*) echo "Warning: really perverted make detected"; SHELL="";;
60     esac
61     if test ! -z "$SHELL"
62     then
63	$SHELL <<HERE
64	exec 2>$DEVNULL
65	echo test >src/_autotst
66	echo test >src/_autotst
67HERE
68	if test $? = 1
69	then
70	   echo "Warning: your $SHELL has noclobber turned on, avoiding it"
71	   SHELL=""
72	fi
73     fi
74     $RM src/_autotst;;
75  *) echo "Warning: perverted make detected"; SHELL="";;
76esac
77test -z "$SHELL" && SHELL=$BSHELL
78
79export SHELL PATH
80
81FGREP="fgrep"		# POSIX, or not POSIX, that is the question...
82if test \^hello = "`echo '^hello' | grep -F '^hello' 2>&1`"
83then FGREP="grep -F"				# and POSIX it is!
84fi
85
86echo hi | $FGREP hi >$DEVNULL
87t=$?
88echo ho | $FGREP hi >$DEVNULL
89f=$?
90if test 0 != $t -o 0 = $f
91then
92  echo "Your \"$FGREP\" program seems to be incapable of returning a proper"
93  echo "exitvalue depending on the success of the search.  This script can"
94  echo "not work without it."
95  exit 2
96fi
97
98case a in
99  [!a]) classok=no ;;		# This shell is *very* old
100  *) classok=yes ;;
101esac
102
103if test ! -z "$LD_LIBRARY_PATH"
104then
105  echo '***************************** WARNING *********************************'
106  echo '* You seem to have set the LD_LIBRARY_PATH variable, this might cause *'
107  echo '* some trouble during the compilation of this package.	If the make   *'
108  echo '* does not finish by itself, do a: "make clean", clear		      *'
109  echo '* LD_LIBRARY_PATH from the environment, and start over.		      *'
110  echo '***************************** WARNING *********************************'
111fi
112
113cd src				# diving into the source directory ######
114
115newln=""
116$RM _autotst.rrr ../man/core_ln
117echo hi >_autotst.rrr
118if $LN ../src/_autotst.rrr ../man/core_ln 2>$DEVNULL
119then
120:
121else
122   $RM ../man/core_ln
123   $LN -s ../src/_autotst.rrr ../man/core_ln && LN="$LN -s" && newln=yes
124fi
125
126cat >_autotst.c <<HERE
127main()
128{ return 0;
129}
130HERE
131$RM _autotst.rrr _autotst.$O _autotst ../man/core_ln
132
133dostrip=no
134
135case "$LDFLAGS1" in
136  *-s*)
137     LDF=""
138     for a in __dummy__ $LDFLAGS1
139     do
140       if test __dummy__ != $a
141       then
142	  if test X-s = X$a
143	  then
144	     dostrip=yes
145	  else
146	     LDF="$LDF $a"
147	  fi
148       fi
149     done
150     LDFLAGS1="$LDF";;
151esac
152
153cc=""
154
155for a in "$CC" cc gcc
156do
157  echo $a $CFLAGS1 _autotst.c -o _autotst $LDFLAGS1 >>_autotst.rrr
158  test -z "$cc" -a ! -z "$a" &&
159   (exec >>_autotst.rrr 2>&1; $a $CFLAGS1 _autotst.c -o _autotst $LDFLAGS1) &&
160   cc="$a"
161  echo "::::" >>_autotst.rrr
162done
163if test -z "$cc"
164then
165  echo 2>&1 "Whoeaaa!  There's something fishy going on here."
166  echo 2>&1 "You have a look and see if you detect anything uncanny:"
167  echo 2>&1 "*******************************************************"
168  cat 2>&1 _autotst.rrr
169  echo 2>&1 "*******************************************************"
170  echo 2>&1 "I suggest you take a look at the definition of CFLAGS* and CC"
171  echo 2>&1 "in the Makefile before you try make again."
172  exit 1
173fi
174$RM _autotst.rrr _autotst.$O _autotst
175echo "$cc seems to work fine, using that as the C-compiler"
176
177if test $dostrip = yes
178then
179  if $cc $CFLAGS1 _autotst.c -o _autotst $LDFLAGS1 -s >$DEVNULL 2>&1
180  then
181     LDFLAGS1="$LDFLAGS1 -s"
182     dostrip=no
183     STRIP=""
184  else
185     $cc $CFLAGS1 _autotst.c -o _autotst $LDFLAGS1 >$DEVNULL 2>&1
186     if test ! -z "$STRIP" && $STRIP _autotst >$DEVNULL 2>&1
187     then
188	:
189     else
190	STRIP=""
191     fi
192  fi
193  $RM _autotst.rrr _autotst.$O _autotst
194else
195  STRIP=""
196fi
197
198
199cat >_autotst.c <<HERE
200#include <sys/types.h>
201#include <stdio.h>
202#include <sys/stat.h>
203main()
204{ struct stat buf;return!&buf;
205}
206HERE
207
208CFLAGS=""
209
210case "$CFLAGS1" in
211  *-D_POSIX_SOURCE*);;
212  *)
213     if $cc -c $CFLAGS1 _autotst.c >$DEVNULL 2>&1
214     then
215     :
216     else
217	$RM _autotst.$O
218	$cc -c $CFLAGS1 -D_POSIX_SOURCE _autotst.c >$DEVNULL 2>&1 &&
219	 CFLAGS=" -D_POSIX_SOURCE"
220     fi;;
221esac
222
223LDFLAGSC=""
224
225if test -f _autotst.$O || $cc -c $CFLAGS1 $CFLAGS _autotst.c >_autotst.rrr 2>&1
226then
227:
228else
229  echo 2>&1 "Whoeaaa!  There's something fishy going on here."
230  echo 2>&1 "You have a look and see if you detect anything uncanny:"
231  echo 2>&1 "*******************************************************"
232  cat 2>&1 _autotst.rrr
233  echo 2>&1 "*******************************************************"
234  echo 2>&1 "I suggest you take a look at the definition of CC and CFLAGS*"
235  echo 2>&1 "in the Makefile before you try make again."
236  echo 2>&1 "Also: write me a mail showing the errorlog you just generated."
237  echo 2>&1 "The errorlog can still be found in src/_autotst.rrr"
238  echo 2>&1 "It would be helpful if you could mention what machine and OS"
239  echo 2>&1 "you are trying to compile this on (uname -a).  Thanks."
240  exit 1
241fi
242$cc $CFLAGS1 $CFLAGS _autotst.$O -o _autotst $LDFLAGS1 -lc >_autotst.rrr 2>&1 \
243 && if grep "[\"']c['\"]" _autotst.rrr >$DEVNULL ||
244   $FGREP libc _autotst.rrr >$DEVNULL
245  then
246  :
247  else
248     LDFLAGSC=" -lc"
249  fi
250
251LDFLAGS="$SEARCHLIBS"
252firstrun=yes
253checkpath=no
254runcombination=no
255
256while $RM _autotst
257 $cc $CFLAGS1 $CFLAGS _autotst.$O -o _autotst $LDFLAGS1 $LDFLAGS \
258  $LDFLAGSC >_autotst.rrr 2>&1 || firstrun=yes
259 test $firstrun = yes -o ! -f _autotst
260do
261  if test $runcombination = yes
262  then
263     LDFLAGS=""
264     read NEWLDFLAGS 0>&9
265     echo 2>&1 " ...trying $NEWLDFLAGS"
266  else
267     set dummy $LDFLAGS
268     shift
269     echo 2>&1 "	...scanning for $# libraries..."
270     NEWLDFLAGS=""
271  fi
272  for lib in `echo _dummy_ $LDFLAGS | sed -e s/-l//g`
273  do
274     if test _dummy_ != $lib
275     then
276	a=-l$lib
277	 ( exec <_autotst.rrr
278	   while read b
279	   do
280	      if test $classok = yes
281	      then
282		 case ":$b:" in
283		    *$cc*_autotst.$O*) ;;
284		    *"not used for resolving"*) ;;
285		    *lib$lib[!a-z_]*|*$a[!a-z_]*|*[!a-z]$lib[!a-z_]*) exit 1;;
286		 esac
287	      else
288		 case ":$b:" in
289		    *$cc*_autotst.$O*) ;;
290		    *"not used for resolving"*) ;;
291		    *) for c in $b
292		       do
293			  case ":$c:" in
294			     *lib$lib[a-z_]*|*$a[a-z_]*|\
295			      *[a-z]$lib*|*$lib[a-z_]*) ;;
296			     *$a*|*$lib*) exit 1;;
297			  esac
298		       done ;;
299		 esac
300	      fi
301	   done
302	   exit 0
303	 )
304	if test $? = 0
305	then
306	   found=yes
307	   if test $checkpath = yes
308	   then
309	      OLDIFS="$IFS"; IFS=":$IFS"
310	      found=no
311	      for libpath in $LIBPATHS $LD_LIBRARY_PATH
312	      do
313		 set $libpath/*lib$lib[A-Z.]*
314		 test -f $1 && found=yes
315	      done
316	      IFS="$OLDIFS"
317	   fi
318	   test yes = $found && NEWLDFLAGS="$NEWLDFLAGS $a"
319	fi
320     fi
321  done
322  if test a"$LDFLAGS" = a"$NEWLDFLAGS"
323  then
324     if test $checkpath = yes
325     then
326	if test $runcombination = yes
327	then
328	   echo 2>&1 "Whoeaaa!	There's something fishy going on here."
329	   echo 2>&1 "You have a look and see if you detect anything uncanny:"
330	   echo 2>&1 "*******************************************************"
331	   echo \ $cc $CFLAGS1 $CFLAGS _autotst.$O -o _autotst $LDFLAGS1\
332	    $LDFLAGS >>_autotst.rrr
333	   echo "	$LDFLAGSC" >>_autotst.rrr
334	   cat 2>&1 _autotst.rrr
335	   echo 2>&1 "*******************************************************"
336	   echo 2>&1 \
337       "I suggest you take a look at the definition of LDFLAGS* and SEARCHLIBS"
338	   echo 2>&1 "in the Makefile before you try make again."
339	   echo 2>&1 \
340	    "Also: write me a mail showing the errorlog you just generated."
341	   echo 2>&1 "The errorlog can still be found in src/_autotst.rrr"
342	   echo 2>&1 \
343	    "It would be helpful if you could mention what machine and OS"
344	   echo 2>&1 "you are trying to compile this on (uname -a).  Thanks."
345	   exit 1
346	fi
347	echo 2>&1 "Inconsistent libraries found, trying to accomodate..."
348	runcombination=yes
349	cat >_autotst <<\HERE
350#!/bin/sh
351
352exec awk '
353 { split($0,lib);
354   nf=0;
355   for(a in lib)
356      nf++;
357   p[0]=nf;
358   while(!p[nf-1])
359    { for(j=i=0;p[i];i++)
360	 while(++j<p[i])
361	    printf(" %s",lib[j]);
362      while(++j<=nf)
363	 printf(" %s",lib[j]);
364      printf("\n");
365      for(k=0;;k++)
366       { if(!p[k])
367	  { p[k]=nf;
368	    break;
369	  }
370	 if(p[k]!=k+1)
371	  { --p[k];
372	    break;
373	  }
374       }
375      while(--k>=0)
376	 p[k]=p[k+1]-1;
377    }
378   exit;
379 }'
380HERE
381	chmod 755 _autotst
382	echo " $LDFLAGS" | ./_autotst >_autotst.lst
383	echo "" >>_autotst.lst
384	exec 8<&0 <_autotst.lst 9<&0 0<&8 8<&-
385	read NEWLDFLAGS 0<&9
386	echo 2>&1 " ...trying $NEWLDFLAGS"
387     fi
388     checkpath=yes
389  else
390     checkpath=no
391  fi
392  LDFLAGS="$NEWLDFLAGS"
393  firstrun=no
394done
395
396$RM _autotst.$O _autotst.c _autotst _autotst.rrr _autotst.lst
397cd ..			# returning to the main procmail directory ######
398
399LDFLAGS="$LDFLAGS$LDFLAGSC"
400
401test -z "$CFLAGS" || echo "Added CFLAGS=$CFLAGS"
402test -z "$LDFLAGS" || echo "Added LDFLAGS=$LDFLAGS"
403if test $dostrip = yes
404then
405  LDFLAGS="$LDFLAGS1$LDFLAGS"
406  echo "Evaded -s in LDFLAGS1"
407else
408  LDFLAGS="\$(LDFLAGS1)$LDFLAGS"
409fi
410
411for a in $SUBDIRS
412do
413  if test ! -f $a/Makefile.init
414  then
415     case "$LN" in
416	ln|*/ln|link|*/link) $LN $a/Makefile $a/Makefile.init
417	   $RM $a/Makefile
418	   $LN $a/Makefile.init $a/Makefile ;;
419	*) cp $a/Makefile $a/Makefile.init ;;
420     esac
421  fi
422done
423
424test -f Makefile.0 || sed -e '/^# Makefile - mark/,$ !d' <Makefile >Makefile.0
425sed -e '/^# Makefile - mark/,$ d' <Makefile >_Makefile
426echo "# Makefile.1 - mark, don't (re)move this, a sed script needs it
427" >>_Makefile
428
429test a$SHELL != a$MSHELL && echo  "SHELL	= $SHELL"	>>_Makefile
430echo "FGREP	= $FGREP"				>>_Makefile
431echo "STRIP	= $STRIP"				>>_Makefile
432test -z "$newln" || echo "LN	= $LN"			>>_Makefile
433test -z "$MAKE" && echo "MAKE	= make"			>>_Makefile
434test a"$cc" != a"$CC" && echo "CC	= $cc"		>>_Makefile
435
436echo "CFLAGS	= \$(CFLAGS1)$CFLAGS"			>>_Makefile
437echo "LDFLAGS	= $LDFLAGS"				>>_Makefile
438echo							>>_Makefile
439
440MANSS=""
441MANS1=""
442MANS5=""
443MANS=""
444NMANS=""
445BINS=""
446NBINS=""
447for a in $MANS1S
448do
449  MANSS="$MANSS $a.1"
450  MANS1="$MANS1 $a.\$(MAN1SUFFIX)"
451done
452for a in $MANS5S
453do
454  MANSS="$MANSS $a.5"
455  MANS5="$MANS5 $a.\$(MAN5SUFFIX)"
456done
457for a in $MANSS
458do
459  MANS="$MANS new/$a"
460  NMANS="$NMANS ../new/$a"
461done
462for a in $BINSS
463do
464  BINS="$BINS new/$a"
465  NBINS="$NBINS ../new/$a"
466done
467
468echo "BINS=$BINS"					>>_Makefile
469echo "MANS=$MANS"					>>_Makefile
470echo "MANS1=$MANS1"					>>_Makefile
471echo "MANS5=$MANS5"					>>_Makefile
472echo "MANSS=$MANSS"					>>_Makefile
473echo "NBINS=$NBINS"					>>_Makefile
474echo "NMANS=$NMANS"					>>_Makefile
475echo							>>_Makefile
476
477for a in $SUBDIRS
478do
479  sed -e '1,/^# Makefile.0 - mark/ d' <_Makefile >$a/_Makefile
480  cat $a/Makefile.0 >>$a/_Makefile
481done
482
483echo "VISIBLE_BINDIR=$VISIBLE_BINDIR"			>>src/_Makefile
484
485for a in $BINSS
486do
487  echo							>>src/_Makefile
488  echo "../new/$a: $a ../config.check"			>>src/_Makefile
489  echo "	@test -d ../new || mkdir ../new"	>>src/_Makefile
490  echo "	@\$(RM) \$@"				>>src/_Makefile
491  echo "	\$(LN) ../src/$a \$@"			>>src/_Makefile
492done
493
494for a in $MANSS
495do
496  echo							>>man/_Makefile
497  echo "../new/$a: $a ../config.check"			>>man/_Makefile
498  echo "	@test -d ../new || mkdir ../new"	>>man/_Makefile
499  echo "	@\$(RM) \$@"				>>man/_Makefile
500  echo "	\$(LN) ../man/$a \$@"			>>man/_Makefile
501done
502
503for a in $MANS1S
504do
505  echo							>>man/_Makefile
506  echo "$a.1: $a.man man.sed mansed"			>>man/_Makefile
507  echo \
508   "	\$(SHELL) ./mansed \$(SHELL) $a.man \$@ \"\$(RM)\" \$(DEVNULL)" \
509   >>man/_Makefile
510done
511
512for a in $MANS5S
513do
514  echo							>>man/_Makefile
515  echo "$a.5: $a.man man.sed mansed"			>>man/_Makefile
516  echo \
517   "	\$(SHELL) ./mansed \$(SHELL) $a.man \$@ \"\$(RM)\" \$(DEVNULL)" \
518   >>man/_Makefile
519done
520
521cat Makefile.1 >>_Makefile
522$MV _Makefile Makefile
523
524sleep 1			# Some machines are just too speedy, make gets confused
525
526for a in $SUBDIRS
527do
528  echo "#" >>$a/_Makefile
529  $MV $a/_Makefile $a/Makefile
530done
531