#!/bin/sh

if [ -r ./Options ]; then
  . ./Options
else
  echo "Hmm...doesn't look like you've run Config..."
  echo "Doing so now."
  sh Config
  exit
fi

echo "Installing IRCD."
if [ ! $2 ]; then
  echo "Target dirs not specified."
  exit
fi
if [ ! -f src/ircd ]; then
  echo "Run make to build ircd."
  exit
fi

echo "Creating necessary dirs..."
if [ ! -d $1 ]; then
  mkdir -p $1
fi
if [ ! -d $2 ]; then
  mkdir -p $2
  chmod 700 $2
fi

echo "Copying necessary files..."
mv -f src/ircd $1/
chmod 500 $1/ircd
if [ -f src/mkpass ]; then
  mv -f src/mkpass $1/
  chmod 500 $1/mkpass
fi
if [ ! -f $2/ircd.conf ]; then
  cp doc/example.conf $2/ircd.conf
  chmod 600 $2/ircd.conf
fi

if [ "$USER" = "root" ]; then
  echo "Setting necessary rights..."
  chown $SET_UID $2
  chgrp $SET_GID $2
  chown $SET_UID $1/ircd
  chgrp $SET_GID $1/ircd
  chown $SET_UID $2/ircd.conf
  chgrp $SET_GID $2/ircd.conf
fi

echo "IRCD installed."
