#!/bin/sh
# No, this isn't generated by autoconf
# Some parts are inspired by autoconf (Free Software Foundation), however
# And the idea is slightly the same

# Usage:
# './configure' and then 'make' and 'make install', or
# './configure --make --install'

SHELL=/bin/sh

ac_prev=
for ac_option
do
  # If the previous option needs an argument, assign it.
  if test -n "$ac_prev"; then
    eval "$ac_prev=\$ac_option"
    ac_prev=
    continue
  fi

  case "$ac_option" in
    -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) ac_optarg= ;;
  esac

  case "$ac_option" in

  --pthread | --thread)     THREADS=1   ;;
  --nopthread | --nothread) THREADS=0   ;;

  --zlib | --gzip)          ZLIB=1	;;
  --nozlib | --nogzip)      ZLIB=0	;;

  --static | --noso)        DYNAMIC=0
                            ;;
  --dynamic | --so)         DYNAMIC=1
                            ;;

  --longlong)               LONGLONG=1  ;;
  --nolonglong)             LONGLONG=0  ;;

  --inaddrt)                NODECLINADDRT=0 ;;
  --noinaddrt)              NODECLINADDRT=1 ;;

  --useuid)                 NOUID=0     ;;
  --nouseuid)               NOUID=1     ;;

  --useftime)               NOFTIME=0   ;;
  --nouseftime)             NOFTIME=1   ;;

  --system=*)               SYSTEMTYPE="$ac_optarg" ;;
  --system)                 ac_prev=SYSTEMTYPE ;;

  --debug)                  OTYPE="-O0 -g3"
                            MKTYPE="firstinfo htssystem htssystem_auto build_auto lastinfo" ;;

  --make)                   DOMAKE=1    ;;
  --install)                DOINSTALL=1 ;;
  --bininstall)             DOINSTALL=1
                            NODOCINSTALL=1
                            ;;
  --docinstall)             DOINSTALL=1
                            DOCINSTALL=1
                            ;;
  --cls)
    cd ..
    chmod 'u=rw,go=r' `find ./ -type f`
    chmod 'u=rwx,go=rx' `find ./ -type d`
    chmod 'u=rwx,go=rx' ./src/configure
    chmod 'u=rwx,go=rx' ./src/strip_cr.in
    chmod 'u=rwx,go=rx' ./src/postinst-config.in
    cd src
    strip_cr *.c *.h
    rm -f ./httrack 2>/dev/null
    exit    
    ;;
  -prefix | --prefix | --prefi | --pref | --pre | --pr)
    ac_prev=PREFIX ;;
  -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=*)
    PREFIX="$ac_optarg" ;;
  -bindir | --bindir | --bindi | --bind | --bin | --bi)
    ac_prev=BINPATH ;;
  -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)
    BINPATH="$ac_optarg" ;;
  -etcdir | --etcdir | --etcdi | --etcd | --etc | --et)
    ac_prev=ETCPATH ;;
  -etcdir=* | --etcdir=* | --etcdi=* | --etcd=* | --etc=* | --et=*)
    ETCPATH="$ac_optarg" ;;
  -libdir | --libdir | --libdi | --libd | --lib | --li)
    ac_prev=LIBPATH ;;
  -libdir=* | --libdir=* | --libdi=* | --libd=* | --lib=* | --li=*)
    LIBPATH="$ac_optarg" ;;
  
  --help)
    cat <<EOF
--prefix=..  : prefix (/usr)
--bindir=..  : binary dir (/usr/bin)
--etcdir=..  : config dir (/usr/etc or /etc)
--libdir=..  : library dir (/usr/lib)
--dynamic    : do use dynamic (.so) mode
--static     : do use static mode
--nopthread  : do not use threads (pthread.h)
--pthread    : do     use threads (pthread.h)
--nozlib     : do not use compression (zlib)
--zlib       : do     use compression (zlib)
--nolonglong : do not use 64-bit int
--longlong   : do     use 64-bit int
--noinaddrt  : do not redeclare in_addr_t
--inaddrt    : do     redeclare in_addr_t
--nouseuid   : do not use setuid()/setgid()
--useuid     : do     use setuid()/setgid()
--nouseftime : do not use ftime()
--useftime   : do     use ftime()
--system=<type> : override system type (uname) (types: 'Default','Linux','SunOS','AIX')
--make       : 'make' after configure
--install    : 'make install' after configure
--bininstall : 'make bininstall' after configure
--docinstall : 'make docinstall' after configure
--debug      : add debug information (for gdb)
EOF
  exit
  ;;

  *) echo "Unrecognized option: $ac_option" 
     exit 
     ;;

  esac

