#!/bin/sh -
temp=`/usr/bin/mktemp -t ka`
havehist=1
if [ "X$1" = "X-h" ]; then
	havehist=0
	shift
fi
srcdir=$1
havejobs=0
if grep '^#define[	 ]*JOBS[	 ]*1' $srcdir/shell.h > /dev/null
then	havejobs=1
fi
exec > externals.c
cat <<\!
/*
 * This file was generated by the mkexternals program.
 */

#include <stdlib.h>
#include "shell.h"
#include "externals.h"

!
awk '/^[^#]/ {if(('$havejobs' || $2 != "-j") && ('$havehist' || $2 != "-h")) \
    print $0}' $srcdir/externals.def | sed 's/-[hj]//' > $temp

const struct externalcmd externalcmd[] = {'
awk '{	for (i = 2 ; i <= NF ; i++) {
		if ($i == "-s") {
			spc = 1;
		} else {
			printf "\t{ \"%s\", %d, %d },\n",  $i, NR-1, spc
			spc = 0;
		}
	}}' $temp
echo '	{ NULL, 0, 0 }
};'

exec > externals.h
cat <<\!
/*
 * This file was generated by the mkexternal program.
 */

#include <sys/cdefs.h>
!
tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ < $temp |
	awk '{	printf "#define %s %d\n", $1, NR-1}'
echo '
struct externalcmd {
      const char *name;
      int code;
      int special;
};

extern int (*const externalfunc[])(int, char **);
extern const struct externalcmd externalcmd[];
'
awk '{	printf "int %s(int, char **);\n", $1}' $temp
rm -f $temp
