#!/bin/sh

# I2P Installer - Installs and pre-configures I2P.
#
# install_i2p_service_unix
# 2004 The I2P Project
# http://www.i2p2.de/
# This code is public domain.
#
# author: hypercubus
#
# Installs I2P as a service on various *nix systems using Java Service Wrapper.
# This script must be run as root.
#
# Java Service Wrapper can be found at:
# http://wrapper.tanukisoftware.org/doc/english/introduction.html

if [ ! "X$USER" = "Xroot" ]; then
    echo "Sorry, you need root privileges to install services."
    exit 1
fi

ERROR_MSG="Cannot determine operating system type. Please install the service manually."
HOST_OS=`./osid`

if [ "X$HOST_OS" = "X" -o "$HOST_OS" = "unknown" ]; then
    echo "$ERROR_MSG"
    exit 1
fi

# The following are several different service installation methods covering some
# of the major *nix operating systems. Most *nix OSes should be able to use one
# of these styles. TODO: AIX, HP-UX, HP-UX/64, IRIX, OSF/1.

install_bsd()
{
    ln -sf `pwd`/i2prouter /usr/local/etc/rc.d/i2prouter.sh
}

install_debian()
{
    ln -sf `pwd`/i2prouter /etc/init.d/i2prouter
    update-rc.d i2prouter start 20 2 3 4 5 . stop 20 0 1 6 .
}

install_gentoo()
{
    ln -sf `pwd`/i2prouter /etc/init.d/i2prouter
    rc-update add i2prouter default
}

install_redhat()
{
    ln -sf `pwd`/i2prouter /etc/rc.d/init.d/i2prouter
    chkconfig --level 345 i2prouter on
}

install_sysv()
{
    ln -sf `pwd`/i2prouter /etc/init.d/i2prouter
    ln -sf /etc/init.d/i2prouter /etc/rc0.d/K20i2prouter
    ln -sf /etc/init.d/i2prouter /etc/rc1.d/K20i2prouter
    ln -sf /etc/init.d/i2prouter /etc/rc2.d/S20i2prouter
    ln -sf /etc/init.d/i2prouter /etc/rc3.d/S20i2prouter
}

case $HOST_OS in
    debian )
        install_debian
        ;;
    fedora | mandrake | redhat | suse )
        install_redhat
        ;;
    freebsd | osx )
        install_bsd
        ;;
    gentoo )
        install_gentoo
        ;;
    solaris )
        install_sysv
        ;;
    * )
        echo "$ERROR_MSG"
        exit 1
        ;;
esac

exit 0
