#!/bin/sh

echo ""
echo "+----------===== Configuring FTP4ALL 2.x =====----------"
cd src
if [ $? -gt 0 ]; then
  echo run script from ftpd-2.xx subdirectory
  exit 1
fi

if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
  if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
    ac_n= ac_c='
' ac_t='	'
  else
    ac_n=-n ac_c= ac_t=
  fi
else
  ac_n= ac_c='\c' ac_t=
fi


echo $ac_n "| checking for gcc ... $ac_c"
if which gcc >/dev/null; then
  echo found
  CC=gcc
else
  echo not found, using CC instead
  CC=cc
fi

DEF=""
echo $ac_n "| checking for sys/select.h ... $ac_c"
if [ -r /usr/include/sys/select.h ]; then
  echo found
  DEF_SELECT_H="#define HAVE_SYS_SELECT_H 1"
else
  echo not found
fi
echo $ac_n "| checking for sys/time.h ... $ac_c"
if [ -r /usr/include/sys/time.h ]; then
  echo found
  DEF_TIME_H="#define HAVE_SYS_TIME_H 1"
else
  echo not found
fi

LIB=""
for library in crypt nsl socket V3; do
  echo $ac_n  "| checking for library $library ... $ac_c"
  if [ -r /usr/lib/lib$library.a ]; then
    echo found
    LIB=${LIB}" -l$library"
  else
    echo not needed
  fi
done

echo $ac_n "| checking for memmove() ... $ac_c"
ar -t /usr/lib/libc.a 2>/dev/null | grep memmove.o >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  echo found
else
  echo "not found, using bcopy() instead"
  DEF_MEMMOVE='#define memmove(x,y,z) bcopy(y,x,z)'
fi

echo $ac_n "| checking for raise() ... $ac_c"
ar -t /usr/lib/libc.a 2>/dev/null | grep raise.o >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  echo found
else
  echo "not found, using kill() instead"
  DEF_RAISE='#define raise(x) kill(getpid(),x)'
fi

echo $ac_n "| checking for sigset() ... $ac_c"
ar -t /usr/lib/libc.a 2>/dev/null | grep sigset.o >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  echo found
  DEF_SIGSET='#define HAVE_SIGSET 1'
else
  echo "not found, using sigaction() instead"
fi

echo $ac_n "| want DES encryption (y/n) ? $ac_c"
read DES
if [ $DES = y ]; then
  DEF=$DEF" -DDES_ENCRYPTION"
  DES="-I../lib"
  LIB=${LIB}" -L../lib -ldes "
else
  DES=""
fi

echo $ac_n "| creating src/Makefile ... $ac_c"

cat << "EOF" >Makefile
#----------------------------#
#      FTP4ALL-Makefile      #
#                            #
#   user configurable part   #
#     you may edit below     #
#----------------------------#

# Compiler to use
EOF

echo "CC = $CC" >>Makefile

cat << "EOF" >>Makefile

# Path to include files (if not in standard include path)
INCLUDE =

# Libraries to link and path to libraries
LIBRARY =

# Symbols to define
DEF = -O


#--------------------------------------------------------------#
#     ----->    DO NOT MODIFY ANYTHING FROM HERE    <-----     #
#--------------------------------------------------------------#

TARGET1 = ftpd
TARGET2 = ftps
OBJS1   = configd.o ftpd.o serverd.o rfc931.o
OBJS2   = commands.o common.o dir.o ff.o ftps.o list.o login.o perm.o site.o transfer.o
HEADER1 = definesd.h externsd.h globalsd.h tweak.h
HEADER2 = defines.h externs.h globals.h perm.h tweak.h
EOF

echo "INCPATH = $DES "'${INCLUDE}' >>Makefile
echo "CFLAGS  = $DEF "'${DEF}' >>Makefile
echo "LIBS1   = $LIB "'${LIBRARY}' >>Makefile
echo "LIBS2   = $LIB "'${LIBRARY}' >>Makefile

cat << "EOF" >>Makefile

all:    ${TARGET1} ${TARGET2}

clean:
	rm -f ${TARGET1} ${TARGET2} ${OBJS1} ${OBJS2} ../bin/${TARGET1} ../bin/${TARGET1} ../bin/${TARGET2}

.c.o:
	${CC} ${CFLAGS} -c $*.c -o $@ ${INCPATH}

${TARGET1}:   ${OBJS1}
	${CC} ${CFLAGS} -o ${TARGET1} ${OBJS1} ${LIBS1} 
	-strip ${TARGET1}
	-ln -f ${TARGET1} ../bin

${OBJS1}:     ${HEADER1}

${TARGET2}:   ${OBJS2}
	${CC} ${CFLAGS} -o ${TARGET2} ${OBJS2} ${LIBS2}
	-strip ${TARGET2}
	-ln -f ${TARGET2} ../bin

${OBJS2}:     ${HEADER2}
EOF

cd ..

echo done
echo $ac_n "| creating Makefile ... $ac_c"

cat << "EOF" >Makefile
#
# FTP4ALL Makefile
#
all:
EOF

if [ ! "$DES" = "" ]; then
  echo "	@cd lib ; make" >>Makefile
fi

cat << "EOF" >>Makefile
	@cd src ; make

clean:
	@cd lib ; make clean
	@cd src ; make clean
	@rm -f Makefile src/tweak.h src/Makefile

install:
	cp -f bin/* /usr/local/bin
EOF

echo done

echo $ac_n "| creating src/tweak.h ... $ac_c"

cat << "EOF" >src/tweak.h
/*   tweak.h
 *
 *   This file is part of FTP4ALL
 *
 *   Copyright (C) 1996,1997,1998,1999 Christoph Schwarz
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define MAXFILENAMELENGTH 32

#ifndef SA_RESTART
  #define SA_RESTART 0
#endif

#ifndef SEEK_END
  #define SEEK_END 2
#endif

#ifndef SIGCLD
  #define SIGCLD SIGCHLD
#endif

#ifndef crypt
  char* crypt(const char*,const char*);
#endif

EOF

echo $DEF_MEMMOVE  >>src/tweak.h
echo $DEF_RAISE    >>src/tweak.h
echo $DEF_SIGSET   >>src/tweak.h
echo $DEF_SELECT_H >>src/tweak.h
echo $DEF_TIME_H   >>src/tweak.h

cat << "EOF" >>src/tweak.h
#ifdef F4ADEB
  #define DEBUG
#endif

/* EOF */
EOF

echo done
echo "+----------===================================----------"
echo ""
echo If you want to change the maximum filename length, edit variable
echo MAXFILENAMELENGTH in src/tweak.h now \(before make\).
echo But beware, because old permission files will become unusable !!!
echo ""
echo Configuration finished.
echo Type \"make\" to compile FTP4ALL.
echo ""
