#! /bin/sh
# Installation script for less.
# This script prompts the operator for various information
# and constructs a makefile.

echo "This script will build a makefile for less."
echo "If you already have a file called \"makefile\" it will be overwritten,"
echo "as will the file \"defines.h\"."
echo "Press RETURN to continue."
read ans

echo "I will ask you some questions about your system."
echo "If you do not know the answer to any question,"
echo "just press RETURN and I will choose a default for you."
echo "Press RETURN now."
read ans

ECHO=./vecho
if [ ! -f $ECHO ]
then
	echo "One moment..."
	cc -o $ECHO vecho.c
	echo ""
fi

$ECHO "Most Unix systems are derived from either System V"
$ECHO "or Berkeley BSD 4.1, 4.2, 4.3, etc."
$ECHO ""
$ECHO "Is your system closest to:"
$ECHO "  1. System V"
$ECHO "  2. BSD 4.1"
$ECHO "  3. BSD 4.2 or later"
$ECHO "  4. Xenix"
$ECHO "Enter a number, or just RETURN if you don't know: \c"
read ans
xenix=0
case "X$ans" in
X1) sys=sys5; sysname="System V" ;;
X2) sys=bsd; bsd41=1; sysname="BSD 4.1" ;;
X3) sys=bsd; bsd41=0; sysname="BSD 4.2" ;;
X4) sys=sys5; xenix=1; sysname="Xenix" ;;
*) sys=unknown ;;
esac
$ECHO ""

DATE=`date`
cat >makefile <<EOF
# Makefile for "less"
# Generated $DATE by $0.
EOF

cat >>makefile <<"EOF"
#
# Invoked as:
#	make all
#   or	make install
# Plain "make" is equivalent to "make all".
#
# If you add or delete functions, remake funcs.h by doing:
#	make newfuncs
# This depends on the coding convention of function headers looking like:
#	" \t public <function-type> \n <function-name> ( ... ) "
#
# Also provided:
#	make lint	# Runs "lint" on all the sources.
#	make clean	# Removes "less" and the .o files.
#	make clobber	# Pretty much the same as make "clean".

SHELL = /bin/sh

EOF

cat >defines.h <<EOF
/* Definition file for less */
/* Generated $DATE by $0. */

EOF

cat >>defines.h <<EOF
/*
 * Define XENIX if running under XENIX 3.0.
 */
#define	XENIX		$xenix

EOF
$ECHO ""



if [ "X$sys" = "Xunknown" ]
then
	alldefault=0
else
	def=yes
	alldefault=1
	$ECHO "Do you want to use ALL the defaults for $sysname?"
	$ECHO "  Enter \"yes\" if you have a STANDARD $sysname."
	$ECHO "  Enter \"no\" if you want to change any of the defaults. [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) alldefault=1 ;;
	X[nN]*) alldefault=0 ;;
	esac
	$ECHO ""
fi

if [ $alldefault = 0 ]
then
	alloptional=0
else
	def=yes
	alloptional=1
	$ECHO "Do you want to use all the optional features of less?"
	$ECHO "  Less has several features which you may or may not"
	$ECHO "  wish to include, such as shell escapes."
	$ECHO "  Enter \"yes\" if you want to include ALL the optional features."
	$ECHO "  Enter \"no\" if you want to select individual features. [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) alloptional=1 ;;
	X[nN]*) alloptional=0 ;;
	esac
	$ECHO ""
fi



def=yes
x=1
if [ $alldefault = 0 ]
then
	$ECHO "Does your C compiler support the \"void\" type? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * VOID is 1 if your C compiler supports the "void" type,
 * 0 if it does not.
 */
#define	VOID		$x

EOF



def=yes
x="void *"
if [ $alldefault = 0 ]
then
	$ECHO "Does your C compiler support the \"void *\" type? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x="void *" ;;
	X[nN]*) x="char *" ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * VOID_POINTER is the definition of a pointer to any object.
 */
#define	VOID_POINTER	$x

EOF



