# Function to prepare runit service directories for a particular daemon
# To be invoked inside a package build file.
preprunitservice() {
              	
   # usage: $ preprunitservice <chrony> <down> <finish>
   # Will create the chrony service directories with a down and a finish file to prevent auto-execution at next boot 
   # and to provide a clean exit respectively. Use the first argument to define the service name
   rsname=$1
   # Use the second argument to add the down file
   down=$2
   # Use the third argument to add the finish file
   finish=$3
   
   # Enter the staging directory
   cd "$PKG"

   # Create the service directories
   mkdir -p "etc/service/$1" var/service 

   # Copy the service run file from the source directory into etc/service/$1
   if [ -f "$SRCDIR/$1.run" ] ; then
     cp "$SRCDIR/$1.run" "etc/service/$1/run"
   else
     echo "$1.run does not exist! Exiting"
     exit 1
   fi

   # If the second argument is "down", or if the second argument is "finish", create that file inside etc/service/$1/
   if [ "$2" = "down" ]; then
     touch "etc/service/$1/down"
   elif [ "$2" = "finish" ]; then
     cp "$SRCDIR/$1.$2" "etc/service/$1/finish"
   fi

   # If the third argument is "finish", copy that file from the source directory into etc/service/$1/
   [ -n "$3" ] && cp "$SRCDIR/$1.$3" "etc/service/$1/finish"

   # Create the symlinks between etc/service and var/service
   ln -s "../../etc/service/$1" "var/service/$1"

   # Finally set the executable permissions on the run file
   chmod 0755 "$PKG/etc/service/$1/run"
}

