Ticket #609: portrc

File portrc, 2.7 KB (added by jcorley1@…, 21 years ago)

portrc shell script

Line 
1#!/bin/sh
2#
3# chkconfig like script for DarwinPorts rc scripts
4#  - Jason Corley <jcorley1ATncDOTrrDOTcom>
5#
6
7# Set up the appropriate variables
8action="$1"
9conf="/Library/StartupItems/DarwinPortsStartup/prefix.conf"
10# Use basename in case someone passes the full path to the service
11service=$(basename $2)
12
13# Simple but verbose usage function
14usage() {
15    echo
16    echo "Usage:  $0 [target] appname.sh"
17    echo ""
18    echo "    $0 enables or disables an application's ability to start"
19    echo "    at boot time.  [target] is either enable or disable."
20    echo ""
21    echo ""
22    echo "Example:  $0 enable apache.sh"
23    echo ""
24    echo "    Will enable the apache service to start at boot time."
25    echo ""
26    echo "Example: $0 disable apache.sh"
27    echo ""
28    echo "    Will disable the apache service so that it does not start"
29    echo "    at boot time."
30    exit 1
31}
32
33# Make sure the first argument wasn't a cry for help
34if [ "$action" == "help" ] \
35   || [ "$action" = "--help" ] \
36   || [ "$action" = "--h" ] \
37   || [ "$action" = "-h" ]; then
38    usage
39fi
40
41# Make sure we got the correct number of arguments
42if [ ! "$#" -eq 2 ]; then
43    usage
44fi
45
46# Find out what the proper prefix should be
47if [ -r "${conf}" ]; then
48    prefix=$(grep -v "^[[:space:]]*#" "${conf}")
49else
50    prefix="/opt/local"
51fi
52
53# Make sure the application startup script exists and is writable
54if [ ! -w ${prefix}/etc/rc.d/${service} ]; then
55    echo "${prefix}/etc/rc.d/${service} does not exist or is not writable!"
56    echo ""
57    echo "Check your permissions to make sure that you can write to the file."
58    usage
59fi
60
61# Make sure the application supports portrc syntax
62grep -q disable ${prefix}/etc/rc.d/${service}
63if [ "$?" != "0" ]; then
64    echo "${prefix}/etc/rc.d/${service} does not support portrc"
65    exit 1
66fi
67
68# Actually do what we were called to do (finally)
69case $action in
70    enable)
71        tempfoo=$(basename $0)
72        TMPFILE=$(mktemp -q /tmp/${tempfoo}.XXXXXX)
73        if [ $? -ne 0 ]; then
74            echo "$0: Can't create temp file, exiting..."
75            exit 1
76        fi
77        sed 's|^.disable.*|#disable = no|g' ${prefix}/etc/rc.d/${service} > ${TMPFILE}
78        mv -f ${TMPFILE} ${prefix}/etc/rc.d/${service}
79        chmod 755 ${prefix}/etc/rc.d/${service}
80        ;;
81    disable)
82        tempfoo=$(basename $0)
83        TMPFILE=`mktemp -q /tmp/${tempfoo}.XXXXXX`
84        if [ $? -ne 0 ]; then
85            echo "$0: Can't create temp file, exiting..."
86            exit 1
87        fi
88        sed 's|^.disable.*|#disable = yes|g' ${prefix}/etc/rc.d/${service} > ${TMPFILE}
89        mv -f ${TMPFILE} ${prefix}/etc/rc.d/${service}
90        chmod 755 ${prefix}/etc/rc.d/${service}
91        ;;
92    *)
93        usage
94        ;;
95esac
96