#!/bin/sh

echo ""
echo "+----------===== Configuring `cat VERSION` =====----------"

cd src
if [ $? -gt 0 ]; then
  echo run script from ftpd-3.xx subdirectory
  exit 1
fi
cd ..

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 C-compiler ... $ac_c"
if [ -x /opt/SUNWspro/bin/cc ]; then
  CC=/opt/SUNWspro/bin/cc
else
  if [ "$CC" = "" ]; then
    if gcc -v >/dev/null 2>&1; then
      CC=gcc
    else
      CC=cc
    fi
  fi
fi
echo $CC

cd cfg
CFLAGS=""
echo $ac_n "| checking for sparcv9 architecture ... $ac_c"
SYSTEM=`uname`
if [ "$SYSTEM" = "SunOS" ]; then
  isalist 2>/dev/null |grep sparcv9 >/dev/null
  if [ $? -eq 0 ]; then
    cc -xarch=v9 -o ./sizeof_long sizeof_long.c >/dev/null 2>/dev/null
    if [ -x ./sizeof_long ]; then
      LONGSIZE=`./sizeof_long`
      if [ $LONGSIZE -eq 64 ]; then
        SPARCV9=yes
        CC=cc
        CFLAGS="-xarch=v9"
        CFLAGS="$CFLAGS $CFLAGS_TEST"
      fi
    fi
  fi
fi
if [ "$SPARCV9" = "yes" ]; then
  echo "yes"
  LIBDIR=/usr/lib/sparcv9
else
  echo "no"
  LIBDIR=/usr/lib
fi
cd ..

if [ "$1" = "debug" -o "$1" = "gdb" ]; then
  if [ "$SPARCV9" = "yes" ]; then
    CFLAGS="$CFLAGS -g"
  else
    CFLAGS="$CFLAGS -g -Wall"
  fi
else
  if [ ! "$SPARCV9" = "yes" ]; then
    CFLAGS="$CFLAGS -O"
  fi
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 resolv V3; do
  echo $ac_n  "| checking for library $library ... $ac_c"
  if [ -r $LIBDIR/lib$library.a -o -r $LIBDIR/lib$library.so ]; 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 "| checking for setenv() ... $ac_c"
ar -t /usr/lib/libc.a 2>/dev/null | grep setenv.o >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
  echo found
  DEF_SETENV='#define HAVE_SETENV 1'
else
  echo "not found, using putenv() instead"
fi

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

cd cfg
echo $ac_n "| compiling test programs ... $ac_c"
$CC $CFLAGS -o ./sizeof_short sizeof_short.c >/dev/null 2>/dev/null
$CC $CFLAGS -o ./sizeof_int sizeof_int.c >/dev/null 2>/dev/null
$CC $CFLAGS -o ./sizeof_long sizeof_long.c >/dev/null 2>/dev/null
$CC $CFLAGS -o ./sizeof_longlong sizeof_longlong.c >/dev/null 2>/dev/null
$CC $CFLAGS -o ./longlongformat longlongformat.c >/dev/null 2>/dev/null
echo done

echo $ac_n "| examining data type int ... $ac_c"
INTSIZE=`./sizeof_int`
echo $INTSIZE bit

echo $ac_n "| examining data type short ... $ac_c"
SHORTSIZE=`./sizeof_short`
echo $SHORTSIZE bit

echo $ac_n "| examining data type long ... $ac_c"
LONGSIZE=`./sizeof_long`
echo $LONGSIZE bit

echo $ac_n "| examining data type long long ... $ac_c"
if [ -x ./sizeof_longlong ]; then
  LONGLONGSIZE=`./sizeof_longlong`
  echo $LONGLONGSIZE bit
else
  LONGLONGSIZE=0
  echo not supported
fi

# incompatible cpu
if [ $INTSIZE -ne 32 -a $INTSIZE -ne 64 ]; then
  echo ERROR: Machine word length must be 32 or 64 bit.
  exit 1
