#!/bin/sh
#
# Firestarter 0.9.x 'Post-Install' script
# ---
# This file moves the Firestarter application into LFS compliant directories
# as well as creating the files to allow a standard user (who is not root) to
# be able to manipulate the application themselves.
#
# Changelog:
#
# 1.5  - Dropped support for 2.2 kernels, added 2.5 to accepted list
#      - Init script pulled from netfilter.init (distributed in tarball)
#      - Added operation feedback messages
# 1.4  - Added fix for not installing GNOME related components if 
#	 gnome-core was not installed.
# 1.3  - Added logic to do some decent Kernel revision detection 
#      (so we remove the correct file when putting the rc.script in.)
# 1.2  - Added generation of a compliant rc.script to start firestarter 
#      on demand
# 1.1x - Added trap for Control-C, 
#        Fixed the symbolic link to console_helper,
#        Added root verification, (the old postinstall script died 
#        on RH / SuSE boxes when run by a non-root user)
#        Don't create new directories for {pam.d / console.apps} if they 
#        already exist
# 1.1  - Added shabang! so distributions with weird locations to 'sh' can cope
# 1.0x - Added variables for required system binaries
# 1.0  - Initial Release

# Must be root to execute this script
if [ $UID -ne 0 ] ; then
    echo "$0: Sorry, you must be root to run this script"
    exit 1
fi

trap '' 2

# System binaries (possibly RH specific)
CHKCONFIG='/sbin/chkconfig'
CONSOLEHELP='/usr/sbin/consolehelper'
FIRESTARTER='' 

# Path to installed Firestarter
if [ -e /usr/local/bin/firestarter ]; then
  FIRESTARTER='/usr/local/bin/firestarter'
else
  if [ -e /usr/bin/firestarter ]; then
    FIRESTARTER='/usr/bin/firestarter'
  else
    if [ -e /opt/bin/firestarter ]; then
      FIRESTARTER='/opt/bin/firestarter'
    fi
  fi
fi

if [ "$FIRESTARTER" == '' ]; then
  echo -e "Unable to determine location of installed Firestarter binary:"
  echo -e "You should install the actual firestarter binary in /usr/sbin/ and provide a symlink to it in /usr/bin\n"
else
  mv $FIRESTARTER /usr/sbin/
  ln -s $CONSOLEHELP /usr/bin/firestarter
fi


if [ ! -d /etc/pam.d ]; then

mkdir -p /etc/pam.d

fi

cat > /etc/pam.d/firestarter < firestarter.pam

echo "Created /etc/pam.d/firestarter PAM authentication configuration file"


if [ ! -d /etc/security/console.apps ]; then

mkdir -p /etc/security/console.apps

fi

cat > /etc/security/console.apps/firestarter < firestarter.console

echo "Created /etc/security/console.apps/firestarter console-access privilege file "


if  [ -e netfilter.init ]; then

  if [ `uname -r` \> '2.3' ] && [ -e /etc/rc.d/init.d/iptables ]; then

  $CHKCONFIG --del iptables

  cat > /etc/rc.d/init.d/firestarter < netfilter.init

  chmod 0700 /etc/rc.d/init.d/firestarter
  $CHKCONFIG --add firestarter
  $CHKCONFIG --level 35 firestarter on

  echo "Init script installed as /etc/rc.d/init.d/firestarter"

  else
  
    echo "No init script is available for your kernel version"

  fi

else

  echo -e "\nMissing netfilter.init:"
  echo -e "Run postinstall script in the firestarter source directory.\nInit script not installed\n"

fi


if [ -d /usr/share/gnome ] && [ -e /usr/local/share/pixmaps/firestarter.png ]; then

mv /usr/local/share/pixmaps/firestarter.png /usr/share/pixmaps/firestarter.png

fi

if [ -d /usr/share/gnome ] && [ -e /usr/local/share/gnome/apps/Internet/firestarter.desktop ]; then

mv /usr/local/share/gnome/apps/Internet/firestarter.desktop /usr/share/gnome/apps/Internet/firestarter.desktop

fi

echo -e "\nYou should now be able to start firestarter as a non-root user by running /usr/bin/firestarter\n"

trap 2

exit 0
