AC_INIT(bedmagic)
AC_CONFIG_HEADER(src/defines.h)
. ./settings
if test .$GCC = .
then
	AC_CHECK_PROGS(GCCS,gcc egcs)
	GCC=$GCCS
fi
CC=$GCC
GCCS=$GCC
if test .$CXX = .
then
	AC_CHECK_PROGS(GXXS,g++ egcs++)
	CXX=$GXXS
fi
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXXCPP
AC_PROG_EGREP
#AC_PROG_INSTALL
AC_LANG_C
#OLDLIBS=$LIBS

tmpindex=false
AC_CHECK_FILES(/lib/libcurses.a /usr/lib/libcurses.a,$tmpindex||STATICCURSES=$ac_file;tmpindex=true,)
tmpindex=false
AC_CHECK_FILES(/lib/libcurses.so /usr/lib/libcurses.so,$tmpindex||DYNAMICCURSES=$ac_file;tmpindex=true,)
if test .$CURSESLIB = .
then
	if test .$CONFIG_CURSESSTATIC = .y -a .$STATICCURSES != .
	then
		for CURSESLIB in $STATICCURSES
		do
		break
		done
	else
		CURSESLIB=curses
	fi
fi
AC_CHECK_LIB($CURSESLIB,assume_default_colors,true,AC_DEFINE(NODEFAULTCOLORS))
AC_CHECK_LIB($CURSESLIB,delscreen,AC_DEFINE(HASDELSCREEN),)
AC_CHECK_LIB($CURSESLIB,wresize,AC_DEFINE(HAS_WRESIZE),)
AC_CHECK_LIB($CURSESLIB,tigetstr,AC_DEFINE(HAS_TIGETSTR),)
AC_CHECK_LIB($CURSESLIB,curs_set,AC_DEFINE(HAS_CURS_SET),)
AC_CHECK_LIB($CURSESLIB,start_color,CONFIG_COLOR=y,CONFIG_COLOR=n)
if (ldd $DYNAMICCURSES|grep gpm)
then
	AC_CHECK_FILES(/lib/libgpm.a /usr/lib/libgpm.a,EXTRALIBS=$ac_file,)
#	EXTRALIBS=-lgpm
fi

OLDLIBS=$LIBS
AC_SEARCH_LIBS(dlopen,dl, CONFIG_PLUGIN=y,CONFIG_PLUGIN=n)
LIBS=""
AC_CHECK_LIB(dl,dlopen)
DLLIBS=$LIBS
BEDSRCDIR=`pwd`
AC_SEARCH_LIBS(lseek64,c,CONFIG_LARGEFILES=y;AC_DEFINE(HAS_LSEEK64),CONFIG_LARGEFILES=n)
if test .$CONFIG_LARGEFILES = .n
then
AC_TYPE_OFF_T
AC_TRY_RUN([
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#if STDC_HEADERS
#include <stdlib.h>
#include <stddef.h>
#endif
int main(void) {
	FILE *file=fopen("conftest.off_t","w");
	if(!file)
		exit(2);
	fprintf(file,"%d\n",sizeof(off_t));
	fclose(file);
	exit(0);
}
],SIZEOF_OFF_T=`cat conftest.off_t`;EXTRAFLAGS="$EXTRAFLAGS -DSIZEOF_OFF_T=$SIZEOF_OFF_T" )
if test -n $SIZEOF_OFF_T
then
	if test $SIZEOF_OFF_T -ge 8
	then
	CONFIG_LARGEFILES=y
	fi
fi
fi
AC_EGREP_CPP(yes234,
     [
#include <sys/mman.h>
#if defined(MAP_FAILED)
       yes234
     #endif
     ], AC_DEFINE(HAS_MAP_FAILED))

