#!/bin/sh
#
# Homemade configure file for the ELF shell project
#
# The ELFsh crew.
#

CHOSENBIT="0"
STATIC="0"
READLN="0"
ELFSHNET="0"

echo > libvars.h
echo > vars.h
echo > config.h

while [ $# -ne 0 ]; do

case $1 in

--enable-64*)
if [ $CHOSENBIT = "1" ]; then
    echo
    echo "Error: enable-32 and enable-64 are incompatible."
    echo
    rm -f config.h
    exit
fi
echo  " [*] Enabling ELF64 support"
BITS="-DELFSH64"
echo  "BITS		= -DELFSH64"		>> config.h
CHOSENBIT="1"
;;

--enable-32*)
if [ $CHOSENBIT = "1" ]; then
    echo
    echo "Error: enable-32 and enable-64 are incompatible."
    echo
    rm -f config.h
    exit
fi
echo  " [*] Enabling ELF32 support"
BITS="-DELFSH32"
echo  "BITS = -DELFSH32"		>> config.h
CHOSENBIT="1"
;;

--enable-network*)
echo  " [*] Enable distributed ELFsh"
ELFSHNET="1"
echo  "LDDUMPOPT		= -ldumpelfsh"		>> config.h
BITS=$BITS" -DELFSHNET"
echo "BITS = " $BITS			>> config.h
;;

--enable-readline*)
echo  " [*] Enabling readline support"
READLN="1"
echo  "READLNOPT	= -DUSE_READLN"		>> config.h
echo  "LDREADLNOPT	= -lreadline -ltermcap" >> config.h
;;

--enable-static*)
echo  " [*] Enabling static linking"
echo  "STATOPT		= -static"              >> config.h
echo  "STATOPT2		= -DUSE_STATIC"		>> config.h
STATIC="1"
;;

--enable-dmalloc*)
echo  " [*] Enabling dmalloc linking"
echo  "EXTRAOPT		= $EXTRAOPT -ldmalloc"            >> config.h
;;

--enable-m64*)
echo  " [*] Enabling ELFsh 64bits binary built"
echo  "BUILDOPT		= -m64 -DM64"				>> config.h
M64="1"
;;

--set-shell*)
shift
echo  " [*] Setting UNIX shell to " $1
echo  "#define ELFSH_SHELL        " $1		>> vars.h
SHELLSET="1"
;;

--set-modpath*)
shift
echo " [*] Setting ELFsh modules path to " $1
echo "#define ELFSH_MODPATH              " $1   >> vars.h
MODPATHSET="1"
;;

--help*)
CHOSENBIT="0"
break;
;;

*)
echo "Invalid configure option. Try --help."
echo
rm -f config.h Makefile
exit

esac;

shift

done


if [ $CHOSENBIT = "0" ]; then
    echo "Error: you MUST specify --enable-32 OR --enable-64."
    echo
    echo "   Available parameters :" 
    echo
    echo "   --enable-32       : Build a ELF32 capable shell."
    echo "   --enable-64       : Build a ELF64 capable shell."
    echo "   --enable-readline : Link with readline"
    echo "   --enable-network  : Build the distributed extension."
    echo "   --enable-static   : Link in static."
    echo "   --enable-dmalloc  : Link with Electric Fence."
    echo "   --enable-m64      : Build ELFsh itself as 64bits binary (default: 32bits)"
    echo
    echo "   Available variables :"
    echo
    echo "   --set-shell       : Set your UNIX shell path."
    echo "   --set-modpath     : Set your ELFsh modules path."
    echo
    echo "   All options are disabled by default."
    echo
    rm -f config.h vars.h Makefile
    exit
fi

#if  [ $READLN = "1" -a $ELFSHNET = "1" ]; then
#    echo
#    echo "Error: Readline and Distributed ELFsh are incompatible."
#    echo
#    rm -f config.h vars.h
#    exit
#fi



# Set default variables in case it was not precised