done

echo "Welcome to HTTrack Website Copier!"
echo "Type in ./configure --help for more details"
echo "If this script fails, you can enter supplemental options through '--option=value'"
echo "or enter in manual make, through 'make help'"
echo ""

if cp -f Makefile.in Makefile; then

SEDEXEC=

# System (OS) type?
printf "Checking for OS type.. "
if test -z "$SYSTEMTYPE"; then
    SYSTEMTYPE="`uname`"
fi
case "$SYSTEMTYPE" in 
	SunOS) printf "SunOS/Solaris\n"; 
               PLATFORM=0
               SOLSOCK=1
               ;;
	AIX)   printf "AIX\n";           PLATFORM=2 ;;
	*)     printf "Linux type\n";    PLATFORM=3 ;;
esac

WTYPE="-Wall -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wnested-externs"

if test -z "$OTYPE"; then
    OTYPE="-O3"
fi
if test -z "$MKTYPE"; then
    MKTYPE="firstinfo htssystem htssystem_auto build_auto strip clean lastinfo"
fi

# Root group
if test -z "$ROOTGROUP"; then
    printf "Checking for 'root' group.."
    if egrep "^root:" /etc/group >/dev/null; then
        ROOTGROUP="root"
    elif egrep "^wheel:" /etc/group >/dev/null; then
        ROOTGROUP="wheel"
    fi
    printf "$ROOTGROUP\n"
else
    echo "Overriding ROOTGROUP=$ROOTGROUP"
fi


# Binaries location
if test -z "$BINPATH"; then
printf "Checking for bin directory.. "
if test -n "$PREFIX"; then
    BINPATH="$PREFIX/bin"
elif test -d "/usr/bin"; then
    BINPATH="/usr/bin"
else
    BINPATH="/bin"
fi
printf "$BINPATH\n"
else
    echo "Overriding BINPATH=$BINPATH"
fi

# /etc location
if test -z "$ETCPATH"; then
printf "Checking for etc directory.. "
if test -n "$PREFIX"; then
    ETCPATH="$PREFIX/etc"
elif test -d "/usr/etc"; then
    ETCPATH="/usr/etc"
else
    ETCPATH="/etc"
fi
printf "$ETCPATH\n"
else
    echo "Overriding ETCPATH=$ETCPATH"
fi

# /usr/lib location
if test -z "$LIBPATH"; then
printf "Checking for lib directory.. "
if test -n "$PREFIX"; then
    LIBPATH="$PREFIX/lib"
elif test -d "/usr/lib"; then
    LIBPATH="/usr/lib"
elif test -d "/usr/local/lib"; then
    LIBPATH="/usr/local/lib"
else
    LIBPATH="/lib"
fi
printf "$LIBPATH\n"
else
    echo "Overriding LIBPATH=$LIBPATH"
fi

# Prefix location
if test -z "$PREFIX"; then
    printf "Checking for prefix directory.. "
    PREFIX="/usr"
    printf "$PREFIX\n"
else
    echo "Overriding PREFIX=$PREFIX"
fi


# 64-bit (long long) cause some troubles to some processors
# because some alignements aren't properly defined
# we only accept 64-bit on tested processors here
if test -z "$LONGLONG"; then
printf "Checking for long long.. "
LONGLONG=
if grep "long long" /usr/include/sys/types.h >/dev/null; then
if uname -a|egrep ' i[3-9]86 ' >/dev/null; then
    LONGLONG=1
fi
fi
if test -n "$LONGLONG"; then
    printf "supported\n"
else
    printf "not tested/supported. Use --LONGLONG=1 to override\n"
fi
else
    echo "Overriding LONGLONG=$LONGLONG"
fi

# Dynamic (.so) module?
if test -z "$DYNAMIC"; then
	DYNAMIC=1
fi
printf "Checking for compilation mode: "
if test "$DYNAMIC" -eq "1"; then
	echo "dynamic"
	SOTYPE=-fPIC
else
	echo "static"
	SOTYPE=
fi

# Do we not have to redeclare in_addr_t ?
# Sometimes this type is defined, or not..
if test -z "$NODECLINADDRT"; then
printf "Checking for in_addr_t declaration in /usr/include/arpa/inet.h.. "
if grep "in_addr_t" /usr/include/arpa/inet.h >/dev/null; then
	printf "found, do not redeclare\n"
	NODECLINADDRT=1
else
	printf "not found, declaring\n"
	NODECLINADDRT=
