#!/bin/sh

INSTALL_PREFIX=/usr/local
COMPILER=cc
COMPILER_OPTIONS=-O


# This should prevent welzed displays
# but it doesn't seem to work on all
# systems. If someone knows the answer...
alias echo=print


echo "Configuring..."
# Save the old makefile
\cp Makefile Makefile.old 2> /dev/null
echo
echo "TIP: Most of the time, you can keep the default here."
echo "If you have gcc, cc is often a simple link on gcc."
echo "Try cc -V to see... It is safe to say cc here."
echo
echo -n "What compiler do you want? [$COMPILER] "
read x
echo "$x"
if [ "$x" != "" ]
then
  COMPILER=$x
fi
echo
echo
echo "TIP: If you are using gcc, you can pass optimization"
echo "flags like -O3 to have a fast library and a fast"
echo "cruncher."
echo "If you are using SparcWorks compiler, you can use the"
echo "-fast flag to act the same way."
echo "With most other compiler, the -O option should give"
echo "pretty similar results. If you know the best speed"
echo "optimisation flags, put them here..."
echo
echo -n "What options do you want to pass? [$COMPILER_OPTIONS] "
read x
echo "$x"
if [ "$x" != "" ]
then
  COMPILER_OPTIONS=$x
fi
echo
echo
echo "TIP: /usr/local is a good place for these libs..."
echo
echo -n "Where shall I install the lib/includes? [$INSTALL_PREFIX] "
read x
if [ "$x" != "" ]
then
  INSTALL_PREFIX=$x
fi
echo
echo
echo "The following options were chosen:"
echo "COMPILER=$COMPILER"
echo "OPTIONS =$COMPILER_OPTIONS"
echo "INSTALL =$INSTALL_PREFIX"
echo -n "Is it right? [O/n]"
read x
if [ "$x" = "n" ]
then
  ./configure
else
  echo -n "Creating Makefile... "
  (
  echo "##########################################################"
  echo "################# CONFIGURABLE PART ######################"
  echo "# Compiler"
  echo "CC	=	$COMPILER"
  echo "#Compiler options"
  echo "CFLAGS	=	$COMPILER_OPTIONS"
  echo "# Archiver"
  echo "AR	=	ar"
  echo "# Where shall we install the files"
  echo "INSTALL_PREFIX	=	$INSTALL_PREFIX"
  echo
  echo
  echo "##########################################################"
  echo "################# YOU SHOULDN'T TOUCH THIS ###############"
  echo "# Vars"
  echo "INCDIRS ="
  echo "LIBDIRS	="
  echo "LIBS ="
  echo
  echo "SRCS	=	common.c buffer.c header.c hash.c crc.c huffman.c ad_huff.c lzw.c lzss.c"
  echo "OBJS	=	common.o buffer.o header.o hash.o crc.o huffman.o ad_huff.o lzw.o lzss.o"
  echo
  echo "TARGETS	=	libcrunch.a crunch decrunch"
  echo
  echo
  echo "# Rules"
  echo "all:	\${TARGETS}"
  echo
  echo "libcrunch.a: \${OBJS}"
  echo "	@\\\rm -f libcrunch.a"
  echo "	\${AR} cqv libcrunch.a \${OBJS}"
  echo
  echo "crunch: libcrunch.a crunch.o"
  echo "	\${CC} crunch.o -L. -lcrunch -o crunch"
  echo
  echo "decrunch: libcrunch.a decrunch.o"
  echo "	\${CC} decrunch.o -L. -lcrunch -o decrunch"
  echo
  echo "install: \${TARGETS}"
  echo "	@cp ./libcrunch.a \${INSTALL_PREFIX}/lib"
  echo "	@cp ./crunch.h \${INSTALL_PREFIX}/include"
  echo "	@cp ./crunch ./decrunch \${INSTALL_PREFIX}/bin"
  echo
  echo "clean:"
  echo "	\\\rm -f \${TARGETS} *.o *\\~ core Makefile.bak"
  echo
  echo "depend:"
  echo "	makedepend \${INCDIRS} \${SRCS}"
  ) > Makefile
  echo "done."
  echo
  echo "Now computing dependencies..."
  make depend
  echo "done."
  echo
  echo "Now you can make a:"
  echo "  make all"
  echo "  make install"
  echo
fi