if [ "$MODPATHSET" != "1" ]; then
    echo "#define ELFSH_MODPATH    \"/usr/share/elfsh/\"" 	>> vars.h
fi

if [ "$SHELLSET" != "1" ]; then
    echo "#define ELFSH_SHELL      \"/bin/bash\""		>> vars.h
fi

if [ "$BUILDOP" != "1" ];  then
    echo "BUILDOP = -DM32"					>> config.h
fi

echo  " [*] Enabling libasm support"
echo  "LDASMOPT	= -lasm"		>> config.h


# Now do the OS dependant configuration
case `uname` in

SunOS*)

if [ "$STATIC" = "0" ]; then
    echo  " [*] Enabling dynamic linking."
    echo  "DLOPT		= -ldl"   >> config.h
fi

    EXTRAOPT="$EXTRAOPT -lnsl -lsocket -lresolv"
    echo "EXTRAOPT = " $EXTRAOPT	  >> config.h

	;;

Linux*)

if [ "$STATIC" = "0" ]; then
    echo  " [*] Enabling dynamic linking."
    echo  "DLOPT		= -ldl"       >> config.h
    echo  "DYNOPT		= -rdynamic"  >> config.h
fi

if [ "$ELFSHNET" = "1" ]; then
    EXTRAOPT="$EXTRAOPT -lutil"
    echo  "EXTRAOPT = " $EXTRAOPT	  >> config.h
fi

    ;;

HP-UX*)
    echo "#define HPUX 1"		  >> libvars.h
    ;;

*)

if [ "$STATIC" = "0" ]; then
    echo  " [*] Enabling dynamic linking."
    echo  "DYNOPT                = -rdynamic"   >> config.h
fi

if [ "$ELFSHNET" = "1" ]; then
    EXTRAOPT="$EXTRAOPT -lutil"
    echo "EXTRAOPT = " $EXTRAOPT		>> config.h
fi

    ;;

esac



echo "##"							 > Makefile 
echo "## Makefile "						>> Makefile
echo "## "							>> Makefile
echo "## Started on  Fri Dec  7 06:54:35 2001 mayhem "		>> Makefile
echo "## Automatically generated from the configure script"	>> Makefile
echo "## "							>> Makefile
echo ""								>> Makefile
echo ""								>> Makefile
echo "include ./config.h "					>> Makefile
echo " "							>> Makefile
echo "RM      = rm -f "						>> Makefile
echo "ETAGS   = etags "					        >> Makefile
echo "CTAGS   = ctags "						>> Makefile
echo "BASEPATH = \$(DESTDIR)/usr"				>> Makefile
echo "MODPATH = \$(BASEPATH)/share/elfsh/"			>> Makefile
echo "BINPATH = \$(BASEPATH)/bin/"				>> Makefile
echo "LIBPATH = \$(BASEPATH)/lib/"				>> Makefile
echo "INCPATH = \$(BASEPATH)/include/"				>> Makefile
echo "MANPATH = \$(BASEPATH)/share/man"				>> Makefile

echo " "							>> Makefile
echo "all     : world final modules tags"			>> Makefile
echo " "							>> Makefile

echo "world   : "						>> Makefile
echo "	@echo 'Builting libasm...' "				>> Makefile
echo "	@cd libasm && \$(MAKE) "				>> Makefile
echo "	@echo 'Libasm has been built successfully.' "		>> Makefile
echo " "						       	>> Makefile
echo "	@echo 'Builting libelfsh...' "				>> Makefile
echo "	@cd libelfsh && \$(MAKE) "				>> Makefile
echo "	@echo 'Libelfsh has been built successfully.' "		>> Makefile
echo " "						       	>> Makefile
echo "	@echo 'Builting libhashelfsh...' "			>> Makefile
echo "	@cd libhash && \$(MAKE) -s "				>> Makefile
echo "	@echo 'Libhash has been built successfully.' "		>> Makefile
echo " "							>> Makefile


if [ "$ELFSHNET" = "1" ]; then