def=long
if [ $alldefault = 0 ]
then
	$ECHO "What type is the \"offset\" argument to lseek? [$def] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		def=$ans
	fi
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * offset_t is the type which lseek() returns.
 * It is also the type of lseek()'s second argument.
 */
#define	offset_t	$def

EOF




def=yes; x=1
if [ $alldefault = 0 ]
then
	$ECHO "Most Unix systems provide the stat() function."
	$ECHO "Does your system have stat()? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * STAT is 1 if your system has the stat() call.
 */
#define	STAT		$x

EOF




def=yes; x=1
if [ $alldefault = 0 ]
then
	$ECHO "Most Unix systems provide the perror() function."
	$ECHO "Does your system have perror()? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * PERROR is 1 if your system has the perror() call.
 * (Actually, if it has sys_errlist, sys_nerr and errno.)
 */
#define	PERROR		$x

EOF




def=yes; x=1
if [ $alldefault = 0 ]
then
	$ECHO "Most Unix systems provide the time() function."
	$ECHO "Does your system have time()? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * GET_TIME is 1 if your system has the time() call.
 */
#define	GET_TIME	$x

EOF

if [ $x = 0 ]
then
	$ECHO "What is the APPROXIMATE performance of your"
	$ECHO "machine, as a percentage of a Vax 11/750?"
	$ECHO "(Enter 100 if your machine is as fast as a Vax,"
	$ECHO " 50 if it is half as fast, 200 if it is twice as fast, etc.)"
	$ECHO "The accuracy of this information is not critical."
	while :
	do
		$ECHO "Percent of Vax 11/750 [100]: \c"
		read ans
		if [ "X$ans" = "X" ]
		then
			ans=100
		fi
		longloop=`expr "$ans" "*" 3`
		if [ $? = 0 ]
		then
			break
		fi
		$ECHO "Enter a number please!"
	done
	$ECHO ""

	cat >>defines.h <<EOF
/*
 * LONGLOOP is the number of lines we should process in the line number
 * scan before displaying a warning that it will take a while.
 */
#define	LONGLOOP	($longloop)
EOF
fi




if [ "$sys" = "bsd" ]
then
	def=no; x=0
else
	def=yes; x=1
fi
if [ $alldefault = 0 ]
then
	$ECHO "Most System V systems have termio.h, while most"
	$ECHO "Berkeley-derived systems have sgtty.h."
	$ECHO "Does your system have termio.h? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * TERMIO is 1 if your system has /usr/include/termio.h.
 * This is normally the case for System 5.
 * If TERMIO is 0 your system must have /usr/include/sgtty.h.
 * This is normally the case for BSD.
 */
#define	TERMIO		$x

EOF




if [ "$sys" = "bsd" -a "$bsd41" = "0" ]
then
	def=yes; x=1
else
	def=no; x=0
fi
if [ $alldefault = 0 ]
then
	$ECHO "Most BSD 4.2 and 4.3 systems have both _setjmp() and setjmp()."
	$ECHO "Most System V and BSD 4.1 systems have only setjmp()."
	$ECHO "Does your system have both _setjmp() and setjmp()? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * HAS__SETJMP is 1 if your system has the _setjmp() call.
 * This is normally the case only for BSD 4.2 and up,
 * not for BSD 4.1 or System 5.
 */
#define	HAS__SETJMP	$x

EOF




if [ "$sys" = "bsd" -a "$bsd41" = "0" ]
then
	def=yes; x=1
else
	def=no; x=0
fi
if [ $alldefault = 0 ]
then
	$ECHO "Most BSD 4.2 and 4.3 systems have the sigsetmask() call."
	$ECHO "Most System V and BSD 4.1 systems do not."
	$ECHO "Does your system have sigsetmask()? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * SIGSETMASK is 1 if your system has the sigsetmask() call.
 * This is normally the case only for BSD 4.2,
 * not for BSD 4.1 or System 5.
 */
#define	SIGSETMASK	$x

EOF




if [ "$sys" = "sys5" -a "$xenix" = "0" ]
then
	def=yes; x=1
else
	def=no; x=0
