#!/bin/sh

if [ -z "$1" ] ; then
    echo "usage: install-service <service_name>"
else
    # Check if the service directory exists in /etc/sv
    if [ -d "/etc/sv/$1/" ] ; then
        # Yeah it's there, but does it have an executable run script, hmm?
        if [ -x "/etc/sv/$1/run" ] ; then
            # So far so good, but has it already been installed?
            if [ -d "/service/$1" ] ; then
                echo "/service/$1 already exists."
                exit 0
            else
                echo -n "Creating symlink... "
                ln -s "/etc/sv/$1" "/service/$1"
                # Did this actually create the symlink?
                if [ -L "/service/$1" ] ; then
                    echo "Done!"
                    exit 0
                else
                    echo "Failed!"
                    exit 1
                fi
            fi            
        else
            echo "/etc/sv/$1/run is missing or not executable."
            exit 1
        fi
    else
        echo "/etc/sv/$1/ is not a valid directory."
        exit 1
    fi
fi