fi
else
    echo "Overriding NODECLINADDRT=$NODECLINADDRT"
fi

# Test if we can use zlib (/usr/lib/libz.so)
# This allow to speed up transfers using HTTP compression
if test -z "$ZLIB"; then
printf "Checking for /usr/lib/libz.so.. "
if test -f "/usr/lib/libz.so"; then
	printf "found\n"
	ZLIB=1
elif test -f "/usr/lib/libz.so.1"; then
	printf "found\n"
	ZLIB=1
else
	printf "library not found (too bad), no http compression will be available\n"
	ZLIB=0
fi
else
	echo "Overriding ZLIB=$ZLIB"
fi

# Sometimes, pthread.h doesn't exists on some systems
# This is sad, because it speeds up some useful things, like DNS or ftp
if test -z "$THREADS"; then
printf "Checking for /usr/include/pthread.h.. "
if test -f "/usr/include/pthread.h"; then
if test -f "/usr/lib/libpthread.so"; then
	printf "found\n"
	THREADS=1
else
	printf "library not found (too bad), no threads will be available\n"
	THREADS=
fi
else
	printf "not found, no threads will be available\n"
	THREADS=
fi
else
    echo "Overriding THREADS=$THREADS"
fi

# Sometimes, setuid and setgid can't be used (missing pwd.h and unistd.h ?!)
if test -z "$NOUID"; then
NOUID=1
printf "Checking for /usr/include/pwd.h and /usr/include/unistd.h.. "
if test -f "/usr/include/pwd.h"; then
if test -f "/usr/include/unistd.h"; then
	NOUID=
fi
fi
if test -z "$NOUID"; then
    printf "found\n"
else
    printf "not found, not using setuid() and setgid()\n"
fi
else
    echo "Overriding NOUID=$NOUID"
fi

# Sometimes, ftime can't be used (missing declaration...)
if test -z "$NOFTIME"; then
NOFTIME=1
printf "Checking for ftime in /usr/include/sys/timeb.h.. "
if grep "extern int ftime" /usr/include/sys/timeb.h >/dev/null; then
	NOFTIME=
fi
if test -z "$NOFTIME"; then
	printf "found\n"
else
	printf "not found (too bad), not using ftime()\n"
fi
else
	echo "Overriding NOFTIME=$NOFTIME"
fi

# Test variables
if test "$NOUID" = 1; then
    SEDEXEC="$SEDEXEC | sed -e 's/__DEFINEUID__/HTS_DO_NOT_USE_UID/'"
fi
if test "$NOFTIME" = 1; then
    SEDEXEC="$SEDEXEC | sed -e 's/__DEFINEFTI__/HTS_DO_NOT_USE_FTIME/'"
fi
if test "$NODECLINADDRT" = 1; then
    SEDEXEC="$SEDEXEC | sed -e 's/__DEFINEINA__/HTS_DO_NOT_REDEFINE_in_addr_t/'"
fi
if test "$THREADS" = 1; then
	SEDEXEC="$SEDEXEC | sed -e \"s/__CFLAGS__/$SOTYPE $OTYPE $WTYPE -D_REENTRANT/g\""
	SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS__/-lpthread/g'"
else
	SEDEXEC="$SEDEXEC | sed -e 's/__DEFINEPTH__/HTS_DO_NOT_USE_PTHREAD/'"
	SEDEXEC="$SEDEXEC | sed -e \"s/__CFLAGS__/$SOTYPE $OTYPE $WTYPE/g\""
	SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS__//g'"
fi
if test "$ZLIB" = 1; then
        SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS3__/-lz/g'"
else
        SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS3__//g'"
fi
SEDEXEC="$SEDEXEC | sed -e \"s/__ZLIB__/$ZLIB/\""
if test "$SOLSOCK" = 1; then
	SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS2__/-lnsl -lsocket/g'"
else
	SEDEXEC="$SEDEXEC | sed -e 's/__LFLAGS2__//g'"
fi
if test ! "$LONGLONG" = 1; then
	SEDEXEC="$SEDEXEC | sed -e 's/__DEFINE64B__/HTS_NO_64_BIT/'"
fi
if test "$DYNAMIC" = 1; then
	SEDEXEC="$SEDEXEC | sed -e 's/__DYNAMIC__/so/'"
	SEDEXEC="$SEDEXEC | sed -e 's/__INSTALL__/bininstall libinstall docinstall config/'"
	SEDEXEC="$SEDEXEC | sed -e 's/__UNINSTALL__/binremove libremove docremove/'"