fi
if [ $alldefault = 0 ]
then
	$ECHO "Some SCO System V systems need sys/ptem.h included to get"
	$ECHO "the size of the screen (struct winsize)."
	$ECHO "Does your system need sys/ptem.h? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * NEED_PTEM_H is 1 if your system needs sys/ptem.h to declare struct winsize.
 * This is normally the case only for SCOs System V.
 */
#define	NEED_PTEM_H	$x

EOF


if [ "$sys" = "bsd" ]
then
	def=2; REGCMP=0;RECOMP=1
else
	def=1; REGCMP=1;RECOMP=0
fi
if [ $alldefault = 0 ]
then
	$ECHO "Most System V systems have the regcmp() function."
	$ECHO "Most Berkeley-derived systems have the re_comp() function."
	$ECHO "Does your system have:"
	$ECHO "  1. regcmp"
	$ECHO "  2. re_comp"
	$ECHO "  3. neither   [$def] \c"
	read ans
	case "X$ans" in
	X1) REGCMP=1;RECOMP=0 ;;
	X2) REGCMP=0;RECOMP=1 ;;
	X3) REGCMP=0;RECOMP=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * REGCMP is 1 if your system has the regcmp() function.
 * This is normally the case for System 5.
 * RECOMP is 1 if your system has the re_comp() function.
 * This is normally the case for BSD.
 * If neither is 1, pattern matching is supported, but without metacharacters.
 */
#define	REGCMP		$REGCMP
#define	RECOMP		$RECOMP

EOF




def=yes
x=1
if [ $alloptional = 0 ]
then
	$ECHO "Do you wish to allow shell escapes? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * SHELL_ESCAPE is 1 if you wish to allow shell escapes.
 * (This is possible only if your system supplies the system() function.)
 */
#define	SHELL_ESCAPE	$x

EOF



def=yes
x=1
edname="vi"
if [ $alloptional = 0 ]
then
	$ECHO "Do you wish to allow editor escapes? [$def] \c"
	read ans
	case "X$ans" in
	X[nN]*) x=0; edname="" ;;
	X[yY]*) x=1
		$ECHO "What is the pathname of the default editor? [$edname] \c"
		read ans 
		if [ "x$ans" != "x" ]
		then
			edname=$ans
		fi
		;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * EDITOR is 1 if you wish to allow editor invocation (the "v" command).
 * (This is possible only if your system supplies the system() function.)
 * EDIT_PGM is the name of the (default) editor to be invoked.
 */
#define	EDITOR		$x
#define	EDIT_PGM	"$edname"

EOF




def=yes
x=1
if [ $alloptional = 0 ]
then
	$ECHO "Do you wish to support \"tag\" files? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * TAGS is 1 if you wish to support tag files.
 */
#define	TAGS		$x

EOF



def=yes
x=1
if [ $alloptional = 0 ]
then
	$ECHO "Do you wish to allow user-defined key definitions? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
USERFILE=$x
cat >>defines.h <<EOF
/*
 * USERFILE is 1 if you wish to allow a .less file to specify 
 * user-defined key bindings.
 */
#define	USERFILE	$x

EOF



def=yes
x=1
if [ $alldefault = 0 ]
then
	$ECHO "If your system provides the popen() function and"
	$ECHO "the \"echo\" shell command, you may allow shell metacharacters" 
	$ECHO "to be expanded in filenames."
	$ECHO "Do you wish to allow shell metacharacters in filenames? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * GLOB is 1 if you wish to have shell metacharacters expanded in filenames.
 * This will generally work if your system provides the "popen" function
 * and the "echo" shell command.
 */
#define	GLOB		$x

/*
 * PIPEC is 1 if you wish to have the "|" command
 * which allows the user to pipe data into a shell command.
 */
#define	PIPEC		$x

EOF



def=yes
x=1
if [ $alloptional = 0 ]
then
	$ECHO "Do you wish to allow log files (-l option)? [$def] \c"
	read ans
	case "X$ans" in
	X[yY]*) x=1 ;;
	X[nN]*) x=0 ;;
	esac
	$ECHO ""