AC_SEARCH_LIBS(mremap,c,AC_DEFINE(HAS_MREMAP))
AC_SEARCH_LIBS(mkstemp,c,AC_DEFINE(HAS_MKSTEMP))
AC_SEARCH_LIBS(strnlen,c,AC_DEFINE(HAS_STRNLEN))
AC_CHECK_LIB(c,getusershell,AC_DEFINE(HAS_GETUSERSHELL),)
AC_CHECK_LIB(c,strptime,,AC_DEFINE(OWNTRANSTIME))
AC_CHECK_LIB(c,gmtime_r,AC_DEFINE(HAS_GMTIME_R))
AC_CHECK_LIB(c,strtoll,AC_DEFINE(HAS_STRTOLL),)
AC_CHECK_LIB(c,pread,AC_DEFINE(HAS_PREAD),)
AC_CHECK_LIB(c,ttyname_r,AC_DEFINE(HAS_TTYNAME_R),)
AC_CHECK_LIB(c,realpath,AC_DEFINE(HAS_REALPATH),)

AC_CHECK_LIB($CURSESLIB,PDC_chins,AC_DEFINE(USE_KEYPAD) AC_DEFINE(PDCURSES) PDCURSES=1,)
AC_CYGWIN

echo -n 'Checking for tm_gmtoff... '
AC_TRY_COMPILE( 
#include <time.h>
,
 {
struct tm tim;
printf("%d",tim.tm_gmtoff);
}
 ,echo yes,echo no;TIMEZONENEEDED=1; AC_DEFINE(TIMEZONENEEDED))
if test .$TIMEZONENEEDED = .1
then
echo -n 'Checking for int timezone... '
AC_TRY_COMPILE(
#include <time.h>
,
 {
time_t x;
x+=timezone;
}
,echo yes,echo no;AC_DEFINE(OWNTRANSTIME))
fi
echo -n 'Checking if strptime reverses strftime... '
AC_TRY_RUN([
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

static char timeformat[]="%a %b %e %H:%M:%S %C%y";

int main(void) {
	time_t bin;
	struct tm *tim;
	struct tm uit;	
	int maxres=100;
	char formal[maxres];
	char formaluit[maxres];
	bin=time(NULL);
	tim=gmtime(&bin);
 	strftime(formal,maxres,timeformat,  tim);
	memset(&uit,'\0',sizeof(uit));
	strptime(formal, timeformat, &uit);
 	strftime(formaluit,maxres,timeformat,  &uit);
	if(strcmp(formaluit,formal)) {
//		printf("Different timeformat\n");
		exit(3);		
		}
	else {
//		printf("Same timeformat\n");
		exit(0);
		}
	}

] , echo  yes,echo no;AC_DEFINE(OWNTRANSTIME))


LIBS=$OLDLIBS
AC_SEARCH_LIBS(re_search,gnuregex,AC_DEFINE(HAS_REGEX_LIB),OBJECTS="$OBJECTS regex.o")