echo "	@echo 'Builting libdumpelfsh...' "			>> Makefile
echo "	@cd libdump && \$(MAKE) -s "				>> Makefile
echo "	@echo 'Libdump has been built successfully.' "		>> Makefile
echo " "							>> Makefile
echo " "							>> Makefile

fi

echo "modules: "						>> Makefile
echo "	@echo 'Building ELFsh modules suite...' "		>> Makefile
echo "	@cd modules && \$(MAKE) "				>> Makefile
echo "	@echo 'ELFsh modules suite built successfully.' "	>> Makefile
echo " "							>> Makefile

echo "install : mod_install "					>> Makefile
echo "	@cp vm/elfsh \$(BINPATH)/ "				>> Makefile
echo "	@cp libelfsh/libelfsh.a libelfsh/libelfsh.so \$(LIBPATH) " >> Makefile
echo "	@cp libasm/libasm.a \$(LIBPATH) "			>> Makefile
echo "	@cp -R libelfsh/include/* \$(INCPATH) "			>> Makefile
echo "	@cp libasm/include/libasm.h \$(INCPATH)"		>> Makefile
echo "	@cp libasm/include/libasm-i386.h \$(INCPATH) "		>> Makefile
echo "	@cp doc/elfsh.1 \$(MANPATH)/man1/ "			>> Makefile
echo "	@chmod 755 \$(MANPATH)/man1/elfsh.1 "			>> Makefile
echo "	@chmod 755 \$(BINPATH)/elfsh "				>> Makefile
echo "	@chmod 755 \$(LIBPATH)libelfsh.so "			>> Makefile
echo "	@chmod 755 \$(LIBPATH)e2dbg.so "			>> Makefile
echo "	@chmod 755 \$(INCPATH)libelfsh*.h "			>> Makefile
echo "	@chmod 755 \$(LIBPATH)libelfsh.a "			>> Makefile
echo "	@chmod 755 \$(LIBPATH)libasm.a "			>> Makefile
echo "	@chmod 755 \$(INCPATH)libelfsh*.h "			>> Makefile
echo "	@chmod 755 \$(INCPATH)libasm*.h "			>> Makefile
echo "	@echo 'ELFsh, Libelfsh, and Libasm installed successfully .'"	>> Makefile
echo " "							>> Makefile 

echo "final:					 	"	>> Makefile
echo "	@echo 'Builting ELFsh ET_EXEC'			"	>> Makefile
echo "	@cd vm && \$(MAKE)			     	"       >> Makefile
echo "	@echo 'ELFsh ET_EXEC has been built successfully.' "	>> Makefile
echo " "							>> Makefile

echo "e2dbg: world et_dyn modules tags			"	>> Makefile
echo ""								>> Makefile

echo "et_dyn:						"	>> Makefile
echo "	@echo 'Builting ELFsh ET_DYN' "				>> Makefile
echo "	@cd vm && \$(MAKE) et_dyn		     	"       >> Makefile
echo "	@echo 'ELFsh ET_DYN has been built successfully.' "	>> Makefile
echo " "							>> Makefile

echo "mod_install: "						>> Makefile
echo "	@mkdir \$(MODPATH) 2>/dev/null || true "		>> Makefile
echo "	@cp modules/*.so \$(MODPATH) "				>> Makefile
echo "	@chmod -R 755 \$(MODPATH) "				>> Makefile
echo " "							>> Makefile

echo "uninstall: "						>> Makefile
echo "	rm -f \$(BINPATH)/elfsh "				>> Makefile
echo "	rm -f \$(LIBPATH)/libelfsh* "				>> Makefile
echo "	rm -f \$(LIBPATH)/e2dbg* "				>> Makefile
echo "	rm -f \$(MODPATH) "					>> Makefile
echo "	rm -f \$(MANPATH)/man1/elfsh.1 "			>> Makefile
echo "	rm -f \$(LIBPATH)/libasm.a "				>> Makefile
echo "	rm -f \$(INCPATH)/libasm*.h "				>> Makefile
echo "	rm -fr \$(INCPATH)/libelfsh*"				>> Makefile
 