fi
cat >>defines.h <<EOF
/*
 * LOGFILE is 1 if you wish to allow the -l option (to create log files).
 */
#define	LOGFILE		$x

EOF

cat >>defines.h <<EOF
/*
 * ONLY_RETURN is 1 if you want RETURN to be the only input which
 * will continue past an error message.
 * Otherwise, any key will continue past an error message.
 */
#define	ONLY_RETURN	0

EOF

cat >>makefile <<EOF

##########################################################################
# Compilation environment.
##########################################################################

EOF



if [ "$xenix" = "1" ]
then
	LIBS="-ltermlib"
elif [ "$sys" = "bsd" ]
then
	LIBS="-ltermcap"
else
	LIBS="-lcurses -ltermcap -lPW"
fi
if [ $alldefault = 0 ]
then
	$ECHO "To build \"less\", you must link with libraries supplied by your system."
	$ECHO "(If this needs to be changed later, edit the makefile"
	$ECHO "and change the definition of LIBS.)"
	$ECHO "What libraries should be used [$LIBS] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		LIBS="$ans"
	fi
	$ECHO ""
fi
cat >>makefile <<EOF
# LIBS is the list of libraries needed.
LIBS = $LIBS

EOF



INSTALL_LESS="/usr/local/bin/less"
INSTALL_KEY="/usr/local/bin/lesskey"
INSTALL_HELP="/usr/local/bin/less.hlp"
INSTALL_LESSMAN="/usr/man/man1/less.1"
INSTALL_KEYMAN="/usr/man/man1/lesskey.1"
LESS_MANUAL="less.nro"
KEY_MANUAL="lesskey.nro"
if [ $alldefault = 0 ]
then
	$ECHO "What is the name of the \"public\" (installed) version of less?"
	$ECHO " [$INSTALL_LESS] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		INSTALL_LESS="$ans"
	fi
	$ECHO "What is the name of the \"public\" (installed) version of lesskey?"
	$ECHO " [$INSTALL_KEY] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		INSTALL_KEY="$ans"
	fi
	$ECHO "What is the name of the \"public\" (installed) version of the help file?"
	$ECHO " [$INSTALL_HELP] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		INSTALL_HELP="$ans"
	fi
	$ECHO "What is the name of the \"public\" (installed) version of the less manual page?"
	$ECHO " [$INSTALL_LESSMAN] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		INSTALL_LESSMAN="$ans"
	fi
	$ECHO "What is the name of the \"public\" (installed) version of the lesskey manual page?"
	$ECHO " [$INSTALL_KEYMAN] \c"
	read ans
	if [ "X$ans" != "X" ]
	then
		INSTALL_KEYMAN="$ans"
	fi
	$ECHO ""
fi

cat >>defines.h <<EOF
/*
 * HELPFILE is the full pathname of the help file.
 */
#define	HELPFILE	"$INSTALL_HELP"

EOF

cat >>makefile <<EOF
# INSTALL_LESS is a list of the public versions of less.
# INSTALL_KEY is a list of the public versions of lesskey.
# INSTALL_HELP is a list of the public version of the help file.
# INSTALL_LESSMAN is a list of the public versions of the less manual page.
# INSTALL_KEYMAN is a list of the public versions of the lesskey manual page.
INSTALL_LESS =		\$(ROOT)$INSTALL_LESS
INSTALL_KEY =		\$(ROOT)$INSTALL_KEY
INSTALL_HELP =		\$(ROOT)$INSTALL_HELP
INSTALL_LESSMAN =	\$(ROOT)$INSTALL_LESSMAN
INSTALL_KEYMAN =	\$(ROOT)$INSTALL_KEYMAN
LESS_MANUAL =		$LESS_MANUAL
KEY_MANUAL =		$KEY_MANUAL
HELPFILE =		$INSTALL_HELP


EOF



cat >>makefile <<"EOF"
# OPTIM is passed to the compiler and the loader.
# It is normally "-O" but may be, for example, "-g".
OPTIM = -O