fi

# sparcv9
if [ $INTSIZE -eq 32 -a $LONGSIZE -eq 64 ]; then
  INT32='int'
  SCANINT32='i'
  INT64='long'
  SCANINT64='li'
fi

# 32 bit cpu
if [ $INTSIZE -eq 32 -a $LONGSIZE -ne 64 ]; then
  INT32='int'
  SCANINT32='i'
  if [ $LONGLONGSIZE -eq 64 ]; then
    echo $ac_n "| argument to scan long long ... $ac_c"
    INT64='long long'
    SCANINT64=`./longlongformat`
    if [ $? -ne 0 ]; then
      echo ERROR: Your C-Library does not support 64 bit integer in scanf and printf functions
      exit 1
    fi
    echo $SCANINT64
  else
    echo ERROR: Your system does not support 64 bit integer
    exit 1
  fi
fi

# 64 bit cpu
if [ $INTSIZE -eq 64 ]; then
  INT64='int'
  SCANINT64='i'
  if [ $SHORTSIZE -eq 32 ]; then
    INT32='short'
    SCANINT32='hi'
  else
    echo ERROR: Your system does not support 32 bit integer
    exit 1
  fi
fi

cd ..

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 Makefile ... $ac_c"

cat << "EOF" >Makefile
##############################################################################
#                                                                            #
#            --===>    DO NOT MODIFY ANYTHING FROM HERE    <===--            #
#                                                                            #
##############################################################################

all:
EOF

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

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

if [ "$CFLAGS" = "-O" ]; then
  echo "	@cd src ; make strip" >>Makefile
fi

cat << "EOF" >>Makefile

strip:
	@cd src ; make strip

clean:
	@cd lib ; make clean
	@cd src ; make clean
	@cd cfg ; make clean
	@rm -f Makefile src/common/tweak.h src/common/Makefile src/Makefile src/ftpa/Makefile src/ftpd/Makefile src/ftps/Makefile

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

echo done





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

cd src

cat << "EOF" >Makefile
##############################################################################
#                                                                            #
#            --===>    DO NOT MODIFY ANYTHING FROM HERE    <===--            #
#                                                                            #
##############################################################################

TARGET1 = ../bin/ftpa
TARGET2 = ../bin/ftpd
TARGET3 = ../bin/ftps

all:
	@cd common ; make
	@cd ftpa ; make
	@cd ftpd ; make
	@cd ftps ; make

strip:
	strip ${TARGET1} ${TARGET2} ${TARGET3}

clean:
	rm -f ${TARGET1} ${TARGET2} ${TARGET3}
	@cd common ; make clean
	@cd ftpa ; make clean
	@cd ftpd ; make clean
	@cd ftps ; make clean
EOF

echo done





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

cd common

cat << "EOF" >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 =


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

OBJS   = rfc931.o utils.o
HEADER = commondef.h tweak.h
EOF

echo "INCPATH = "'${INCLUDE}' >>Makefile
echo "CFLAGS  = $CFLAGS "'${DEF}' >>Makefile

cat << "EOF" >>Makefile

all:    ${OBJS}

clean:
	rm -f ${OBJS}

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

${OBJS}:     ${HEADER}

EOF

echo done





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

cd ../ftpa

cat << "EOF" >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 =


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

TARGET = ../../bin/ftpa
OBJS   = ftpa.o
HEADER = defines.h ../common/tweak.h
EOF

echo "INCPATH = "-I../common " $DES "'${INCLUDE}' >>Makefile
echo "CFLAGS  = $CFLAGS $DEF "'${DEF}' >>Makefile
echo "LIBS    = ../common/rfc931.o ../common/utils.o $LIB "'${LIBRARY}' >>Makefile

cat << "EOF" >>Makefile

all:    ${TARGET}

clean:
	rm -f ${TARGET} ${OBJS}

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

