#!/bin/bash

VERSION=0.5.1

function rootcheck {
 if [ "$UID" != "0" ]; then
  echo -e "\033[40m\033[1;34mERROR: Root check FAILED (you MUST be root to install this program)! Quitting...\033[0m"
  exit 1
 fi
}

function check_old {
 if [ -d /etc/ipkungfu ] ; then
  echo "An existing ipkungfu installation has been"
  echo "detected.  The installer will back up your"
  echo "old configuration directory to"
  echo "/etc/bakipkungfu (deleting that directory"
  echo "first if it already exists) and create a new"
  echo "/etc/ipkungfu directory.  Press ctrl-c now"
  echo "to exit the installer, or press enter to"
  echo "continue."
  read foo
  rm -rf /etc/bakipkungfu
  mv /etc/ipkungfu /etc/bakipkungfu
  mkdir /etc/ipkungfu
 fi
}

function installinit {
 if [ -d /etc/init.d ] ; then
  echo "Installing init script to /etc/init.d..."
  cp -f files/rc.ipkungfu /etc/init.d/ipkungfu
  chmod 744 /etc/init.d/ipkungfu
  chkconfig --add ipkungfu > /dev/null 2>&1
  chkconfig ipkungfu on > /dev/null 2>&1
 else
  if [ -d /etc/rc.d/init.d ] ; then
   echo "Installing init script to /etc/rc.d/init.d..."
   cp -f files/rc.ipkungfu /etc/init.d/ipkungfu
   chmod 744 /etc/init.d/rc.d/ipkungfu
   chkconfig --add ipkungfu > /dev/null 2>&1
   chkconfig ipkungfu on > /dev/null 2>&1
  fi
 fi
}

function installdocs {
 echo "Installing man page..."
 rm -f /usr/share/man/man8/ipkungfu*
 cp -f files/ipkungfu.8.bz2 /usr/share/man/man8
 echo "Installing documentation to /usr/share/doc/ipkungfu-$VERSION..."
 if [ ! -d /usr/share/doc/ipkungfu-$VERSION ] ; then
  mkdir /usr/share/doc/ipkungfu-$VERSION
 fi
 cp -f Changelog /usr/share/doc/ipkungfu-$VERSION
 cp -f README /usr/share/doc/ipkungfu-$VERSION
 cp -f FAQ /usr/share/doc/ipkungfu-$VERSION
 cp -f COPYRIGHT /usr/share/doc/ipkungfu-$VERSION
}

function installbin {
 if [ ! -d /usr/local/sbin ] ; then
  echo -e "Creating directory \033[40m\033[1;34m/usr/local/sbin\033[0m..."
  mkdir /usr/local/sbin
 fi
 echo -e "Copying \033[40m\033[1;34m`pwd`/ipkungfu\033[0m to /usr/local/sbin..."
 cp -f ipkungfu /usr/local/sbin
}

function installconf {
 if [ ! -e /etc/ipkungfu/ipkungfu.conf ] ; then
  echo -e "Installing /etc/ipkungfu/ipkungfu.conf...  \033[40m\033[1;34mEDIT THIS FILE!\033[0m"
  cp files/ipkungfu.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/advanced.conf ] ; then
  echo "Installing /etc/ipkungfu/advanced.conf..."
  cp files/advanced.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/custom.conf ] ; then
  echo "Installing /etc/ipkungfu/custom.conf..."
  cp files/custom.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/log.conf ] ; then
  echo "Installing /etc/ipkungfu/log.conf..."
  cp files/log.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/vhosts.conf ] ; then
  echo "Installing /etc/ipkungfu/vhosts.conf"
  cp files/vhosts.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/accept_hosts.conf ] ; then
  echo "Installing /etc/ipkungfu/accept_hosts.conf"
  cp files/accept_hosts.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/deny_hosts.conf ] ; then
  echo "Installing /etc/ipkungfu/deny_hosts.conf"
  cp files/deny_hosts.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/redirect.conf ] ; then
  echo "Installing /etc/ipkungfu/redirect.conf"
  cp files/redirect.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/forward.conf ] ; then
  echo "Installing /etc/ipkungfu/forward.conf"
  cp files/forward.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/pre.conf ] ; then
  echo "Installing /etc/ipkungfu/pre.conf"
  cp files/pre.conf /etc/ipkungfu
 fi
 if [ ! -f /etc/ipkungfu/post.conf ] ; then
  echo "Installing /etc/ipkungfu/post.conf"
  cp files/post.conf /etc/ipkungfu
 fi
}

# Make Directories
if [ ! -d /etc/ipkungfu ] ; then
 echo -e "Creating directory \033[40m\033[1;34m/etc/ipkungfu\033[0m..."
 mkdir /etc/ipkungfu
 chmod 700 /etc/ipkungfu
fi
if [ ! -d /usr/local/sbin ] ; then
 echo -e "Creating directory \033[40m\033[1;34m/usr/local/sbin\033[0m..."
 mkdir /usr/local/sbin
fi

rootcheck
installdocs
installinit
installbin
installdocs
check_old
installconf

echo  " Installation complete."