CFLAGS = $(OPTIM) -DSTRCSPN -I.



##########################################################################
# Files
##########################################################################

SRC1 =	ch.c cmdbuf.c command.c decode.c help.c input.c 
SRC2 =	line.c linenum.c main.c edit.c option.c optfunc.c \
	opttbl.c os.c 
SRC3 =	charset.c filename.c lsystem.c output.c position.c ifile.c \
	brac.c forwback.c jump.c search.c 
SRC4 =	mark.c prompt.c screen.c signal.c tags.c ttyin.c version.c
SRC5 =  regexp.c regerror.c

SRC =	$(SRC1) $(SRC2) $(SRC3) $(SRC4) $(SRC5)

OBJ =	brac.o ch.o charset.o cmdbuf.o command.o decode.o edit.o filename.o \
	forwback.o help.o input.o jump.o line.o linenum.o \
	lsystem.o main.o option.o optfunc.o opttbl.o os.o \
	output.o position.o mark.o ifile.o prompt.o screen.o \
	search.o signal.o tags.o ttyin.o version.o \
	regexp.o regerror.o


##########################################################################
# Rules for building stuff
##########################################################################

EOF

if [ "$USERFILE" = "1" ]
then
	cat >>makefile <<"EOF"
all: less lesskey
install: install_less install_help install_key install_lman install_kman
EOF
else
	cat >>makefile <<"EOF"
all: less
install: install_less install_help install_lman
EOF
fi

cat >>makefile <<"EOF"

less: $(OBJ)
	$(CC) $(LDFLAGS) $(OPTIM) -o less $(OBJ) $(LIBS) $(LDLIBS)

lesskey: lesskey.o
	$(CC) $(LDFLAGS) $(OPTIM) -o lesskey lesskey.o $(LDLIBS)

install_less: less
	for f in $(INSTALL_LESS); do  rm -f $$f; cp less $$f;  done
	touch install_less

install_key: lesskey
	for f in $(INSTALL_KEY); do  rm -f $$f; cp lesskey $$f;  done
	touch install_key

install_help: less.hlp
	for f in $(INSTALL_HELP); do  rm -f $$f; cp less.hlp $$f;  done
	touch install_help

install_lman: $(LESS_MANUAL) 
	for f in $(INSTALL_LESSMAN); do  rm -f $$f; cp $(LESS_MANUAL) $$f;  done
	touch install_lman

install_kman: $(KEY_MANUAL)
	for f in $(INSTALL_KEYMAN); do  rm -f $$f; cp $(KEY_MANUAL) $$f;  done
	touch install_kman

##########################################################################
# Maintenance
##########################################################################

lint:
	lint -hp $(SRC)

newfuncs funcs.h:
	if [ -f funcs.h ]; then mv funcs.h funcs.h.OLD; fi
	awk -f mkfuncs.awk $(SRC) >funcs.h

clean:
	rm -f $(OBJ) lesskey.o less lesskey vecho

clobber:
	rm -f *.o less lesskey vecho install_less install_key \
		install_help install_lman install_kman

shar:
	shar -v README CHANGES linstall \
		less.nro lesskey.nro \
		vecho.c mkfuncs.awk > less1.shr
	shar -v less.man lesskey.man \
		less.h position.h cmd.h option.h > less2.shr 
	shar -v lesskey.c $(SRC1) > less3.shr
	shar -v $(SRC2) > less4.shr
	shar -v $(SRC3) less.hlp > less5.shr
	shar -v $(SRC4) funcs.h > less6.shr
	shar -v $(SRC5) regexp.h > less7.shr


##########################################################################
# Dependencies
##########################################################################

$(OBJ): less.h funcs.h defines.h position.h
command.o decode.o: cmd.h
option.o opttbl.o optfunc.o: option.h

lesskey.o: less.h funcs.h defines.h cmd.h

EOF
$ECHO ""

$ECHO "The makefile and defines.h have been built."
$ECHO "You should check them to make sure everything is as you want it to be."
$ECHO "When you are satisfied, just type \"make\", and \"less\" will be built."