${TARGET}:   ${OBJS}
	${CC} ${CFLAGS} -o ${TARGET} ${OBJS} ${LIBS} 

${OBJS}:     ${HEADER}

EOF

echo done





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

cd ../ftpd

cat << "EOF" >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 =


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

TARGET = ../../bin/ftpd
OBJS   = debug.o f4adp_admin.o f4adp_config.o f4adp_group.o f4adp_message.o \
         f4adp_server.o f4adp_user.o ftpd.o group.o limits.o log.o \
         registry.o reject.o serverd.o stat.o user.o
HEADER = definesd.h externsd.h globalsd.h ../common/tweak.h
EOF

echo "INCPATH = "-I../common " $DES "'${INCLUDE}' >>Makefile
echo "CFLAGS  = $CFLAGS $DEF "'${DEF}' >>Makefile
echo "LIBS    = ../common/rfc931.o ../common/utils.o $LIB "'${LIBRARY}' >>Makefile

cat << "EOF" >>Makefile

all:    ${TARGET}

clean:
	rm -f ${TARGET} ${OBJS}

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

${TARGET}:   ${OBJS}
	${CC} ${CFLAGS} -o ${TARGET} ${OBJS} ${LIBS} 

${OBJS}:     ${HEADER}

EOF

echo done





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

cd ../ftps

cat << "EOF" >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 =


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

TARGET = ../../bin/ftps
OBJS   = commands.o common.o config.o dir.o ff.o ftps.o list.o log.o login.o \
         perm.o site.o transfer.o
HEADER = defines.h externs.h globals.h ../common/tweak.h
EOF

echo "INCPATH = -I../common "'${INCLUDE}' >>Makefile
echo "CFLAGS  = $CFLAGS "'${DEF}' >>Makefile
echo "LIBS    = ../common/rfc931.o ../common/utils.o $LIB "'${LIBRARY}' >>Makefile

cat << "EOF" >>Makefile

all:    ${TARGET}

clean:
	rm -f ${TARGET} ${OBJS} ${OBJS}

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

${TARGET}:   ${OBJS}
	${CC} ${CFLAGS} -o ${TARGET} ${OBJS} ${LIBS}

${OBJS}:     ${HEADER}
EOF

echo done





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

cd ../common

cat << "EOF" >tweak.h
/*   tweak.h
 *
 *   This file is part of FTP4ALL
 *
 *   Copyright (C) 1996-2000 by Crescent
 *
 *   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

if [ "$SPARCV9" = "yes" ]; then
  cat <<EOF >>tweak.h
#ifdef ferror
#undef ferror
#endif
#ifdef feof
#undef feof
#endif
#ifdef NULL
#undef NULL
#define NULL 0
#endif
EOF
fi

if [ "$1" = "debug" ]; then
  echo "#define DEBUG 1" >>tweak.h
fi

echo $DEF_MEMMOVE  >>tweak.h
echo $DEF_RAISE    >>tweak.h
echo $DEF_SIGSET   >>tweak.h
echo $DEF_SETENV   >>tweak.h
echo $DEF_SOCKETPAIR >>tweak.h
echo $DEF_SELECT_H >>tweak.h
echo $DEF_TIME_H   >>tweak.h
echo "#define int32 $INT32" >>tweak.h
echo "#define int64 $INT64" >>tweak.h
echo "#define I32 \"$SCANINT32\"" >>tweak.h
echo "#define I64 \"$SCANINT64\"" >>tweak.h
echo "#define SI32 \"%$SCANINT32\"" >>tweak.h
echo "#define SI64 \"%$SCANINT64\"" >>tweak.h

cat << "EOF" >>tweak.h

/* EOF */
EOF

echo done





echo "+----------===================================----------"
echo ""
echo If you want to change the maximum filename length, edit variable
echo MAXFILENAMELENGTH in src/common/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 ""
