#!/bin/sh i2p_version="0.8.6" i2p_filename="i2pinstall_$i2p_version.exe" i2p_url="http://i2p.googlecode.com/files/$i2p_filename" runsvdir=/etc/sv get_dep() { dep=$1 [ "$(dpkg-query -s "$dep"|sed -ne 's/^Status: //p')" = "install ok installed" ] || sudo apt-get install "$dep" >&2 } oneword() { [ $# -eq 1 ]; } oneword $i2p_filename || { echo "$0 FAIL!" >&2; exit 1; } have() { which "$1" >/dev/null; } wget_or_curl() { if have wget; then wget -c "$1"; elif have curl; then curl -C- -O "$1"; else get_dep wget && wget_or_curl "$1" || echo "install wget or curl" >&2; false; fi; } user_exists() { getent passwd "$1" >/dev/null; } set -e do_install() { if ! user_exists i2p; then if [ -e /home/i2p ]; then echo "$0: No i2p user, but /home/i2p exists; please remove /home/i2p! Exiting." >&2 exit 1 fi sudo adduser i2p --disabled-login --gecos 'i2p anonymous router' fi if ! [ -e ~i2p/i2prouter ]; then if ! [ -e ~i2p/$i2p_filename ]; then wget_or_curl "$i2p_url" ln -s "$PWD/$i2p_filename" ~i2p/ fi su - i2p -c "echo INSTALL_PATH=~i2p | java -jar $i2p_filename -options /dev/stdin" fi } do_uninstall() { if user_exists i2p; then DELETEME=$(getent passwd i2p|cut -d: -f6) deluser i2p else DELETEME=/home/i2p fi if [ -d "$DELETEME" ]; then echo -n "Remove directory '$DELETEME' [y/N]? " >&2 if read y; then case "$y" in [yY]) echo -n 'Removing... ' >&2; rm -fr "$DELETEME"; echo 'done.' >&2;; *) echo 'Left i2p home directory in place.' >&2;; esac fi fi } do_stop() { if [ -x ~i2p/i2prouter ]; then sudo su - i2p -c './i2prouter stop' fi } do_start() { sudo su - i2p -c './i2prouter start' } case "$1" in install) do_install;; uninstall) do_stop; do_uninstall;; stop) do_stop;; start|*) do_install; do_start;; esac