OLDLIBS=$LIBS
LIBS=""
AC_SEARCH_LIBS(pthread_create,pthread c_r,CONFIG_THREADS=y,CONFIG_THREADS=n)
if test $CONFIG_THREADS = y
then
AC_TRY_RUN([
#include <signal.h>
#include <setjmp.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

sigjmp_buf  jmp;
void sighand(int sig) {
	signal(SIGINT,sighand);
	siglongjmp(jmp, 4);
	}
void main(void) {
int ret=0;
sigset_t set;
pid_t pid=getpid();
	printf("Checking for longjump from interrupt more than once... ");
	signal(SIGINT,sighand);
	if(sigsetjmp(jmp,2)==4) {
		ret++;
		if(ret>4) {
			puts("yes");
			exit( 0);
			}
		}
	kill(pid,SIGINT);
	puts("no");
	exit( 4);
	}
], ,AC_DEFINE(UNBLOCKSIGNALS))
AC_TRY_RUN([

#include <stdio.h>
     #include <sys/types.h>

#include <unistd.h>
#include <signal.h>
#include <pthread.h>
pid_t pid, childpid,parentpid;
int start=-1;
void *one,*two;
pthread_t th_a,th_b;
void * thread1(void *arg) {

int j,i;
	pid=getpid();
        sigset_t sigs;
        sigemptyset(&sigs);
        sigaddset(&sigs,SIGCONT);
        sigaddset(&sigs,SIGTSTP);
         pthread_sigmask(SIG_BLOCK,&sigs,NULL);
printf(".");
for(;;) {
	if(start>=0) {
		sleep(1);
		fflush(stdout);
		 while(start<0xFFFFF)
			start++;
		printf(".");
		kill(parentpid,SIGUSR1);
		exit(0);
		}
	sleep(1);
	};
}
void *thread2(void *arg) {
unsigned int j,i;
	pid=getpid();
start=0;
raise(SIGTSTP);
//pthread_kill(pthread_self() , SIGTSTP);
//printf("start %d\n",start);
fflush(stdout);
	while(getchar()!='q') {
		printf("char\n");
		};
	}
void sighandler(int sig) {
	kill(childpid,SIGKILL);
	puts(" yes");	
	exit(0);
	}
void main(void) {
signal(SIGUSR1,sighandler);
childpid=fork();
if(childpid)   { /*parent*/
        sigset_t sigs;
        sigemptyset(&sigs);
        sigaddset(&sigs,SIGCONT);
        sigaddset(&sigs,SIGTSTP);
pthread_sigmask(SIG_BLOCK,&sigs,NULL);
        // sigprocmask(SIG_BLOCK,&sigs,NULL);
	printf("Checking if SIGSTOP and threads work together..");
	fflush(stdout);
	sleep(10);
	kill(childpid,SIGKILL);
	printf(" no\n");
	exit(2);
}
else { /*child */
	usleep(1);
	sigset_t mask;
	pid=getpid();
	parentpid=getppid();
	pthread_create(&th_a, NULL, thread1, one);
	thread2(two);
	}
}

], ,AC_DEFINE(NOSTOPANDTHREADS))

AC_TRY_COMPILE(
#include <pthread.h>
, {
	pthread_mutexattr_t mut;
	pthread_mutexattr_init(&mut);
	pthread_mutexattr_settype(&mut,PTHREAD_MUTEX_ERRORCHECK);
	}
,AC_DEFINE(MY_PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_ERRORCHECK))
AC_TRY_COMPILE(
#include <pthread.h>
,
 {
	pthread_mutexattr_t mut;
	pthread_mutexattr_init(&mut);
	pthread_mutexattr_settype(&mut,PTHREAD_MUTEX_ERRORCHECK_NP);
	}
,AC_DEFINE(MY_PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_ERRORCHECK_NP))

AC_SEARCH_LIBS(sem_wait,pthread c_r,AC_DEFINE(HAS_SEMAPHORE))
AC_SEARCH_LIBS(sem_getvalue,pthread c_r,AC_DEFINE(HAS_GETVALUE))
fi

THREADSLIB=$LIBS
LIBS=$OLDLIBS


echo -n 'checking for sysconf(_SC_AVPHYS_PAGES)... '
AC_TRY_COMPILE( 
#include <unistd.h>
,
long psize;
long av;
long max;
psize=sysconf(_SC_PAGESIZE);
max=sysconf(_SC_PHYS_PAGES);
av=sysconf(_SC_AVPHYS_PAGES);
exit(0);
 ,echo yes;AC_DEFINE(SYSCONF_MEM),echo no)

#AC_CHECK_FILE(/proc/meminfo,AC_DEFINE(HAS_MEMINFO),)
echo -n 'Checking /proc/meminfo... '
if test -e /proc/meminfo && grep -q 'MemFree:' /proc/meminfo  && grep -q 'Cached:' /proc/meminfo  && grep -q 'Buffers:' /proc/meminfo  
then
echo Useful
AC_DEFINE(HAS_MEMINFO)
fi
THISPID=$$


AC_CHECK_FILE(/proc/$THISPID/file,AC_DEFINE(PROCEXEFILENAMEFORMAT,"/proc/%d/file"))
AC_CHECK_FILE(/proc/$THISPID/exe,AC_DEFINE(PROCEXEFILENAMEFORMAT,"/proc/%d/exe"))

