updated on Sat Jan 14 12:12:45 UTC 2012
[aur-mirror.git] / sv-helper / sv-helper.sh
blob261a81df23b422ae9cfb9cbf9feacf028ee03ac0
1 #!/bin/sh
3 # Locate the service in the user's $SVDIR or /etc/sv
4 function find_service {
5 service=$1
6 svdir=$(svdir 2>/dev/null)
7 if [ -d "$svdir/../sv/$service" ];then
8 echo "$svdir/../sv/$service"
9 elif [ -d "$svdir/../Service/$service" ];then
10 echo "$svdir/../Service/$service"
11 elif [ -d "$svdir/../Services/$service" ];then
12 echo "$svdir/../Services/$service"
13 elif [ "$svdir" != "/etc/sv/$service" -a -d "/etc/sv/$service" ];then
14 echo "/etc/sv/$service"
18 # Set to user's $SVDIR or /service
19 function svdir {
20 if [ -z $SVDIR ];then
21 #echo "using /service" >&2
22 svdir=/service
23 else
24 #echo "using $SVDIR" >&2
25 svdir=$SVDIR
27 echo $svdir
30 # Add sudo if we don't own the directory in question
31 function check_owner {
32 lndir=$1
33 if [ ! -w $lndir ];then
34 echo "sudo "
38 # Symlink a service (from find_service's path to `svdir`/$service)
39 function enable {
40 echo "Enabling $1" >&2
41 service=$1
42 svdir=$(find_service $service)
43 if [ -z "$svdir" -o ! -d "$svdir" ];then
44 echo "No such service '$service'" >&2
45 exit 1
47 ln_dir=$(svdir)
48 if [ -L "$ln_dir/$service" ];then
49 echo "Service already enabled!" >&2
50 echo " $(sv s $ln_dir/$service)" >&2
51 exit 1
53 $(check_owner $ln_dir) ln -s $svdir $ln_dir
56 # Remove a symlink of a service (from find_service's path to `svdir`/$service)
57 function disable {
58 echo "Disabling $1" >&2
59 service=$1
60 ln_dir=$(svdir)
61 if [ ! -L "$ln_dir/$service" ];then
62 echo "Service not enabled!" >&2
63 exit 1
65 $(check_owner $ln_dir) rm $ln_dir/$service
68 # Generic list, of one service or all
69 function list {
70 svdir=$(svdir)
71 if [ ! -z "$1" ];then
72 $(check_owner $svdir) sv s "$svdir/"$1
73 else
74 echo "Listing All Services"
75 $(check_owner $svdir) sv s "$svdir/"*
79 # Usage
80 function usage {
81 cmd=$1
82 case "$cmd" in
83 sv-enable) echo "sv-enable <service> - Enable a service and start it (will restart on boots)";;
84 sv-disable) echo "sv-disable <service> - Disable a service from starting (also stop the service)";;
85 svls) echo "svls [<service>] - Show list of services (Default: all services, pass a service name to see just one)";;
86 sv-find) echo "sv-find <service> - Find a service, if it exists";;
87 sv-list) echo "sv-list - List available services";;
88 *) echo "Invalid command (sv-list svls sv-find sv-enable sv-disable)";;
89 esac
92 # Start main program
94 cmd=$(basename $0) # Get the command
96 # help
97 while getopts h options
99 case $options in
100 h) echo $(usage $cmd)
101 exit;;
102 esac
103 done
105 case "$cmd" in
106 sv-enable) enable $@;;
107 sv-disable) disable $@;;
108 svls) list $@;;
109 sv-find) find_service $@;;
110 sv-list) find $(find_service) -maxdepth 1 -mindepth 1 -type d -exec basename {} \; ;;
111 *) usage;
112 break;;
113 esac