#!/bin/bash
# nullanvoid :: bitjammin
# This is staticly built for the versions specified in the Snort Ubuntu guide
# https://www.snort.org/documents/snort-3-1-0-0-on-ubuntu-18-20

usage()
{
  MY_ID=`basename "${0}"`;
  echo -e "Usage: \e[1;35m${MY_ID}\e[0m Aabd
          -A: Do all; apt installs, download, build and install
          -a: Perform apt installs
          -b: Perform build and install of snort packages
          -d: Perform download of snort packages
          Note: ${MY_ID} downloads and builds all in ./snort.
          Example: ${MY_ID} -A"
    exit 0;
}


apt()
{

  echo -e "\e[1;35mPeforming apt installs...\e[0m";
  APT_PKGS="autoconf
            autotools-dev 
            bison
            build-essential
            cmake
            cpputest
            ethtool
            flex
            git
            libcmocka-dev
            libdumbnet-dev
            libhwloc-dev
            libluajit-5.1-dev
            liblzma-dev
            libmnl-dev
            libnetfilter-queue-dev
            libpcap-dev
            libsqlite3-dev
            libssl-dev
            libtool
            libunwind-dev
            openssl
            pkg-config
            uuid-dev 
            wget
            zlib1g-dev"
  
  sudo apt-get install $APT_PKGS;
}


download()
{
  declare -A SNORT_PKGS;
  SNORT_PKGS=(["libsafe"]="https://github.com/rurban/safeclib/releases/download/v02092020/libsafec-02092020.tar.gz"
	      ["pcre"]="https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz"
	      ["gperf"]="https://github.com/gperftools/gperftools/releases/download/gperftools-2.8/gperftools-2.8.tar.gz"
	      ["ragel"]="http://www.colm.net/files/ragel/ragel-6.10.tar.gz" 
	      ["boost"]="https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.gz"
	      ["hyperscan"]="https://github.com/intel/hyperscan/archive/v5.3.0.tar.gz"
	      ["flatbuffers"]="https://github.com/google/flatbuffers/archive/v1.12.0.tar.gz" 
	      ["libdaq"]="https://github.com/snort3/libdaq/archive/refs/tags/v3.0.3.tar.gz"
	      ["snort"]="https://github.com/snort3/snort3/archive/refs/tags/3.1.4.0.tar.gz")

  WGET_CMD="wget --quiet --show-progress";

  for KEY in ${!SNORT_PKGS[@]}; do
    ${WGET_CMD} ${SNORT_PKGS[${KEY}]} -O $KEY.tar.gz
    if [ $? == 0 ]; then
      echo -e "\e[1;35m Downloaded ${KEY}.\e[0m";
    fi
  done
}


build_install()
{
  for TAR in $(ls *.tar.gz); do
    echo -e "\e[1;35mUntarring $TAR\e[0m";
    tar zxf $TAR -C build/
  done
  
  echo -e "\e[1;35mBuilding libsafe...\e[0m"
  cd build/libsafec-02092020.0-g6d921f
  ./configure
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuilding pcre...\e[0m"
  cd build/pcre-8.44
  ./configure;
  make;
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuilding gperftools\e[0m" 
  cd build/gperftools-2.8
  ./configure
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuidling ragel...\e[0m"
  cd build/ragel-6.10
  ./configure
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuilding hyperscan...\e[0m"
  mkdir build/hyperscan-build
  cd build/hyperscan-build
  cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBOOST_ROOT=../boost_1_74_0/ ../hyperscan-5.3.0
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuilding flatbuffers...\e[0m"
  mkdir build/flatbuffers-build
  cd build/flatbuffers-build
  cmake ../flatbuffers-1.12.0
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mBuilding libdaq..."
  cd build/libdaq-3.0.3
  ./bootstrap
  ./configure
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mUpdating shared libraries...\e[0m"
  sudo ldconfig
  
  echo -e "\e[1;35mBuilding snort...\e[0m"
  cd build/snort3-3.1.4.0
  ./configure_cmake.sh --prefix=/usr/local --enable-tcmalloc
  cd build
  make
  sudo make install
  cd ../../
  
  echo -e "\e[1;35mSnorting...\e[0m"
  /usr/local/bin/snort -V
  
  echo -e "\e[1;35mSnorting again...\e[0m"
  snort -c /usr/local/etc/snort/snort.lua
}

ALL=false;
APT=false;
BUILD=false;
DOWNLOAD=false;

while getopts "Aabd" OPT; do
  case ${OPT} in
    A)
      ALL=true;
      ;;
    a)
      APT=true;
      ;;
    b)
      BUILD=true
      ;;
    d)
      DOWNLOAD=true;
      ;;
    :)
      usage;
      ;;
    \?)
      echo "Invalid option: -${OPTARG}";
      exit 1;
      ;;
  esac
done

if [ $OPTIND == 1 ]; then
  usage;
fi

mkdir -p ./snort/build
if [ $? != 0 ]; then
  echo -e "\e[1;35mCould not make our build and download directory... exiting.\e[0m";
  exit 1;
fi

cd ./snort

if [ $ALL == true ]; then
  apt;
  download;
  build_install;
  exit;
fi

if [ $APT == true ]; then
  apt;
fi

if [ $DOWNLOAD == true ]; then
  download;
fi

if [ $BUILD == true ]; then
  build_install;
fi