AC_SEARCH_LIBS(log,m)
RXVTS="rxvt rxvt.bin rxvt.sh rxvt-xpm rxvt-xterm"
RXVT=""
NRXVT=""
CONFIG_XBED=n
if test .$PDCURSES != .1
then
for i in $RXVTS
do
	if $i -h 2>&1 | grep -qi menubar
	then
		CONFIG_XBED=y
		RXVT="$RXVT $i"
	else
		NRXVT="$NRXVT $i"
	fi
done
if test .$CONFIG_XBED != .y
then
		RXVT=rxvt
		AC_MSG_WARN(****XBED NEEDS RXVT WITH MENUBAR**, see rxvt -h)
fi
fi
AC_CHECK_PROG(CONFIG_PROGMAN,update-menus,y,n)
#AC_CHECK_PROG(HAS_MAKEDEPEND,makedepend,y,n)
AC_CHECK_PROG(HAS_FILE,file,y,n)
AC_CHECK_PROG(HAS_NROFF,nroff,y,n)
if test .$HAS_FILE = .y
then
	if file `which file`|grep -q '\<ELF\>'
	then
		USES_ELF=y
	else
		USES_ELF=n
	fi
fi
SYSTEM=unknown
if  which uname
then
 SYSTEM=`uname -s`
fi
#if test .$SYSTEM = .Linux
#then
#	AC_DEFINE(PROCARRAY)
#fi
AC_PROG_LN_S
AC_PATH_XTRA
echo nox: $no_x
if test .$no_x = .yes
then
	HAS_X=n
else
	HAS_X=y
fi
AC_LANG_CPLUSPLUS
echo -n 'Checking for set_new_handler... '
AC_TRY_RUN([
#include <new.h>
void mynewhandler(void) {
	}
int main(void)
 {
set_new_handler(mynewhandler);
exit(0);
}
],echo yes;AC_DEFINE(HAS_SETNEWHANDLER),echo no)
AC_LANG_C
AC_CHECK_HEADERS(gnuregex.h regex.h)
AC_CHECK_HEADER(features.h,,echo fea;EXTRAFLAGS="$EXTRAFLAGS -I$BEDSRCDIR/utils")
AC_CHECK_HEADER(alloca.h,,echo allo;EXTRAFLAGS="$EXTRAFLAGS -I$BEDSRCDIR/x11")
AC_CHECK_HEADER(float.h,AC_DEFINE(HAVE_FLOAT_H))
AC_SUBST(HAS_X)
AC_SUBST(X_CFLAGS)
AC_SUBST(X_EXTRA_LIBS)
AC_SUBST(X_PRE_LIBS)
AC_SUBST(X_LIBS)
AC_SUBST(SYSTEM)
AC_SUBST(USES_ELF)
AC_SUBST(HAS_FILE)
#AC_SUBST(HAS_MAKEDEPEND)
AC_SUBST(BEDSRCDIR)
AC_SUBST(CONFIG_XBED)
AC_SUBST(RXVT)
AC_SUBST(NRXVT)
AC_SUBST(CONFIG_PROGMAN)
AC_SUBST(CONFIG_PLUGIN)
AC_SUBST(CONFIG_THREADS)
AC_SUBST(CONFIG_LARGEFILES)
AC_SUBST(CONFIG_COLOR)
AC_SUBST(DYNAMICCURSES)
AC_SUBST(STATICCURSES)
AC_SUBST(CURSESLIB)
AC_SUBST(EXTRALIBS)
AC_SUBST(VERSION)
AC_SUBST(GCCS)
AC_SUBST(CXX)
AC_SUBST(LIBS)
AC_SUBST(PROCEXEFILENAMEFORMAT)
AC_SUBST(DLLIBS)
AC_SUBST(THREADSLIB)
AC_SUBST(EXTRAFLAGS)
AC_SUBST(OBJECTS)
AC_SUBST(CYGWIN)
AC_SUBST(PDCURSES)
AC_OUTPUT(config pkg/Makefile package/README)

