source: installer/resources/i2prouter @ 8137661

Last change on this file since 8137661 was 8137661, checked in by zzz <zzz@…>, 4 years ago

i2prouter: Add root check to restart and condrestart commands (ticket #1770)

  • Property mode set to 100644
File size: 70.3 KB
Line 
1#!/bin/sh
2
3#
4# Copyright (c) 1999, 2011 Tanuki Software, Ltd.
5# http://www.tanukisoftware.com
6# All rights reserved.
7#
8# Make sure that PIDFILE points to the correct location,
9# if you have changed the default location set in the
10# wrapper configuration file.
11#
12# If this script fails to successfully invoke i2psvc on your platform,
13# try the runplain.sh script instead.
14#
15# This software is the proprietary information of Tanuki Software.
16# You shall use it only in accordance with the terms of the
17# license agreement you entered into with Tanuki Software.
18# http://wrapper.tanukisoftware.com/doc/english/licenseOverview.html
19#
20# Java Service Wrapper sh script.  Suitable for starting and stopping
21#  wrapped Java applications on UNIX platforms.
22#
23#-----------------------------------------------------------------------------
24# These settings can be modified to fit the needs of your application
25# Optimized for use with version 3.5.22 of the Wrapper.
26
27# Paths
28# Note that (percent)INSTALL_PATH, (percent)USER_HOME,  and (percent)SYSTEM_java_io_tmpdir
29# should have been replaced by the izpack installer.
30# If you did not run the installer, replace them with the appropriate paths.
31I2P="%INSTALL_PATH"
32if [ "`uname -s`" = "Darwin" ]; then
33   if [ -d "%USER_HOME/Library/Application Support" ]; then
34       I2P_CONFIG_DIR="%USER_HOME/Library/Application Support/i2p"
35   else
36       I2P_CONFIG_DIR="%USER_HOME/.i2p"
37   fi
38else
39    I2P_CONFIG_DIR="%USER_HOME/.i2p"
40fi
41I2PTEMP="%SYSTEM_java_io_tmpdir"
42# PORTABLE installation:
43# Use the following instead.
44#I2PTEMP="%INSTALL_PATH"
45
46# Application
47APP_NAME="i2p"
48APP_LONG_NAME="I2P Service"
49
50# gettext - we look for it in the path
51# fallback to echo is below, we can't set it to echo here.
52GETTEXT=$(which gettext > /dev/null 2>&1)
53
54# Where to install the systemd service
55SYSTEMD_SERVICE="/etc/systemd/system/${APP_NAME}.service"
56if grep -q systemd /proc/1/comm > /dev/null 2>&1 ; then
57    USE_SYSTEMD=1
58fi
59
60# If specified, the Wrapper will be run as the specified user.
61# IMPORTANT - Make sure that the user has the required privileges to write
62#  the PID file and wrapper.log files and that the directories exist.
63#  Failure to write the pid file will cause the Wrapper to exit.
64#  Failure to write the log file will cause the Wrapper to use CWD for the log file location.
65#
66# NOTE - This will set the user which is used to run the Wrapper as well as
67#  the JVM and is not useful in situations where a privileged resource or
68#  port needs to be allocated prior to the user being changed.
69#RUN_AS_USER=
70
71# Wrapper
72WRAPPER_CMD="$I2P/i2psvc"
73WRAPPER_CONF="$I2P/wrapper.config"
74
75# Priority at which to run the wrapper.  See "man nice" for valid priorities.
76#  nice is only used if a priority is specified.
77PRIORITY=
78
79# Location of the pid and status files.
80PIDDIR="$I2P_CONFIG_DIR"
81#PIDDIR="$I2PTEMP"
82
83# Location of the wrapper.log file
84LOGDIR="$I2P_CONFIG_DIR"
85#LOGDIR="$I2PTEMP"
86LOGFILE="$LOGDIR/wrapper.log"
87
88# If you'd like to run I2P as root (not recommended), uncomment the
89# following line
90#ALLOW_ROOT=true
91
92# FIXED_COMMAND tells the script to use a hard coded action rather than
93# expecting the first parameter of the command line to be the command.
94# By default the command will will be expected to be the first parameter.
95#FIXED_COMMAND=console
96
97# PASS_THROUGH tells the script to pass all arguments through to the JVM
98#  as is.  If FIXED_COMMAND is specified then all arguments will be passed.
99#  If not set then all arguments starting with the second will be passed.
100#PASS_THROUGH=true
101
102# If uncommented, causes the Wrapper to be shutdown using an anchor file.
103#  When launched with the 'start' command, it will also ignore all INT and
104#  TERM signals.
105#IGNORE_SIGNALS=true
106
107# Wrapper will start the JVM asynchronously. Your application may have some
108#  initialization tasks and it may be desirable to wait a few seconds
109#  before returning.  For example, to delay the invocation of following
110#  startup scripts.  Setting WAIT_AFTER_STARTUP to a positive number will
111#  cause the start command to delay for the indicated period of time
112#  (in seconds).
113#
114WAIT_AFTER_STARTUP=0
115
116# If set, wait for the wrapper to report that the daemon has started
117WAIT_FOR_STARTED_STATUS=true
118WAIT_FOR_STARTED_TIMEOUT=120
119
120# If set, the status, start_msg and stop_msg commands will print out detailed
121#   state information on the Wrapper and Java processes.
122#DETAIL_STATUS=true
123
124# By default we show a detailed usage block.  Uncomment to show brief usage.
125#BRIEF_USAGE=true
126
127# flag for using upstart when installing (rather than init.d rc.d)
128USE_UPSTART=
129
130# Source the environment variables for the locale if $LANG isn't set
131# If you want to set custom locale variables for I2P,
132# you may comment out this block and set them yourself here.
133
134if [ ! -n $LANG ]; then
135        for ENV_FILE in /etc/environment /etc/default/locale; do
136                [ -r "$ENV_FILE" ] || continue
137                [ -s "$ENV_FILE" ] || continue
138
139                for var in LANG LANGUAGE LC_ALL LC_CTYPE; do
140                        value=`egrep "^${var}=" "$ENV_FILE" | tail -n1 | cut -d= -f2`
141                        [ -n "$value" ] && eval export $var=$value
142                done
143        done
144fi
145
146# Mac OSX 10.10 requires the parameter bellow to run without a icon in the Dock.
147if [ "`uname -s`" = "Darwin" ]; then
148    export JAVA_TOOL_OPTIONS="-Djava.awt.headless=true"
149fi
150
151# When installing on On Mac OSX platforms, the following domain will be used to
152#  prefix the plist file name.
153PLIST_DOMAIN=org.tanukisoftware.wrapper
154
155# The following two lines are used by the chkconfig command. Change as is
156#  appropriate for your application.  They should remain commented.
157# chkconfig: 2345 20 80
158# description: I2P Service
159
160# Initialization block for the install_initd and remove_initd scripts used by
161#  SUSE linux distributions.
162### BEGIN INIT INFO
163# Provides: i2p
164# Required-Start: $local_fs $network $syslog
165# Should-Start:
166# Required-Stop:
167# Default-Start: 2 3 4 5
168# Default-Stop: 0 1 6
169# Short-Description: I2P Service
170# Description: I2P is a load-balanced unspoofable packet switching network
171### END INIT INFO
172
173# Do not modify anything beyond this point
174#-----------------------------------------------------------------------------
175if [ ! -e "$WRAPPER_CONF" ]; then
176       echo "Starting I2P Failed: Unable to find $WRAPPER_CONF"
177       exit 1
178fi
179
180# Workaround for Gentoo
181JAVABINARY=$(awk -F'=' '/^ *wrapper\.java\.command/{print $2}' "$WRAPPER_CONF")
182
183if [ -e /etc/gentoo-release ]; then
184    if [ $JAVABINARY = java ]; then
185        if [ -x /etc/java-config-2/current-system-vm/bin/java ]; then
186            JAVABINARY="/etc/java-config-2/current-system-vm/bin/java"
187        else
188            echo "Please set wrapper.java.command in $WRAPPER_CONF"
189            exit 1
190        fi
191    fi
192fi
193
194WRAPPER_URL="https://geti2p.net/en/misc/manual-wrapper"
195unsupported() {
196    echo "The most likely reason is that a supported version of the java"
197    echo "wrapper is not available in the I2P installation package for your"
198    echo "platform. It may be possible to manually download and install"
199    echo "a compatible wrapper for your system."
200    echo "See ${WRAPPER_URL} for hints."
201    echo
202    echo "In the meantime, you may start I2P by running the script"
203    echo "${I2P}/runplain.sh"
204    echo
205}
206
207freebsd10() {
208    echo
209    echo "The current version of the Tanuki wrapper that is bundled"
210    echo "with I2P does not support FreeBSD 10."
211    echo
212    echo "See http://trac.i2p2.de/ticket/1118#comment:13 for a work-around."
213    echo "After applying the work-around, as a normal user or a dedicated"
214    echo "i2p user, you can then run:"
215    echo "$ ./i2prouter start"
216    echo
217    echo "Or, you may also start I2P by running the script:"
218    echo "${I2P}/runplain.sh"
219    echo
220}
221
222failed() {
223    echo "**`gettext 'Failed to load the wrapper'`**"
224    case `uname -s` in
225        FreeBSD)
226            case `uname -r` in
227                10*|11*)
228                    freebsd10
229                    ;;
230                *)
231                    # We should never get here on recent versions of FreeBSD
232                    if ! $(pkg info -E 'libiconv*' > /dev/null 2>&1); then
233                        echo
234                        echo "The wrapper requires libiconv."
235                        echo
236                        echo "It can be installed with pkg install libiconv"
237                        echo
238                    fi
239                    ;;
240            esac
241            ;;
242
243        *)
244            echo
245            unsupported
246            ;;
247    esac
248    exit 1
249}
250
251if [ -n "$FIXED_COMMAND" ]
252then
253    COMMAND="$FIXED_COMMAND"
254else
255    COMMAND="$1"
256fi
257
258
259# Required for HP-UX Startup
260if [ `uname -s` = "HP-UX" -o `uname -s` = "HP-UX64" ] ; then
261        PATH=$PATH:/usr/bin
262fi
263
264# Get the fully qualified path to the script
265case $0 in
266    /*)
267        SCRIPT="$0"
268        ;;
269    *)
270        PWD=`pwd`
271        SCRIPT="$PWD/$0"
272        ;;
273esac
274
275# Resolve the true real path without any sym links.
276CHANGED=true
277while [ "X$CHANGED" != "X" ]
278do
279    # Change spaces to ":" so the tokens can be parsed.
280    SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
281    # Get the real path to this script, resolving any symbolic links
282    TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
283    REALPATH=
284    for C in $TOKENS; do
285        # Change any ":" in the token back to a space.
286        C=`echo $C | sed -e 's;:; ;g'`
287        REALPATH="$REALPATH/$C"
288        # If REALPATH is a sym link, resolve it.  Loop for nested links.
289        while [ -h "$REALPATH" ] ; do
290            LS="`ls -ld "$REALPATH"`"
291            LINK="`expr "$LS" : '.*-> \(.*\)$'`"
292            if expr "$LINK" : '/.*' > /dev/null; then
293                # LINK is absolute.
294                REALPATH="$LINK"
295            else
296                # LINK is relative.
297                REALPATH="`dirname "$REALPATH"`""/$LINK"
298            fi
299        done
300    done
301
302    if [ "$REALPATH" = "$SCRIPT" ]
303    then
304        CHANGED=""
305    else
306        SCRIPT="$REALPATH"
307    fi
308done
309
310# Get the location of the script.
311REALDIR=`dirname "$REALPATH"`
312# Normalize the path
313REALDIR=`cd "${REALDIR}"; pwd`
314
315# If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
316#  the working directory is later changed.
317FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
318if [ "$FIRST_CHAR" != "/" ]
319then
320    PIDDIR=$REALDIR/$PIDDIR
321fi
322# Same test for WRAPPER_CMD
323FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
324if [ "$FIRST_CHAR" != "/" ]
325then
326    WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
327fi
328# Same test for WRAPPER_CONF
329FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
330if [ "$FIRST_CHAR" != "/" ]
331then
332    WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
333fi
334
335# Process ID
336ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
337COMMANDFILE="$PIDDIR/$APP_NAME.command"
338STATUSFILE="$PIDDIR/$APP_NAME.status"
339JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
340PIDFILE="$PIDDIR/$APP_NAME.pid"
341LOCKDIR="/var/lock/subsys"
342LOCKFILE="$LOCKDIR/$APP_NAME"
343pid=""
344
345# Resolve the location of the 'ps' command
346PSEXE="/usr/ucb/ps"
347    if [ ! -x "$PSEXE" ]
348    then
349        PSEXE="/usr/bin/ps"
350        if [ ! -x "$PSEXE" ]
351        then
352            PSEXE="/bin/ps"
353            if [ ! -x "$PSEXE" ]
354            then
355                echo 'Unable to locate "ps".'
356                echo 'Please report this message along with the location of the command on your system.'
357                exit 1
358            fi
359        fi
360    fi
361
362TREXE="/usr/bin/tr"
363if [ ! -x "$TREXE" ]
364then
365    TREXE="/bin/tr"
366    if [ ! -x "$TREXE" ]
367    then
368        echo 'Unable to locate "tr".'
369        echo 'Please report this message along with the location of the command on your system.'
370        exit 1
371    fi
372fi
373
374
375# Resolve the os
376DIST_OS=`uname -s | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
377case "$DIST_OS" in
378    'sunos')
379        DIST_OS="solaris"
380        ;;
381    'hp-ux' | 'hp-ux64')
382        # HP-UX needs the XPG4 version of ps (for -o args)
383        DIST_OS="hpux"
384        UNIX95=""
385        export UNIX95
386        ;;
387    'darwin')
388        DIST_OS="macosx"
389        ;;
390    'unix_sv')
391        DIST_OS="unixware"
392        ;;
393    'gnu/kfreebsd')
394        DIST_OS="kfreebsd"
395        ;;
396    'os/390')
397        DIST_OS="zos"
398        ;;
399esac
400
401# Resolve the architecture
402if [ "$DIST_OS" = "macosx" ]
403then
404    OS_VER=`sw_vers | grep ProductVersion | cut -d: -f2 | sed -e 's/[^0-9]*//'`
405    DIST_ARCH="universal"
406    if [ $(sysctl -n hw.cpu64bit_capable) -eq 1 ]; then
407        DIST_BITS="64"
408    else
409        DIST_BITS="32"
410    fi
411    APP_PLIST_BASE=${PLIST_DOMAIN}.${APP_NAME}
412    APP_PLIST=${APP_PLIST_BASE}.plist
413else
414    DIST_ARCH=`uname -m 2>/dev/null | $TREXE "[A-Z]" "[a-z]" | $TREXE -d ' '`
415    case "$DIST_ARCH" in
416        'athlon' | 'i386' | 'i486' | 'i586' | 'i686')
417            DIST_ARCH="x86"
418            if [ "${DIST_OS}" = "solaris" ] ; then
419                DIST_BITS=`isainfo -b`
420            else
421                DIST_BITS="32"
422            fi
423            ;;
424        amdfx* | 'amd64' | 'x86_64')
425            DIST_ARCH="x86"
426            DIST_BITS="64"
427            ;;
428        'ia32')
429            DIST_ARCH="ia"
430            DIST_BITS="32"
431            ;;
432        'ia64' | 'ia64n' | 'ia64w')
433            DIST_ARCH="ia"
434            DIST_BITS="64"
435            ;;
436        'ip27')
437            DIST_ARCH="mips"
438            DIST_BITS="32"
439            ;;
440        'power' | 'powerpc' | 'power_pc' | 'ppc64')
441            if [ "${DIST_ARCH}" = "ppc64" ] ; then
442                DIST_BITS="64"
443            else
444                DIST_BITS="32"
445            fi
446            DIST_ARCH="ppc"
447            if [ "${DIST_OS}" = "aix" ] ; then
448                if [ `getconf KERNEL_BITMODE` -eq 64 ]; then
449                    DIST_BITS="64"
450                else
451                    DIST_BITS="32"
452                fi
453            fi
454            ;;
455        'pa_risc' | 'pa-risc')
456            DIST_ARCH="parisc"
457            if [ `getconf KERNEL_BITS` -eq 64 ]; then
458                DIST_BITS="64"
459            else
460                DIST_BITS="32"
461            fi
462            ;;
463        'sun4u' | 'sparcv9' | 'sparc')
464            DIST_ARCH="sparc"
465            DIST_BITS=`isainfo -b`
466            ;;
467        '9000/800' | '9000/785')
468            DIST_ARCH="parisc"
469            if [ `getconf KERNEL_BITS` -eq 64 ]; then
470                DIST_BITS="64"
471            else
472                DIST_BITS="32"
473            fi
474            ;;
475        '2064' | '2066' | '2084' | '2086' | '2094' | '2096' | '2097' | '2098' | '2817')
476            DIST_ARCH="390"
477            DIST_BITS="64"
478            ;;
479        armv*)
480            if [ -z "`readelf -A /proc/self/exe | grep Tag_ABI_VFP_args`" ] ; then
481                DIST_ARCH="armel"
482                DIST_BITS="32"
483            else
484                DIST_ARCH="armhf"
485                DIST_BITS="32"
486            fi
487            ;;
488    esac
489fi
490
491# Through Java 6, OSX always places Java in the same location so we can reliably set JAVA_HOME
492# As of OSX 10.5 / Java 7, call /usr/libexec/java_home to find it
493# https://developer.apple.com/library/mac/qa/qa1170/_index.html
494if [ "$DIST_OS" = "macosx" ]
495then
496    if [ -z "$JAVA_HOME" ]; then
497        if [ -x "/usr/libexec/java_home" ]; then
498            JAVA_HOME=`/usr/libexec/java_home`
499        fi
500        if [ -z "$JAVA_HOME" ]; then
501            JAVA_HOME="/Library/Java/Home"
502        fi
503        export JAVA_HOME
504    fi
505fi
506
507# Test Echo
508ECHOTEST=`echo -n "x"`
509if [ "$ECHOTEST" = "x" ]
510then
511    ECHOOPT="-n "
512else
513    ECHOOPT=""
514fi
515
516
517gettext() {
518    # Call external gettext using our own translation files.
519    # Don't attempt to translate via the wrapper,
520    # it probably isn't supported in the community edition.
521    if [ "X${LANG#en}" = "X$LANG" ] && [ $(which $GETTEXT > /dev/null 2>&1) ] ; then
522            TEXTDOMAINDIR=$I2P/locale $GETTEXT -d i2prouter "$1"
523            if [ $? != 0 ] ; then
524                echo "$1"
525            fi
526        else
527            echo "$1"
528        fi
529}
530
531outputFile() {
532    if [ -f "$1" ]
533    then
534        echo "  $1  Found but not executable.";
535    else
536        echo "  $1"
537    fi
538}
539
540setup_openbsd() {
541    if ! pkg_info -qe 'java-tanukiwrapper->=3.5.19'; then
542        echo "Java wrapper not found. Either:"
543        echo "- install the package java-tanukiwrapper then start I2P again; or"
544        echo "- start I2P with $I2P/runplain.sh"
545        exit 1
546    else
547        # Wrapper package is installed so let's try copying the bits into the proper places
548        pkg_path=$(pkg_info -v java-tanukiwrapper   | awk '/^@cwd/{print $2}')
549        if [ -z $pkg_path ]; then
550            # Fallback, but we shouldn't end up here
551            echo "Unable to locate ${WRAPPER_CMD} in ${I2P}!"
552            echo
553            unsupported
554            exit 1
555        fi
556        set -e   # abort in case of error
557        # Package java-tanukiwrapper-3.5.19.1 misspells 'tanuki'
558        if [ -e "$pkg_path/lib/tanukuwrapper/libwrapper.so" ]; then
559            ln -sf "$pkg_path/lib/tanukuwrapper/libwrapper.so" "$I2P/lib/libwrapper.so"
560        else    # they fixed the path
561            ln -sf "$pkg_path/lib/tanukiwrapper/libwrapper.so" "$I2P/lib/libwrapper.so"
562        fi
563        ln -sf "$pkg_path/share/java/classes/tanukiwrapper.jar" "$I2P/lib/wrapper.jar"
564        ln -sf "$pkg_path/sbin/tanukiwrapper" "$WRAPPER_CMD"
565    fi
566    set +e
567}
568
569if [ "$DIST_OS" = "openbsd" -a ! -f "$WRAPPER_CMD" ]; then
570    # We handle it here because Tanuki doesn't ship OpenBSD binaries.
571    # We don't either, but they are (finally!) in OpenBSD itself.
572    setup_openbsd
573fi
574
575# Decide on the wrapper binary to use.
576# If the bits of the OS could be detected, we will try to look for the
577#  binary with the correct bits value.  If it doesn't exist, fall back
578#  and look for the 32-bit binary.  If that doesn't exist either then
579#  look for the default.
580WRAPPER_TEST_CMD=""
581if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS" ]
582then
583    WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
584    if [ ! -x "$WRAPPER_TEST_CMD" ]
585    then
586        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
587    fi
588    if [ -x "$WRAPPER_TEST_CMD" ]
589    then
590        WRAPPER_CMD="$WRAPPER_TEST_CMD"
591    else
592        outputFile "$WRAPPER_TEST_CMD"
593        WRAPPER_TEST_CMD=""
594    fi
595fi
596if [ -f "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" -a -z "$WRAPPER_TEST_CMD" ]
597then
598    WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
599    if [ ! -x "$WRAPPER_TEST_CMD" ]
600    then
601        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
602    fi
603    if [ -x "$WRAPPER_TEST_CMD" ]
604    then
605        WRAPPER_CMD="$WRAPPER_TEST_CMD"
606    else
607        outputFile "$WRAPPER_TEST_CMD"
608        WRAPPER_TEST_CMD=""
609    fi
610fi
611if [ -f "$WRAPPER_CMD" -a -z "$WRAPPER_TEST_CMD" ]
612then
613    WRAPPER_TEST_CMD="$WRAPPER_CMD"
614    if [ ! -x "$WRAPPER_TEST_CMD" ]
615    then
616        chmod +x "$WRAPPER_TEST_CMD" 2>/dev/null
617    fi
618    if [ -x "$WRAPPER_TEST_CMD" ]
619    then
620        WRAPPER_CMD="$WRAPPER_TEST_CMD"
621    else
622        outputFile "$WRAPPER_TEST_CMD"
623        WRAPPER_TEST_CMD=""
624    fi
625fi
626if [ -z "$WRAPPER_TEST_CMD" ]
627then
628    echo 'Unable to locate any of the following binaries:'
629    outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BITS"
630    if [ ! "$DIST_BITS" = "32" ]
631    then
632        outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
633    fi
634    outputFile "$WRAPPER_CMD"
635
636    exit 1
637fi
638
639if [ ! -r "${WRAPPER_CMD}" ]; then
640    echo "Unable to locate ${WRAPPER_CMD} in ${I2P}!"
641    echo
642    unsupported
643    echo
644    exit 1
645fi
646
647if $(which ldd > /dev/null 2>&1); then
648    # This should cover every *NIX other than OSX since OSX doesn't have ldd.
649    # OSX has otool. Is otool on every OSX installation? Is otool's output the same as ldd's?
650    # The wrapper we ship for OSX are for PPC and Intel, so maybe we don't need to worry about OSX?
651    if (ldd "$WRAPPER_CMD" |grep -q 'not found') > /dev/null 2>&1 || \
652                         ! (ldd "$WRAPPER_CMD" > /dev/null 2>&1); then
653        failed
654    fi
655fi
656
657
658# Build the nice clause
659if [ "X$PRIORITY" = "X" ]
660then
661    CMDNICE=""
662else
663    CMDNICE="nice -$PRIORITY"
664fi
665
666# Build the anchor file clause.
667if [ "X$IGNORE_SIGNALS" = "X" ]
668then
669   ANCHORPROP=
670   IGNOREPROP=
671else
672   ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
673   IGNOREPROP=wrapper.ignore_signals=TRUE
674fi
675
676# Build the status file clause.
677if [ "X$DETAIL_STATUS$WAIT_FOR_STARTED_STATUS" = "X" ]
678then
679   STATUSPROP=
680else
681   STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
682fi
683
684# Build the command file clause.
685COMMANDPROP=
686
687# Build the log file clause.
688LOGPROP="wrapper.logfile=\"$LOGFILE\""
689
690if [ ! -n "$WAIT_FOR_STARTED_STATUS" ]
691then
692    WAIT_FOR_STARTED_STATUS=true
693fi
694
695if [ $WAIT_FOR_STARTED_STATUS = true ] ; then
696    DETAIL_STATUS=true
697fi
698
699
700# Build the lock file clause.  Only create a lock file if the lock directory exists on this platform.
701LOCKPROP=
702if [ -d $LOCKDIR ]
703then
704    if [ -w $LOCKDIR ]
705    then
706        LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
707    fi
708fi
709
710prepAdditionalParams() {
711    ADDITIONAL_PARA=""
712    if [ -n "$PASS_THROUGH" ] ; then
713        ADDITIONAL_PARA="--"
714    fi
715    while [ -n "$1" ] ; do
716        ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
717        shift
718    done
719}
720
721checkUser() {
722    # $1 touchLock flag
723    # $2.. [command] args
724
725    # Check the configured user.  If necessary rerun this script as the desired user.
726    if [ "X$RUN_AS_USER" != "X" ]
727    then
728
729        # Resolve the location of the 'id' command
730        IDEXE="/usr/xpg4/bin/id"
731        if [ ! -x "$IDEXE" ]
732        then
733            IDEXE="/usr/bin/id"
734            if [ ! -x "$IDEXE" ]
735            then
736                echo 'Unable to locate "id".'
737                echo 'Please report this message along with the location of the command on your system.'
738                exit 1
739            fi
740        fi
741        if [ "`$IDEXE -u -n "$RUN_AS_USER" 2>/dev/null`" != "$RUN_AS_USER" ]
742        then
743            echo "User $RUN_AS_USER does not exist."
744            exit 1
745        fi
746
747        if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
748        then
749            # Already running as the configured user.  Avoid password prompts by not calling su.
750            RUN_AS_USER=""
751        fi
752    fi
753    if [ "X$RUN_AS_USER" != "X" ]
754    then
755        # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
756        # able to create the lock file.  The Wrapper will be able to update this file once it
757        # is created but will not be able to delete it on shutdown.  If $1 is set then
758        # the lock file should be created for the current command
759        if [ "X$LOCKPROP" != "X" ]
760        then
761            if [ "X$1" != "X" ]
762            then
763                # Resolve the primary group
764                RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
765                if [ "X$RUN_AS_GROUP" = "X" ]
766                then
767                    RUN_AS_GROUP=$RUN_AS_USER
768                fi
769                touch $LOCKFILE
770                chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
771            fi
772        fi
773
774        # Still want to change users, recurse.  This means that the user will only be
775        #  prompted for a password once. Variables shifted by 1
776        shift
777
778        # Wrap the parameters so they can be passed.
779        ADDITIONAL_PARA=""
780        while [ -n "$1" ] ; do
781            ADDITIONAL_PARA="$ADDITIONAL_PARA \"$1\""
782            shift
783        done
784
785        # Use "runuser" if this exists.  runuser should be used on RedHat in preference to su.
786        #
787        if test -f "/sbin/runuser"
788        then
789            /sbin/runuser -s /bin/sh - $RUN_AS_USER -c "\"$REALPATH\" $ADDITIONAL_PARA"
790        else
791            su - $RUN_AS_USER -s /bin/sh -c "\"$REALPATH\" $ADDITIONAL_PARA"
792        fi
793        RUN_AS_USER_EXITCODE=$?
794        # Now that we are the original user again, we may need to clean up the lock file.
795        if [ "X$LOCKPROP" != "X" ]
796        then
797            getpid
798            if [ "X$pid" = "X" ]
799            then
800                # Wrapper is not running so make sure the lock file is deleted.
801                if [ -f "$LOCKFILE" ]
802                then
803                    rm "$LOCKFILE"
804                fi
805            fi
806        fi
807
808        exit $RUN_AS_USER_EXITCODE
809    fi
810}
811
812getpid() {
813    pid=""
814    if [ -f "$PIDFILE" ]
815    then
816        if [ -r "$PIDFILE" ]
817        then
818            pid=`cat "$PIDFILE"`
819            if [ "X$pid" != "X" ]
820            then
821                # It is possible that 'a' process with the pid exists but that it is not the
822                #  correct process.  This can happen in a number of cases, but the most
823                #  common is during system startup after an unclean shutdown.
824                # The ps statement below looks for the specific wrapper command running as
825                #  the pid.  If it is not found then the pid file is considered to be stale.
826                case "$DIST_OS" in
827                    'freebsd')
828                        pidtest=`$PSEXE -p $pid -o args | tail -1`
829                        if [ "X$pidtest" = "XCOMMAND" ]
830                        then
831                            pidtest=""
832                        fi
833                        ;;
834                    'macosx')
835                        pidtest=`$PSEXE -ww -p $pid -o command | grep -F "$WRAPPER_CMD" | tail -1`
836                        ;;
837                    'solaris')
838                        if [ -f "/usr/bin/pargs" ]
839                        then
840                            pidtest=`pargs $pid | fgrep "$WRAPPER_CMD" | tail -1`
841                        else
842                            case "$PSEXE" in
843                            '/usr/ucb/ps')
844                                pidtest=`$PSEXE -auxww  $pid | fgrep "$WRAPPER_CMD" | tail -1`
845                                ;;
846                            '/usr/bin/ps')
847                                TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
848                                COUNT=`echo $TRUNCATED_CMD | wc -m`
849                                COUNT=`echo ${COUNT}`
850                                COUNT=`expr $COUNT - 1`
851                                TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
852                                pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
853                                ;;
854                            '/bin/ps')
855                                TRUNCATED_CMD=`$PSEXE -o comm -p $pid | tail -1`
856                                COUNT=`echo $TRUNCATED_CMD | wc -m`
857                                COUNT=`echo ${COUNT}`
858                                COUNT=`expr $COUNT - 1`
859                                TRUNCATED_CMD=`echo $WRAPPER_CMD | cut -c1-$COUNT`
860                                pidtest=`$PSEXE -o comm -p $pid | fgrep "$TRUNCATED_CMD" | tail -1`
861                                ;;
862                            *)
863                                echo "Unsupported ps command $PSEXE"
864                                exit 1
865                                ;;
866                            esac
867                        fi
868                        ;;
869                    'hpux')
870                        pidtest=`$PSEXE -p $pid -x -o args | grep -F "$WRAPPER_CMD" | tail -1`
871                        ;;
872                    *)
873                        pidtest=`$PSEXE -p $pid -o args | grep -F "$WRAPPER_CMD" | tail -1`
874                        ;;
875                esac
876
877                if [ "X$pidtest" = "X" ]
878                then
879                    # This is a stale pid file.
880                    rm -f "$PIDFILE"
881                    echo "Removed stale pid file: $PIDFILE"
882                    pid=""
883                fi
884            fi
885        else
886            echo "Cannot read $PIDFILE."
887            exit 1
888        fi
889    fi
890}
891
892getstatus() {
893    STATUS=
894    if [ -f "$STATUSFILE" ]
895    then
896        if [ -r "$STATUSFILE" ]
897        then
898            STATUS=`cat "$STATUSFILE"`
899        fi
900    fi
901    if [ "X$STATUS" = "X" ]
902    then
903        STATUS="Unknown"
904    fi
905
906    JAVASTATUS=
907    if [ -f "$JAVASTATUSFILE" ]
908    then
909        if [ -r "$JAVASTATUSFILE" ]
910        then
911            JAVASTATUS=`cat "$JAVASTATUSFILE"`
912        fi
913    fi
914    if [ "X$JAVASTATUS" = "X" ]
915    then
916        JAVASTATUS="Unknown"
917    fi
918}
919
920testpid() {
921    case "$DIST_OS" in
922     'solaris')
923        case "$PSEXE" in
924        '/usr/ucb/ps')
925            pid=`$PSEXE  $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
926            ;;
927        '/usr/bin/ps')
928            pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
929            ;;
930        '/bin/ps')
931            pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
932            ;;
933        *)
934            echo "Unsupported ps command $PSEXE"
935            exit 1
936            ;;
937        esac
938        ;;
939    *)
940        pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` 2>/dev/null
941        ;;
942    esac
943    if [ "X$pid" = "X" ]
944    then
945        # Process is gone so remove the pid file.
946        rm -f "$PIDFILE"
947        pid=""
948    fi
949}
950
951launchdtrap() {
952    stopit
953    exit
954}
955
956waitforwrapperstop() {
957    getpid
958    while [ "X$pid" != "X" ] ; do
959        sleep 1
960        getpid
961    done
962}
963
964create_config_dir() {
965    if [ ! -d "$I2P_CONFIG_DIR" ]; then
966        UMASK=$(awk -F'=' '/^ *wrapper\.umask/{print $2}' $WRAPPER_CONF)
967        umask $UMASK
968        if ! mkdir -p "$I2P_CONFIG_DIR"; then
969            echo "Error creating $I2P_CONFIG_DIR! Edit $0 and set I2P_CONFIG_DIR" >&2
970            echo "to the correct location." >&2
971            exit 1
972        fi
973    fi
974}
975
976launchdinternal() {
977    getpid
978    trap launchdtrap TERM
979    if [ "X$pid" = "X" ]
980    then
981        prepAdditionalParams "$@"
982        create_config_dir
983
984        # The string passed to eval must handles spaces in paths correctly.
985        COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.java.command=\"$JAVABINARY\" wrapper.displayname=\"$APP_LONG_NAME\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP $LOGPROP $ADDITIONAL_PARA"
986        eval $COMMAND_LINE || failed
987    else
988        eval echo `gettext '$APP_LONG_NAME is already running.'`
989        exit 1
990    fi
991    # launchd expects that this script stay up and running so we need to do our own monitoring of the Wrapper process.
992    if [ $WAIT_FOR_STARTED_STATUS = true ]
993    then
994        waitforwrapperstop
995    fi
996}
997
998console() {
999    eval echo "`gettext 'Running $APP_LONG_NAME'`..."
1000    getpid
1001    if [ "X$pid" = "X" ]
1002    then
1003        trap '' 3 2
1004
1005        prepAdditionalParams "$@"
1006        create_config_dir
1007
1008        # The string passed to eval must handles spaces in paths correctly.
1009        COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.java.command=\"$JAVABINARY\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" $ANCHORPROP $STATUSPROP $COMMANDPROP $LOCKPROP $LOGPROP $ADDITIONAL_PARA"
1010        eval $COMMAND_LINE
1011        local RETVAL=$?
1012        if [ $RETVAL -ne '0' ] && [ $RETVAL -ne '2' ]; then
1013            failed
1014        fi
1015    else
1016        eval echo `gettext '$APP_LONG_NAME is already running.'`
1017        exit 1
1018    fi
1019}
1020
1021waitforjavastartup() {
1022    getstatus
1023    eval echo $ECHOOPT "`gettext 'Waiting for $APP_LONG_NAME'`..."
1024
1025    # Wait until the timeout or we have something besides Unknown.
1026    counter=15
1027    while [ "$JAVASTATUS" = "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
1028        echo $ECHOOPT"."
1029        sleep 1
1030        getstatus
1031        counter=`expr $counter - 1`
1032    done
1033
1034    if [ -n "$WAIT_FOR_STARTED_TIMEOUT" ] ; then
1035        counter=$WAIT_FOR_STARTED_TIMEOUT
1036    else
1037        counter=120
1038    fi
1039    while [ "$JAVASTATUS" != "STARTED" -a "$JAVASTATUS" != "Unknown" -a $counter -gt 0 -a -n "$JAVASTATUS" ] ; do
1040        echo $ECHOOPT"."
1041        sleep 1
1042        getstatus
1043        counter=`expr $counter - 1`
1044    done
1045    if [ "X$ECHOOPT" != "X" ] ; then
1046        echo ""
1047    fi
1048}
1049
1050startwait() {
1051    if [ $WAIT_FOR_STARTED_STATUS = true ]
1052    then
1053        waitforjavastartup
1054    fi
1055    # Sleep for a few seconds to allow for intialization if required
1056    #  then test to make sure we're still running.
1057    #
1058    i=0
1059    while [ $i -lt $WAIT_AFTER_STARTUP ]
1060    do
1061        sleep 1
1062        echo $ECHOOPT"."
1063        i=`expr $i + 1`
1064    done
1065    if [ $WAIT_AFTER_STARTUP -gt 0 -o $WAIT_FOR_STARTED_STATUS = true ]
1066    then
1067        getpid
1068        if [ "X$pid" = "X" ]
1069        then
1070            eval echo " `gettext 'WARNING: $APP_LONG_NAME may have failed to start.'`"
1071            failed
1072        else
1073            eval echo ' running: PID:$pid'
1074        fi
1075    else
1076        echo ""
1077    fi
1078}
1079
1080macosxstart() {
1081    # The daemon has been installed.
1082    echo "Starting $APP_LONG_NAME.  Detected Mac OSX and installed launchd daemon."
1083    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1084        eval echo `gettext 'Must be root to perform this action.'`
1085        exit 1
1086    fi
1087
1088    getpid
1089    if [ "X$pid" != "X" ] ; then
1090        eval echo `gettext '$APP_LONG_NAME is already running.'`
1091        exit 1
1092    fi
1093
1094    # If the daemon was just installed, it may not be loaded.
1095    LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
1096    if [ "X${LOADED_PLIST}" = "X" ] ; then
1097        launchctl load /Library/LaunchDaemons/${APP_PLIST}
1098    fi
1099    # If launchd is set to run the daemon already at Load, we don't need to call start
1100    getpid
1101    if [ "X$pid" = "X" ] ; then
1102        launchctl start ${APP_PLIST_BASE}
1103    fi
1104
1105    startwait
1106}
1107
1108upstartstart() {
1109    # The daemon has been installed.
1110    echo "Starting $APP_LONG_NAME.  Detected Linux and installed upstart."
1111    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1112        eval echo `gettext 'Must be root to perform this action.'`
1113        exit 1
1114    fi
1115
1116    getpid
1117    if [ "X$pid" != "X" ] ; then
1118        eval echo `gettext '$APP_LONG_NAME is already running.'`
1119        exit 1
1120    fi
1121
1122    /sbin/start ${APP_NAME}
1123
1124    startwait
1125}
1126
1127start() {
1128    eval echo "`gettext 'Starting $APP_LONG_NAME'`..."
1129    getpid
1130    if [ "X$pid" = "X" ]
1131    then
1132        prepAdditionalParams "$@"
1133        create_config_dir
1134
1135        # The string passed to eval must handles spaces in paths correctly.
1136        COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.java.command=\"$JAVABINARY\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $STATUSPROP $COMMANDPROP $LOCKPROP $LOGPROP $ADDITIONAL_PARA"
1137        eval $COMMAND_LINE || failed
1138    else
1139        eval echo `gettext '$APP_LONG_NAME is already running.'`
1140        exit 1
1141    fi
1142
1143    startwait
1144}
1145
1146
1147stopit() {
1148    # $1 exit if down flag
1149
1150    eval echo "`gettext 'Stopping $APP_LONG_NAME'`..."
1151    getpid
1152    if [ "X$pid" = "X" ]
1153    then
1154        eval echo `gettext '$APP_LONG_NAME was not running.'`
1155        if [ "X$1" = "X1" ]
1156        then
1157            exit 1
1158        fi
1159    else
1160        if [ "X$IGNORE_SIGNALS" = "X" ]
1161        then
1162            # Running so try to stop it.
1163            kill -TERM $pid
1164            if [ $? -ne 0 ]
1165            then
1166                # An explanation for the failure should have been given
1167                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1168                exit 1
1169            fi
1170        else
1171            rm -f "$ANCHORFILE"
1172            if [ -f "$ANCHORFILE" ]
1173            then
1174                # An explanation for the failure should have been given
1175                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1176                exit 1
1177            fi
1178        fi
1179
1180        # We can not predict how long it will take for the wrapper to
1181        #  actually stop as it depends on settings in wrapper.conf.
1182        #  Loop until it does.
1183        savepid=$pid
1184        CNT=0
1185        TOTCNT=0
1186        while [ "X$pid" != "X" ]
1187        do
1188            # Show a waiting message every 5 seconds.
1189            if [ "$CNT" -lt "5" ]
1190            then
1191                CNT=`expr $CNT + 1`
1192            else
1193                eval echo "`gettext 'Waiting for $APP_LONG_NAME to exit'`..."
1194                CNT=0
1195            fi
1196            TOTCNT=`expr $TOTCNT + 1`
1197
1198            sleep 1
1199
1200            testpid
1201        done
1202
1203        pid=$savepid
1204        testpid
1205        if [ "X$pid" != "X" ]
1206        then
1207            eval echo `gettext 'Failed to stop $APP_LONG_NAME.'`
1208            exit 1
1209        else
1210            eval echo `gettext 'Stopped $APP_LONG_NAME.'`
1211        fi
1212    fi
1213}
1214
1215graceful() {
1216    # $1 exit if down flag
1217
1218    eval echo "`gettext 'Stopping $APP_LONG_NAME gracefully'`..."
1219    getpid
1220    if [ "X$pid" = "X" ]
1221    then
1222        eval echo `gettext '$APP_LONG_NAME was not running.'`
1223        if [ "X$1" = "X1" ]
1224        then
1225            exit 1
1226        fi
1227    else
1228        if [ "X$IGNORE_SIGNALS" = "X" ]
1229        then
1230            # Running so try to stop it.
1231            # This sends HUP. router.gracefulHUP must be set in router.config,
1232            # or else this will do the same as stop.
1233            kill -HUP $pid
1234            if [ $? -ne 0 ]
1235            then
1236                # An explanation for the failure should have been given
1237                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1238                exit 1
1239            fi
1240        else
1241            rm -f "$ANCHORFILE"
1242            if [ -f "$ANCHORFILE" ]
1243            then
1244                # An explanation for the failure should have been given
1245                eval echo `gettext 'Unable to stop $APP_LONG_NAME.'`
1246                exit 1
1247            fi
1248        fi
1249    fi
1250}
1251
1252pause() {
1253    echo "Pausing $APP_LONG_NAME."
1254}
1255
1256resume() {
1257    echo "Resuming $APP_LONG_NAME."
1258}
1259
1260status() {
1261    getpid
1262    if [ "X$pid" = "X" ]
1263    then
1264        eval echo `gettext '$APP_LONG_NAME is not running.'`
1265        exit 1
1266    else
1267        if [ "X$DETAIL_STATUS" = "X" ]
1268        then
1269            eval echo `gettext '$APP_LONG_NAME is running: PID:$pid'`
1270        else
1271            getstatus
1272            eval echo `gettext '$APP_LONG_NAME is running: PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS'`
1273        fi
1274        exit 0
1275    fi
1276}
1277
1278installUpstart() {
1279    echo " Installing the $APP_LONG_NAME daemon using upstart.."
1280    if [ -f "${APP_NAME}.conf" ] ; then
1281        echo " a custom upstart conf file ${APP_NAME}.conf found"
1282        cp "${REALDIR}/${APP_NAME}.install" "/etc/init/${APP_NAME}.conf"
1283    else
1284        echo ' creating default upstart conf file..'
1285        echo "# ${APP_NAME} - ${APP_LONG_NAME}" > "/etc/init/${APP_NAME}.conf"
1286        echo "description \"${APP_LONG_NAME}\"" >> "/etc/init/${APP_NAME}.conf"
1287        echo "author \"Tanuki Software Ltd. <info@tanukisoftware.com>\"" >> "/etc/init/${APP_NAME}.conf"
1288        echo "start on runlevel [2345]" >> "/etc/init/${APP_NAME}.conf"
1289        echo "stop on runlevel [!2345]" >> "/etc/init/${APP_NAME}.conf"
1290        echo "env LANG=${LANG}" >> "/etc/init/${APP_NAME}.conf"
1291       echo "exec \"${REALPATH}\" launchdinternal" >> "/etc/init/${APP_NAME}.conf"
1292    fi
1293}
1294
1295installsystemd() {
1296    if [ -d "/etc/systemd/system/" ]; then
1297        cat << EOF >> "$SYSTEMD_SERVICE"
1298[Unit]
1299Description=$APP_LONG_NAME
1300After= local-fs.target network.target time-sync.target
1301
1302[Service]
1303Type=forking
1304ExecStart=$I2P/i2prouter start
1305ExecReload=$I2P/i2prouter restart
1306ExecStop=$I2P/i2prouter stop
1307PIDFile=$I2P_CONFIG_DIR/i2p.pid
1308
1309[Install]
1310WantedBy=multi-user.target
1311EOF
1312    systemctl --system daemon-reload > /dev/null 2>&1
1313    fi
1314}
1315
1316installdaemon() {
1317    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1318        eval echo `gettext 'Must be root to perform this action.'`
1319        exit 1
1320    else
1321        APP_NAME_LOWER=`echo "$APP_NAME" | $TREXE "[A-Z]" "[a-z]"`
1322        if [ "$DIST_OS" = "solaris" ] ; then
1323            echo 'Detected Solaris:'
1324            if [ -f /etc/init.d/$APP_NAME ] ; then
1325                eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1326                exit 1
1327            else
1328                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1329                ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1330                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K20$APP_NAME_LOWER"
1331                ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S20$APP_NAME_LOWER"
1332            fi
1333        elif [ "$DIST_OS" = "linux" ] ; then
1334            if [ -f /etc/redhat-release -o -f /etc/redhat_version -o -f /etc/fedora-release ]  ; then
1335                echo 'Detected RHEL or Fedora:'
1336                if [ -f "/etc/init.d/$APP_NAME" -o -f "/etc/init/${APP_NAME}.conf" ] ; then
1337                    eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1338                    exit 1
1339                else
1340                    if [ -n "$USE_UPSTART" -a -d "/etc/init" ] ; then
1341                        installUpstart
1342                    else
1343                        eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1344                        ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1345                        /sbin/chkconfig --add "$APP_NAME"
1346                        /sbin/chkconfig "$APP_NAME" on
1347                    fi
1348                fi
1349            elif [ -f /etc/slackware-version ]; then
1350                echo 'Detected Slackware Linux:'
1351                if [ -e "/etc/rc.d/rc.i2p" -o -f "/etc/rc.d/rc.i2p.new" ]; then
1352                    echo "Found initscript from I2P Slackpkg. Aborting." >&2
1353                    exit 1
1354                else
1355                    if grep -q ${APP_NAME}router /etc/rc.d/rc.local > /dev/null 2>&1; then
1356                        eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1357                        exit 1
1358                    else
1359                        echo "${REALDIR}/${APP_NAME}router start"  >> /etc/rc.d/rc.local
1360                        if [ ! -e /etc/rc.d/rc.local_shutdown ]; then
1361                            echo "#!/bin/sh" >> /etc/rc.d/rc.local_shutdown
1362                        fi
1363                        echo "${REALDIR}/${APP_NAME}router stop"  >> /etc/rc.d/rc.local_shutdown
1364                        chmod 755 /etc/rc.d/rc.local_shutdown
1365                    fi
1366                fi
1367            elif [ -f /etc/arch-release ]; then
1368            echo 'Detected Arch Linux:'
1369                if [ -f "/etc/rc.d/i2prouter" -o -f "/usr/lib/systemd/system/i2prouter.service" ]; then
1370                    echo 'AUR package found. Refusing to continue.'
1371                    exit 1
1372                elif [ -f /etc/rc.d/i2p -a ! "$USE_SYSTEMD" = "1" ] || [ -f "$SYSTEMD_SERVICE" -a "$USE_SYSTEMD" = "1" ]; then
1373                    eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1374                    exit 1
1375                else
1376                        if [ ! -f "/etc/init.d/i2p" ]; then
1377                            if [ "$USE_SYSTEMD" != "1" ]; then
1378                                echo "#!/bin/bash"  > /etc/rc.d/${APP_NAME}
1379                                echo   >> /etc/rc.d/${APP_NAME}
1380                                echo ". /etc/rc.conf"  >> /etc/rc.d/${APP_NAME}
1381                                echo ". /etc/rc.d/functions"  >> /etc/rc.d/${APP_NAME}
1382                                echo   >> /etc/rc.d/${APP_NAME}
1383                                echo "case "\$1" in"  >> /etc/rc.d/${APP_NAME}
1384                                echo "  start)"  >> /etc/rc.d/${APP_NAME}
1385                                echo "    stat_busy \"Starting i2p-Router\""  >> /etc/rc.d/${APP_NAME}
1386                                echo "    ${REALDIR}/${APP_NAME}router start >/dev/null 2>&1"  >> /etc/rc.d/${APP_NAME}
1387                                echo "    if [ \$? -gt 0 ]; then"  >> /etc/rc.d/${APP_NAME}
1388                                echo "      stat_fail"  >> /etc/rc.d/${APP_NAME}
1389                                echo "    else"  >> /etc/rc.d/${APP_NAME}
1390                                echo "      stat_done"  >> /etc/rc.d/${APP_NAME}
1391                                echo "      add_daemon i2prouter"  >> /etc/rc.d/${APP_NAME}
1392                                echo "    fi"  >> /etc/rc.d/${APP_NAME}
1393                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1394                                echo "  stop)"  >> /etc/rc.d/${APP_NAME}
1395                                echo "    stat_busy "Stopping i2p-Router""  >> /etc/rc.d/${APP_NAME}
1396                                echo "    ${REALDIR}/${APP_NAME}router stop > /dev/null 2>&1"  >> /etc/rc.d/${APP_NAME}
1397                                echo "    if [ \$? -gt 0 ]; then"  >> /etc/rc.d/${APP_NAME}
1398                                echo "      stat_fail"  >> /etc/rc.d/${APP_NAME}
1399                                echo "    else"  >> /etc/rc.d/${APP_NAME}
1400                                echo "      stat_done"  >> /etc/rc.d/${APP_NAME}
1401                                echo "      rm_daemon i2prouter"  >> /etc/rc.d/${APP_NAME}
1402                                echo "    fi"  >> /etc/rc.d/${APP_NAME}
1403                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1404                                echo "  restart)"  >> /etc/rc.d/${APP_NAME}
1405                                echo "    ${REALDIR}/${APP_NAME}router restart"  >> /etc/rc.d/${APP_NAME}
1406                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1407                                echo "  console)"  >> /etc/rc.d/${APP_NAME}
1408                                echo "    ${REALDIR}/${APP_NAME}router console"  >> /etc/rc.d/${APP_NAME}
1409                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1410                                echo "  status)"  >> /etc/rc.d/${APP_NAME}
1411                                echo "    ${REALDIR}/${APP_NAME}router status"  >> /etc/rc.d/${APP_NAME}
1412                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1413                                echo "  dump)"  >> /etc/rc.d/${APP_NAME}
1414                                echo "    ${REALDIR}/${APP_NAME}router dump"  >> /etc/rc.d/${APP_NAME}
1415                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1416                                echo "  graceful)"  >> /etc/rc.d/${APP_NAME}
1417                                echo "    ${REALDIR}/${APP_NAME}router graceful"  >> /etc/rc.d/${APP_NAME}
1418                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1419                                echo "  *)"  >> /etc/rc.d/${APP_NAME}
1420                                echo "    echo \"usage: \$0 {start|stop|restart|console|status|dump}\""  >> /etc/rc.d/${APP_NAME}
1421                                echo "    ;;"  >> /etc/rc.d/${APP_NAME}
1422                                echo "esac"  >> /etc/rc.d/${APP_NAME}
1423                                chmod 755 /etc/rc.d/${APP_NAME}
1424                                chown root:root /etc/rc.d/${APP_NAME}
1425                                echo " The $APP_LONG_NAME daemon has been installed."
1426                                echo ' Add \"i2p\" to the DAEMONS variable in /etc/rc.conf to enable.'
1427                            else
1428                                # We'll end up here if systemd is enabled.
1429                                # If systemd is enabled we don't need the initscript
1430                                rm -f /etc/rc.d/${APP_NAME}
1431                            fi
1432                        fi
1433                        if [ ! -f "${SYSTEMD_SERVICE}" ]; then
1434                            installsystemd
1435                        fi
1436                fi
1437            elif [ -f /etc/SuSE-release ] ; then
1438                echo 'Detected SuSE or SLES:'
1439                 if [ -f /etc/rc.d/${APP_NAME} -a ! "$USE_SYSTEMD" = "1" ] || [ -f "$SYSTEMD_SERVICE" -a "$USE_SYSTEMD" = "1" ]; then
1440                    eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1441                    exit 1
1442                else
1443                    if [ ! -f "/etc/init.d/$APP_NAME" ]; then
1444                        if [ "$USE_SYSTEMD" != "1" ]; then
1445                            eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1446                            ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1447                            sed -i "s/Default-Start: 2 3 4 5/Default-Start: 5/" $0
1448                            insserv "/etc/init.d/$APP_NAME"
1449                        else
1450                            rm -f "/etc/init.d/$APP_NAME"
1451                        fi
1452                    fi
1453                    if [ ! -f "${SYSTEMD_SERVICE}" ]; then
1454                        installsystemd
1455                    fi
1456                fi
1457            elif [ -f /etc/lsb-release -o -f /etc/debian_version ] ; then
1458                echo 'Detected Debian-based distribution:'
1459                if [ -f "/etc/init.d/$APP_NAME" -o -f "/etc/init/${APP_NAME}.conf" ] ; then
1460                    eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1461                    exit 1
1462                else
1463                    if [ -n "$USE_UPSTART" -a -d "/etc/init" ] ; then
1464                        installUpstart
1465                    else
1466                        echo " Installing the $APP_LONG_NAME daemon using init.d.."
1467                        ln -s "$REALPATH" "/etc/init.d/$APP_NAME"
1468                        update-rc.d "$APP_NAME" defaults
1469                    fi
1470                fi
1471            else
1472                echo 'Detected Linux:'
1473                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1474                    eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1475                    exit 1
1476                else
1477                    eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1478                    ln -s "$REALPATH" /etc/init.d/$APP_NAME
1479                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/K20$APP_NAME_LOWER"
1480                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc3.d/S20$APP_NAME_LOWER"
1481                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/S20$APP_NAME_LOWER"
1482                    ln -s "/etc/init.d/$APP_NAME" "/etc/rc5.d/K20$APP_NAME_LOWER"
1483                fi
1484            fi
1485        elif [ "$DIST_OS" = "hpux" ] ; then
1486            echo 'Detected HP-UX:'
1487            if [ -f "/sbin/init.d/$APP_NAME" ] ; then
1488                eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1489                exit 1
1490            else
1491                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1492                ln -s "$REALPATH" "/sbin/init.d/$APP_NAME"
1493                ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/K20$APP_NAME_LOWER"
1494                ln -s "/sbin/init.d/$APP_NAME" "/sbin/rc3.d/S20$APP_NAME_LOWER"
1495            fi
1496        elif [ "$DIST_OS" = "aix" ] ; then
1497            echo 'Detected AIX:'
1498            if [ -f "/etc/rc.d/init.d/$APP_NAME" ] ; then
1499                echo " The $APP_LONG_NAME daemon is already installed as rc.d script."
1500                exit 1
1501            elif [ -n "`/usr/sbin/lsitab $APP_NAME`" -a -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1502                echo " The $APP_LONG_NAME daemon is already installed as SRC service."
1503                exit 1
1504            else
1505                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1506                if [ -n "`/usr/sbin/lsitab install_assist`" ] ; then
1507                    echo ' The task /usr/sbin/install_assist was found in the inittab, this might cause problems for all subsequent tasks to launch at this process is known to block the init task. Please make sure this task is not needed anymore and remove/deactivate it.'
1508                fi
1509                /usr/bin/mkssys -s "$APP_NAME" -p "$REALPATH" -a "launchdinternal" -u 0 -f 9 -n 15 -S
1510                /usr/sbin/mkitab "$APP_NAME":2:once:"/usr/bin/startsrc -s \"${APP_NAME}\" >/dev/console 2>&1"
1511
1512            fi
1513        elif [ "$DIST_OS" = "freebsd" ] ; then
1514            echo 'Detected FreeBSD:'
1515            if [ -f "/etc/rc.d/$APP_NAME" ] ; then
1516                eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1517                exit 1
1518            else
1519                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1520                sed -i .bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
1521                if [ -f "${REALDIR}/${APP_NAME}.install" ] ; then
1522                    ln -s "${REALDIR}/${APP_NAME}.install" "/etc/rc.d/$APP_NAME"
1523                else
1524                    echo '#!/bin/sh' > "/etc/rc.d/$APP_NAME"
1525                    echo "#" >> "/etc/rc.d/$APP_NAME"
1526                    echo "# PROVIDE: $APP_NAME" >> "/etc/rc.d/$APP_NAME"
1527                    echo "# REQUIRE: NETWORKING" >> "/etc/rc.d/$APP_NAME"
1528                    echo "# KEYWORD: shutdown" >> "/etc/rc.d/$APP_NAME"
1529                    echo ". /etc/rc.subr" >> "/etc/rc.d/$APP_NAME"
1530                    echo "name=\"$APP_NAME\"" >> "/etc/rc.d/$APP_NAME"
1531                    echo "rcvar=\`set_rcvar\`" >> "/etc/rc.d/$APP_NAME"
1532                    echo "command=\"${REALDIR}/${APP_NAME}router\"" >> "/etc/rc.d/$APP_NAME"
1533                    echo 'start_cmd="${name}_start"' >> "/etc/rc.d/$APP_NAME"
1534                    echo 'load_rc_config $name' >> "/etc/rc.d/$APP_NAME"
1535                    echo 'status_cmd="${name}_status"' >> "/etc/rc.d/$APP_NAME"
1536                    echo 'stop_cmd="${name}_stop"' >> "/etc/rc.d/$APP_NAME"
1537                    echo "${APP_NAME}_status() {" >> "/etc/rc.d/$APP_NAME"
1538                    echo '${command} status' >> "/etc/rc.d/$APP_NAME"
1539                    echo '}' >> "/etc/rc.d/$APP_NAME"
1540                    echo "${APP_NAME}_stop() {" >> "/etc/rc.d/$APP_NAME"
1541                    echo '${command} stop' >> "/etc/rc.d/$APP_NAME"
1542                    echo '}' >> "/etc/rc.d/$APP_NAME"
1543                    echo "${APP_NAME}_start() {" >> "/etc/rc.d/$APP_NAME"
1544                    echo '${command} start' >> "/etc/rc.d/$APP_NAME"
1545                    echo '}' >> "/etc/rc.d/$APP_NAME"
1546                    echo 'run_rc_command "$1"' >> "/etc/rc.d/$APP_NAME"
1547                fi
1548                echo "${APP_NAME}_enable=\"YES\"" >> /etc/rc.conf
1549                chmod 555 "/etc/rc.d/$APP_NAME"
1550            fi
1551        elif [ "$DIST_OS" = "macosx" ] ; then
1552            echo 'Detected Mac OSX:'
1553            if [ -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1554                eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1555                exit 1
1556            else
1557                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1558                if [ -f "${REALDIR}/${APP_PLIST}" ] ; then
1559                    ln -s "${REALDIR}/${APP_PLIST}" "/Library/LaunchDaemons/${APP_PLIST}"
1560                else
1561                    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > "/Library/LaunchDaemons/${APP_PLIST}"
1562                    echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"" >> "/Library/LaunchDaemons/${APP_PLIST}"
1563                    echo "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" >> "/Library/LaunchDaemons/${APP_PLIST}"
1564                    echo "<plist version=\"1.0\">" >> "/Library/LaunchDaemons/${APP_PLIST}"
1565                    echo "    <dict>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1566                    echo "        <key>Label</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1567                    echo "        <string>${APP_PLIST_BASE}</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1568                    echo "        <key>ProgramArguments</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1569                    echo "        <array>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1570                    echo "            <string>${REALDIR}/${APP_NAME}router</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1571                    echo "            <string>launchdinternal</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1572                    echo "        </array>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1573                    echo "        <key>OnDemand</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1574                    echo "        <true/>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1575                    echo "        <key>RunAtLoad</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1576                    echo "        <true/>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1577                    if [ "X$RUN_AS_USER" != "X" ] ; then
1578                        echo "        <key>UserName</key>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1579                        echo "        <string>${RUN_AS_USER}</string>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1580                    fi
1581                    echo "    </dict>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1582                    echo "</plist>" >> "/Library/LaunchDaemons/${APP_PLIST}"
1583                fi
1584                chmod 555 "/Library/LaunchDaemons/${APP_PLIST}"
1585            fi
1586        elif [ "$DIST_OS" = "zos" ] ; then
1587            echo 'Detected z/OS:'
1588            if [ -f /etc/rc.bak ] ; then
1589                eval echo " `gettext 'The $APP_LONG_NAME daemon is already installed.'`"
1590                exit 1
1591            else
1592                eval echo " `gettext 'Installing the $APP_LONG_NAME daemon'`.."
1593                cp /etc/rc /etc/rc.bak
1594                sed  "s:echo /etc/rc script executed, \`date\`::g" /etc/rc.bak > /etc/rc
1595                echo "_BPX_JOBNAME='${APP_NAME}' \"${REALDIR}/${APP_NAME}router\" start" >>/etc/rc
1596                echo '/etc/rc script executed, `date`' >>/etc/rc
1597            fi
1598        else
1599            eval echo `gettext 'Install not currently supported for $DIST_OS'`
1600            exit 1
1601        fi
1602    fi
1603}
1604
1605removedaemon() {
1606    if [ `id | sed 's/^uid=//;s/(.*$//'` != "0" ] ; then
1607        eval echo `gettext 'Must be root to perform this action.'`
1608        exit 1
1609    else
1610        stopit "0"
1611        APP_NAME_LOWER=`echo "$APP_NAME" | $TREXE "[A-Z]" "[a-z]"`
1612        if [ "$DIST_OS" = "solaris" ] ; then
1613            echo 'Detected Solaris:'
1614            if [ -f "/etc/init.d/$APP_NAME" ] ; then
1615                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1616                for i in "/etc/rc3.d/S20$APP_NAME_LOWER" "/etc/rc3.d/K20$APP_NAME_LOWER" "/etc/init.d/$APP_NAME"
1617                do
1618                    rm -f $i
1619                done
1620            else
1621                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1622                exit 1
1623            fi
1624        elif [ "$DIST_OS" = "linux" ] ; then
1625            if [ -f /etc/redhat-release -o -f /etc/redhat_version -o -f /etc/fedora-release ] ; then
1626                echo 'Detected RHEL or Fedora:'
1627                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1628                    eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1629                    /sbin/chkconfig "$APP_NAME" off
1630                    /sbin/chkconfig --del "$APP_NAME"
1631                    rm -f "/etc/init.d/$APP_NAME"
1632                elif [ -f "/etc/init/${APP_NAME}.conf" ] ; then
1633                    echo " Removing $APP_LONG_NAME daemon from upstart..."
1634                    rm "/etc/init/${APP_NAME}.conf"
1635                else
1636                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1637                    exit 1
1638                fi
1639            elif [ -f /etc/slackware-version ] ; then
1640                echo 'Detected Slackware Linux:'
1641                if grep -q ${APP_NAME}router /etc/rc.d/rc.local > /dev/null 2>&1 ; then
1642                    eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1643                    sed -i "/i2prouter/d" /etc/rc.d/rc.local /etc/rc.d/rc.local_shutdown
1644                else
1645                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1646                    exit 1
1647                fi
1648            elif [ -f /etc/arch-release ] ; then
1649                echo 'Detected Arch Linux:'
1650                if [ -f "/etc/rc.d/$APP_NAME" -o -f "$SYSTEMD_SERVICE" ] ; then
1651                    eval echo "`gettext 'Removing $APP_LONG_NAME daemon'`..."
1652                    rm -f "/etc/rc.d/$APP_NAME"
1653                    rm -f "$SYSTEMD_SERVICE"
1654                else
1655                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1656                    exit 1
1657                fi
1658            elif [ -f /etc/SuSE-release ] ; then
1659                echo 'Detected SuSE or SLES:'
1660                if [ -f "/etc/init.d/$APP_NAME" -o ${SYSTEMD_SERVICE} ] ; then
1661                    eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1662                    insserv -r "/etc/init.d/$APP_NAME"
1663                    rm -f "/etc/init.d/$APP_NAME"
1664                    rm -f "$SYSTEMD_SERVICE"
1665                else
1666                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1667                    exit 1
1668                fi
1669            elif [ -f /etc/lsb-release -o -f /etc/debian_version ] ; then
1670                echo 'Detected Debian-based distribution:'
1671                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1672                    echo " Removing $APP_LONG_NAME daemon from init.d..."
1673                    update-rc.d -f "$APP_NAME" remove
1674                    rm -f "/etc/init.d/$APP_NAME"
1675                elif [ -f "/etc/init/${APP_NAME}.conf" ] ; then
1676                    echo " Removing $APP_LONG_NAME daemon from upstart..."
1677                    rm "/etc/init/${APP_NAME}.conf"
1678                else
1679                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1680                    exit 1
1681                fi
1682            else
1683                echo 'Detected Linux:'
1684                if [ -f "/etc/init.d/$APP_NAME" ] ; then
1685                    eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1686                    for i in "/etc/rc3.d/K20$APP_NAME_LOWER" "/etc/rc5.d/K20$APP_NAME_LOWER" "/etc/rc3.d/S20$APP_NAME_LOWER" "/etc/init.d/$APP_NAME" "/etc/rc5.d/S20$APP_NAME_LOWER"
1687                    do
1688                        rm -f $i
1689                    done
1690                else
1691                    eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1692                    exit 1
1693                fi
1694            fi
1695        elif [ "$DIST_OS" = "hpux" ] ; then
1696            echo 'Detected HP-UX:'
1697            if [ -f "/sbin/init.d/$APP_NAME" ] ; then
1698                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1699                for i in "/sbin/rc3.d/K20$APP_NAME_LOWER" "/sbin/rc3.d/S20$APP_NAME_LOWER" "/sbin/init.d/$APP_NAME"
1700                do
1701                    rm -f $i
1702                done
1703            else
1704                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1705                exit 1
1706            fi
1707        elif [ "$DIST_OS" = "aix" ] ; then
1708            echo 'Detected AIX:'
1709            if [ -f "/etc/rc.d/init.d/$APP_NAME" -o -n "`/usr/sbin/lsitab $APP_NAME`" -o -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1710                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1711                if [ -f "/etc/rc.d/init.d/$APP_NAME" ] ; then
1712                    for i in "/etc/rc.d/rc2.d/S20$APP_NAME_LOWER" "/etc/rc.d/rc2.d/K20$APP_NAME_LOWER" "/etc/rc.d/init.d/$APP_NAME"
1713                    do
1714                        rm -f $i
1715                    done
1716                fi
1717                if [ -n "`/usr/sbin/lsitab $APP_NAME`" -o -n "`/usr/bin/lssrc -S -s $APP_NAME`" ] ; then
1718                    /usr/sbin/rmitab $APP_NAME
1719                    /usr/bin/rmssys -s $APP_NAME
1720                fi
1721            else
1722                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1723                exit 1
1724            fi
1725        elif [ "$DIST_OS" = "freebsd" ] ; then
1726            echo 'Detected FreeBSD:'
1727            if [ -f "/etc/rc.d/$APP_NAME" ] ; then
1728                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1729                for i in "/etc/rc.d/$APP_NAME"
1730                do
1731                    rm -f $i
1732                done
1733                sed -i .bak "/${APP_NAME}_enable=\"YES\"/d" /etc/rc.conf
1734            else
1735                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1736                exit 1
1737            fi
1738        elif [ "$DIST_OS" = "macosx" ] ; then
1739            echo 'Detected Mac OSX:'
1740            if [ -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1741                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1742                # Make sure the plist is installed
1743                LOADED_PLIST=`launchctl list | grep ${APP_PLIST_BASE}`
1744                if [ "X${LOADED_PLIST}" != "X" ] ; then
1745                    launchctl unload "/Library/LaunchDaemons/${APP_PLIST}"
1746                fi
1747                rm -f "/Library/LaunchDaemons/${APP_PLIST}"
1748            else
1749                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1750                exit 1
1751            fi
1752        elif [ "$DIST_OS" = "zos" ] ; then
1753            echo 'Detected z/OS:'
1754            if [ -f /etc/rc.bak ] ; then
1755                eval echo " `gettext 'Removing $APP_LONG_NAME daemon'`..."
1756                cp /etc/rc /etc/rc.bak
1757                sed  "s/_BPX_JOBNAME=\'APP_NAME\'.*//g" /etc/rc.bak > /etc/rc
1758                rm /etc/rc.bak
1759            else
1760                eval echo " `gettext 'The $APP_LONG_NAME daemon is not currently installed.'`"
1761                exit 1
1762            fi
1763        else
1764            eval echo `gettext 'Remove not currently supported for $DIST_OS'`
1765            exit 1
1766        fi
1767    fi
1768}
1769
1770dump() {
1771    echo "Dumping $APP_LONG_NAME..."
1772    getpid
1773    if [ "X$pid" = "X" ]
1774    then
1775        eval echo `gettext '$APP_LONG_NAME was not running.'`
1776    else
1777        kill -3 $pid
1778
1779        if [ $? -ne 0 ]
1780        then
1781            echo "Failed to dump $APP_LONG_NAME."
1782            exit 1
1783        else
1784            echo "Dumped $APP_LONG_NAME."
1785        fi
1786    fi
1787}
1788
1789# Used by HP-UX init scripts.
1790startmsg() {
1791    getpid
1792    if [ "X$pid" = "X" ]
1793    then
1794        echo "Starting $APP_LONG_NAME...  Wrapper:Stopped"
1795    else
1796        if [ "X$DETAIL_STATUS" = "X" ]
1797        then
1798            echo "Starting $APP_LONG_NAME...  Wrapper:Running"
1799        else
1800            getstatus
1801            echo "Starting $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS"
1802        fi
1803    fi
1804}
1805
1806# Used by HP-UX init scripts.
1807stopmsg() {
1808    getpid
1809    if [ "X$pid" = "X" ]
1810    then
1811        echo "Stopping $APP_LONG_NAME...  Wrapper:Stopped"
1812    else
1813        if [ "X$DETAIL_STATUS" = "X" ]
1814        then
1815            echo "Stopping $APP_LONG_NAME...  Wrapper:Running"
1816        else
1817            getstatus
1818            echo "Stopping $APP_LONG_NAME...  Wrapper:$STATUS, Java:$JAVASTATUS"
1819        fi
1820    fi
1821}
1822
1823showUsage() {
1824    # $1 bad command
1825
1826    if [ -n "$1" ]
1827    then
1828        echo "Unexpected command: $1"
1829        echo "";
1830    fi
1831
1832    MSG='Usage: '
1833    if [ -n "$FIXED_COMMAND" ] ; then
1834        if [ -n "$PASS_THROUGH" ] ; then
1835            echo "${MSG} $0 {JavaAppArgs}"
1836        else
1837            echo "${MSG} $0"
1838        fi
1839    else
1840            if [ -n "$PASS_THROUGH" ] ; then
1841                echo "${MSG} $0 [ console {JavaAppArgs} | start {JavaAppArgs} | stop | restart {JavaAppArgs} | condrestart {JavaAppArgs} | status | install | remove | dump ]"
1842            else
1843                echo "${MSG} $0 [ console | start | stop | graceful | restart | condrestart | status | install | remove | dump ]"
1844        fi
1845    fi
1846
1847    if [ ! -n "$BRIEF_USAGE" ]
1848    then
1849        echo "";
1850        if [ ! -n "$FIXED_COMMAND" ] ; then
1851            echo "`gettext 'Commands:'`"
1852            echo "  console      `gettext 'Launch in the current console.'`"
1853            echo "  start        `gettext 'Start in the background as a daemon process.'`"
1854            echo "  stop         `gettext 'Stop if running as a daemon or in another console.'`"
1855            echo "  graceful     `gettext 'Stop gracefully, may take up to 11 minutes.'`"
1856            echo "  restart      `gettext 'Stop if running and then start.'`"
1857            echo "  condrestart  `gettext 'Restart only if already running.'`"
1858            echo "  status       `gettext 'Query the current status.'`"
1859            echo "  install      `gettext 'Install to start automatically when system boots.'`"
1860            echo "  remove       `gettext 'Uninstall.'`"
1861            echo "  dump         `gettext 'Request a Java thread dump if running.'`"
1862            echo "";
1863        fi
1864        if [ -n "$PASS_THROUGH" ] ; then
1865            echo "JavaAppArgs: Zero or more arguments which will be passed to the Java application."
1866            echo "";
1867        fi
1868    fi
1869
1870    exit 1
1871}
1872
1873showsetusermesg()  {
1874    echo "`gettext 'Please edit i2prouter and set the variable RUN_AS_USER'`."
1875}
1876
1877checkifstartingasroot() {
1878    if [ ! `grep ^RUN_AS_USER $0` ] && [ ! `grep ^ALLOW_ROOT $0` ] && [ `id -ur` = '0' ]; then
1879        echo "`gettext 'Running I2P as the root user is *not* recommended.'`"
1880        showsetusermesg
1881        echo
1882        echo "`gettext 'To run as root anyway, edit i2prouter and set ALLOW_ROOT=true.'`"
1883        exit 1
1884    fi
1885}
1886
1887docommand() {
1888    case "$COMMAND" in
1889        'console')
1890            checkifstartingasroot
1891            checkUser touchlock "$@"
1892            if [ ! -n "$FIXED_COMMAND" ] ; then
1893                shift
1894            fi
1895            console "$@"
1896            ;;
1897
1898        'start')
1899            checkifstartingasroot
1900            if [ "$DIST_OS" = "macosx" -a -f "/Library/LaunchDaemons/${APP_PLIST}" ] ; then
1901                macosxstart
1902            elif [ "$DIST_OS" = "linux" -a -f "/etc/init/${APP_NAME}.conf" ] ; then
1903                checkUser touchlock "$@"
1904                upstartstart
1905            else
1906                checkUser touchlock "$@"
1907                if [ ! -n "$FIXED_COMMAND" ] ; then
1908                    shift
1909                fi
1910                start "$@"
1911            fi
1912            ;;
1913
1914        'stop')
1915            checkUser "" "$COMMAND"
1916            stopit "0"
1917            ;;
1918
1919        'graceful')
1920            checkUser "" "$COMMAND"
1921            graceful "0"
1922            ;;
1923
1924        'restart')
1925            checkifstartingasroot
1926            checkUser touchlock "$COMMAND"
1927            if [ ! -n "$FIXED_COMMAND" ] ; then
1928                shift
1929            fi
1930            stopit "0"
1931            start "$@"
1932            ;;
1933
1934        'condrestart')
1935            checkifstartingasroot
1936            checkUser touchlock "$COMMAND"
1937            if [ ! -n "$FIXED_COMMAND" ] ; then
1938                shift
1939            fi
1940            stopit "1"
1941            start "$@"
1942            ;;
1943
1944        'status')
1945            checkUser "" "$COMMAND"
1946            status
1947            ;;
1948
1949        'install')
1950            installdaemon
1951            if [ ! `grep ^RUN_AS_USER $0` ]; then
1952                 echo
1953                 showsetusermesg
1954            fi
1955            echo
1956            echo "You may want to disable the browser from launching at startup at"
1957            echo "http://127.0.0.1:7657/configclients"
1958            echo
1959            echo "I2P_CONFIG_DIR is currently set to $I2P_CONFIG_DIR."
1960            echo "Change the value in $0 if this is not"
1961            echo "appropriate for your configuration."
1962            ;;
1963
1964        'remove' | 'uninstall')
1965            removedaemon
1966            ;;
1967
1968        'dump')
1969            checkUser "" "$COMMAND"
1970            dump
1971            ;;
1972
1973        'start_msg')
1974            # Internal command called by launchd on HP-UX.
1975            checkUser "" "$COMMAND"
1976            startmsg
1977            ;;
1978
1979        'stop_msg')
1980            # Internal command called by launchd on HP-UX.
1981            checkUser "" "$COMMAND"
1982            stopmsg
1983            ;;
1984
1985        'launchdinternal')
1986            # Internal command called by launchd on Max OSX.
1987            # We do not want to call checkUser here as it is handled in the launchd plist file.  Doing it here would confuse launchd.
1988            if [ ! -n "$FIXED_COMMAND" ] ; then
1989                shift
1990            fi
1991            launchdinternal "$@"
1992            ;;
1993
1994        *)
1995            showUsage "$COMMAND"
1996            ;;
1997    esac
1998}
1999
2000docommand "$@"
2001
2002exit 0
Note: See TracBrowser for help on using the repository browser.