else
	SEDEXEC="$SEDEXEC | sed -e 's/__DYNAMIC__//'"
	SEDEXEC="$SEDEXEC | sed -e 's/__INSTALL__/bininstall docinstall config/'"
	SEDEXEC="$SEDEXEC | sed -e 's/__UNINSTALL__/binremove docremove/'"
fi

SEDEXEC="$SEDEXEC | sed -e \"s/__PLATFORM__/$PLATFORM/g\""
SEDEXEC="$SEDEXEC | sed -e 's/#__AUTONAME__/AUTONAME  = auto/'"
SEDEXEC="$SEDEXEC | sed -e 's/#define __DEFINE.*__//g'"

# Paths
TMP=`echo $BINPATH | sed -e 's/\\//\\\\\\//g'` 
SEDEXEC="$SEDEXEC | sed -e \"s/__BINPATH__/$TMP/g\""
TMP=`echo $ETCPATH | sed -e 's/\\//\\\\\\//g'`
SEDEXEC="$SEDEXEC | sed -e \"s/__ETCPATH__/$TMP/g\""
TMP=`echo $LIBPATH | sed -e 's/\\//\\\\\\//g'`
SEDEXEC="$SEDEXEC | sed -e \"s/__LIBPATH__/$TMP/g\""
TMP=`echo $PREFIX | sed -e 's/\\//\\\\\\//g'`
SEDEXEC="$SEDEXEC | sed -e \"s/__PREFIX__/$TMP/g\""
TMP=`echo $ROOTGROUP | sed -e 's/\\//\\\\\\//g'`
SEDEXEC="$SEDEXEC | sed -e \"s/__ROOTGROUP__/$TMP/g\""

TMP=`echo $PREFIX | sed -e 's/\\//\\\\\\//g'`
SEDEXEC="$SEDEXEC | sed -e \"s/__DEFINEPRE__/$TMP/g\""
SEDEXEC="$SEDEXEC | sed -e 's/__MAKEAUTO__/$MKTYPE/'"

# Search for gmake
printf "Checking for make.. "
MAKEPATH=
if test -f "/usr/bin/gmake"; then
MAKEPATH=/usr/bin/gmake
else
if test -f "/bin/gmake"; then
MAKEPATH=/bin/gmake
else
if test -f "/usr/local/bin/gmake"; then
MAKEPATH=/usr/local/bin/gmake
fi
fi
fi
if test -n "$MAKEPATH"; then
printf "found $MAKEPATH\n"
else
MAKEPATH=make
printf "not found, assume make will work\n"
fi

# Sed strip_cr
printf "Checking for perl.. "
PERLPATH=
cp -f strip_cr.in strip_cr
if test -f "/usr/bin/perl"; then
PERLPATH=/usr/bin/perl
else
if test -f "/bin/perl"; then
PERLPATH=/bin/perl
else
if test -f "/usr/local/bin/perl"; then
PERLPATH=/usr/local/bin/perl
fi
fi
fi
if test -n "$PERLPATH"; then
printf "found $PERLPATH\nEnsuring that *.c/*.h source files don't contains CR (^M).. "
TMP=`echo $PERLPATH | sed -e 's/\\//\\\\\\//g'` 
cat strip_cr | sed -e "s/__PERL__/$TMP/" >  __tmp; mv __tmp strip_cr
chmod 700 strip_cr
./strip_cr *.c *.h
printf "done\n"
fi

# Sed postinst-config
cp -f postinst-config.in postinst-config
CMD="cat postinst-config $SEDEXEC > __tmp; mv __tmp postinst-config"
if eval $CMD; then
chmod 700 postinst-config
else
echo "Error while seding postinst-config"
exit 1
fi

# Sed all __VARS__
CMD="cat Makefile $SEDEXEC > __tmp; mv __tmp Makefile"
echo "Command: $CMD"
if eval $CMD; then

echo ""
echo "Makefile created!"
echo "Type in '$MAKEPATH' to build and '$MAKEPATH install' to install"

if test -n "$DOMAKE"; then
    echo "Making.."
    eval $MAKEPATH clean
    eval $MAKEPATH
fi
if test -n "$DOINSTALL"; then
    if test -n "$NODOCINSTALL"; then
      echo "Installing binary.."
      eval $MAKEPATH bininstall
    elif test -n "$DOCINSTALL"; then
      echo "Installing docs.."
      eval $MAKEPATH docinstall
    else
      echo "Installing.."
      eval $MAKEPATH install
    fi
fi

else
    echo "Error while seding Makefile.."
    exit 1
fi

else
	echo "Error copying Makefile.in -> Makefile.. giving up"
        exit 1
fi