echo "	@echo 'ELFsh, Libelfsh and Libasm uninstalled successfully' "	>> Makefile
echo " "							>> Makefile

echo "clean   : cleandoc "					>> Makefile
echo "	@echo 'Cleaning tree ...' "				>> Makefile
echo "	cd vm        && \$(MAKE) -s clean "			>> Makefile
echo "	cd libelfsh  && \$(MAKE) -s clean "			>> Makefile
echo "	cd libasm    && \$(MAKE) -s clean "			>> Makefile
echo "	cd libhash   && \$(MAKE) -s clean "			>> Makefile
echo "	cd libdump   && \$(MAKE) -s clean "			>> Makefile
echo "	cd modules   && \$(MAKE) -s clean "			>> Makefile
echo "	@echo 'Tree cleaned .' "				>> Makefile
echo " "							>> Makefile

echo "fclean  : cleandoc "					>> Makefile
echo "	@echo 'Fcleaning tree ...' "				>> Makefile
echo "	cd vm        && \$(MAKE) -s fclean "			>> Makefile
echo "	cd libelfsh  && \$(MAKE) -s fclean "			>> Makefile
echo "	cd libasm    && \$(MAKE) -s fclean "			>> Makefile
echo "	cd libhash   && \$(MAKE) -s fclean "			>> Makefile
echo "	cd libdump   && \$(MAKE) -s fclean "			>> Makefile
echo "	cd modules   && \$(MAKE) -s fclean "			>> Makefile
echo "	@echo 'Tree cleaned .' "				>> Makefile
echo " "							>> Makefile

echo "cleandoc: "						>> Makefile
echo "	@rm -f *~ *\# \#* .\#* "				>> Makefile
echo "	@rm -f doc/*~ doc/*\# doc/\#* doc/.\#* "		>> Makefile
echo "	@rm -f doc/research/*~ doc/research/*\# "		>> Makefile
echo "	@rm -f doc/research/\#* doc/research/.\#* "		>> Makefile
echo " "							>> Makefile

echo "tags    : "						>> Makefile
echo "	@echo Doing tags ... "					>> Makefile
echo "	@\$(ETAGS) vm/*.c vm/include/*.h libelfsh/*.c \\"	>> Makefile
echo "	libelfsh/include/*.h libhash/*.c \\"			>> Makefile
echo "	libhash/include/*.h libhash/*.c \\"			>> Makefile
echo "	modules/*.c libdump/*.c \\"				>> Makefile
echo "	libasm/src/*.c libasm/src/arch/ia32/*.c \\"		>> Makefile
echo "	libasm/include/*.h \\"					>> Makefile
echo "	libdump/include/*.h "					>> Makefile
echo " "							>> Makefile

echo "info    : "						>> Makefile
echo "	@echo '###############' Total '###############'"	>> Makefile
echo "	wc -l vm/*.c libelfsh/*.c libelfsh/include/*.h \\"	>> Makefile
echo "	vm/include/*.h modules/*.c libasm/src/*.c \\"		>> Makefile
echo "  libasm/src/arch/ia32/*.c                         "      >> Makefile
echo "	@echo '###############' Total w/o headers '###'"	>> Makefile
echo "	wc -l vm/*.c libelfsh/*.c modules/*.c "			>> Makefile
echo "	@echo '###############' Libelfsh only '#######'"	>> Makefile
echo "	wc -l libelfsh/*.c "					>> Makefile
echo "	@echo '###############' Libasm only ##########'"	>> Makefile
echo "	wc -l libasm/src/*.c libasm/src/arch/ia32/*.c "		>> Makefile
echo "	@echo '###############' VM only '#############'"	>> Makefile
echo "	wc -l vm/*.c "						>> Makefile
echo "	@echo '###############' Modules only '########'"	>> Makefile
echo "	wc -l modules/*.c "					>> Makefile
echo " "							>> Makefile


echo
echo " Now make && make install"
echo 







