#!/bin/bash
# Script by S/ash
# Shiting automake don't want to work so i did my own script
# Create makefile for rtcscan
#

VERSIONSTR='v1.0'
VERSIONNB='0x0100'
AVAILABLE_LANG='en fr'
LANG='fr'
DEBUG=false
DEBUGDATA=false
INCLUDES=/usr/include
INSTALLBIN=/usr/local/bin
INSTALLDOC=/usr/doc/rtcscan
SOURCE_LIST='arguments interface main rtcscan echoscan ftpscan ipraw portfile udptcpscan'
INC_LIST='stdio.h stdlib.h pwd.h errno.h fcntl.h unistd.h string.h ctype.h sys/time.h sys/types.h netdb.h sys/socket.h netinet/in.h netinet/ip.h netinet/tcp.h netinet/udp.h netinet/ip_icmp.h arpa/inet.h'

MSG()
{
cat << EOF
================================================================================
RtC Scan $VERSIONSTR : a network mapper
  A RtC Tech Software, part of the RtC Neset Project
  RtC    : http://www.rtc.fr.st  -  rtc@fr.st
  Author : S/ash <slash-rtc@fr.st>
Configuration Script
================================================================================
EOF
}

CheckIncludes()
{
for c in $INC_LIST
do
  echo -n "Checking $c... "
  FILE="$INCLUDES/$c"
  if [ -f $FILE ]; then
    echo "ok"
  else
    echo "not found"
    exit
  fi
done
}

BuildMakeFile()
{
if [ "$DEBUG" = "true" ]; then
  CCFLAGS='-g -c'
else
  CCFLAGS='-c'
fi

cat >Makefile << EOF
# RtC Scan $VERSIONSTR : a network mapper
#   A RtC Tech Software, part of the RtC Neset Project
#   RtC    : http://www.rtc.fr.st  -  rtc@fr.st
#   Author : S/ash <slash-rtc@fr.st>

all:		disp mdir compile link clean

allinstall:	all install

disp:
		@echo "RtC Scan $VERSIONSTR : a network mapper"
		@echo "  A RtC Tech Software, part of the RtC Neset Project"
		@echo "  RtC    : http://www.rtc.fr.st  -  rtc@fr.st"
		@echo "  Author : S/ash <slash-rtc@fr.st>"
 
mdir:
		@mkdir -p obj

link:
		gcc -o rtcscan obj/*.o

install:
		@cp -f rtcscan     $INSTALLBIN
		@chmod 0755 $INSTALLBIN/rtcscan 
		@mkdir -p          $INSTALLDOC 
		@cp -f README.*    $INSTALLDOC 
		@cp -f netmapping  $INSTALLDOC
		@cp -f changelog.* $INSTALLDOC
		@cp -f portlist.*  $INSTALLDOC
		@cp -f license     $INSTALLDOC
		@cp -f BUGS        $INSTALLDOC
		@chmod -R 0644 $INSTALLDOC/*
		@chmod 0755 $INSTALLDOC

clean:
		@rm -rf obj
		@rm -f  *.o
		@rm -f  *~
		@rm -f  src/*.o
		@rm -f  src/*~
		@rm -f  core
		@rm -f  src/core

EOF

for c in $SOURCE_LIST
do
  cat >>Makefile << EOF
$c.o:
		gcc $CCFLAGS -o obj/$c.o  src/$c.c

EOF
done

echo -n "compile:	" >>Makefile

for c in $SOURCE_LIST
do
  echo -n "$c.o " >>Makefile
done

cat >>Makefile << EOF

EOF
}

BuildConfigHeader()
{
cat >src/config.h <<EOF
/* langage  */
#define RTCSCAN_LANG                "$LANG"

/* version   */
#define VER_NB                       $VERSIONNB
#define VER_STR                     "$VERSIONSTR"

EOF

if [ "$DEBUG" = "true" ]; then
cat >>src/config.h <<EOF

#define DEBUGGING
EOF

if [ "$DEBUGDATA" = "true" ]; then
cat >>src/config.h <<EOF

#define DEBUGGING_DATA
EOF
fi

fi
}

if [ "$1" = "-r" ]; then
rm -f Makefile
rm -f src/config.h
rm -f rtcscan
else

if [ "$1" = "-d"  -o  "$1" = "-D" ]; then
DEBUG=true
if [ "$1" = "-D" ]; then
DEBUGDATA=true
fi
fi

MSG

echo -n "Choose language [$AVAILABLE_LANG]... "
read ans

if [ ! "$ans" = "" ]; then

case "$ans" in
  *" "*) echo "$ans is not a correct language string"
         exit 1
esac

case " $AVAILABLE_LANG " in
   *" $ans "*) LANG=$ans;;
   *)          echo "$ans is not supported by rtcscan"
               exit 1
esac

fi

echo "using language $LANG..."

CheckIncludes
BuildMakeFile
BuildConfigHeader

fi

exit 0
