From 21f4d8e858745c5f6605059c0625962e1dd6e5c5 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Fri, 13 Apr 2018 09:33:16 +1000 Subject: [PATCH] Add init.d service file & modify how configDirName is set * Add init.d service file & helper script to start service * Change how configDirName is set as XDG_CONFIG_HOME does not exist on systems where X11 is not present. When using init scripts and using XDG_CONFIG_HOME, ~ is not expanded thus existing config cannot be found. Using ~ in --confdir also does not work when running under init.d --- init.d/onedrive.init | 81 ++++++++++++++++++++++++++++++++++++++ init.d/onedrive_service.sh | 5 +++ src/main.d | 13 +++++- 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 init.d/onedrive.init create mode 100644 init.d/onedrive_service.sh diff --git a/init.d/onedrive.init b/init.d/onedrive.init new file mode 100644 index 00000000..cb892a68 --- /dev/null +++ b/init.d/onedrive.init @@ -0,0 +1,81 @@ +#!/bin/sh +# +# chkconfig: 2345 20 80 +# description: Starts and stops OneDrive Free Client +# + +# Source function library. +if [ -f /etc/init.d/functions ] ; then + . /etc/init.d/functions +elif [ -f /etc/rc.d/init.d/functions ] ; then + . /etc/rc.d/init.d/functions +else + exit 1 +fi + +# Source networking configuration. +. /etc/sysconfig/network + +# Check that networking is up. +[ ${NETWORKING} = "no" ] && exit 1 + +APP_NAME="OneDrive Free Client" +STOP_TIMEOUT=${STOP_TIMEOUT-5} +RETVAL=0 + +start() { + echo -n $"Starting $APP_NAME: " + daemon --user root onedrive_service.sh + RETVAL=$? + echo + [ $RETVAL -eq 0 ] && touch /var/lock/subsys/onedrive || \ + RETVAL=1 + return $RETVAL +} + +stop() { + echo -n $"Shutting down $APP_NAME: " + killproc onedrive + RETVAL=$? + echo + [ $RETVAL = 0 ] && rm -f /var/lock/subsys/onedrive ${pidfile} +} + +restart() { + stop + start +} + +rhstatus() { + status onedrive + return $? +} + +# Allow status as non-root. +if [ "$1" = status ]; then + rhstatus + exit $? +fi + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + reload) + reload + ;; + status) + rhstatus + ;; + *) + echo $"Usage: $0 {start|stop|restart|reload|status}" + exit 2 +esac + +exit $? \ No newline at end of file diff --git a/init.d/onedrive_service.sh b/init.d/onedrive_service.sh new file mode 100644 index 00000000..09cceaec --- /dev/null +++ b/init.d/onedrive_service.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# This script is to assist in starting the onedrive client when using init.d +APP_OPTIONS="--monitor --verbose" +onedrive $APP_OPTIONS > /dev/null 2>&1 & +exit 0 \ No newline at end of file diff --git a/src/main.d b/src/main.d index 9e509de1..e75de5b3 100644 --- a/src/main.d +++ b/src/main.d @@ -6,8 +6,17 @@ static import log; int main(string[] args) { + // Determine the user home directory. + // Need to avoid using ~ here as expandTilde() below does not interpret correctly when running under init.d scripts + string homePath = environment.get("XDG_CONFIG_HOME"); + if (homePath == ""){ + // XDG_CONFIG_HOME does not exist on systems where X11 is not present - ie - headless systems / servers + // Get HOME environment variable + homePath = environment.get("HOME"); + } + // configuration directory - string configDirName = environment.get("XDG_CONFIG_HOME", "~/.config") ~ "/onedrive"; + string configDirName = homePath ~ "/.config/onedrive"; // only download remote changes bool downloadOnly; // override the sync directory @@ -96,8 +105,10 @@ int main(string[] args) string logFilePath = "/var/log/onedrive/"; if (!exists(logFilePath)) mkdirRecurse(logFilePath); + // load configuration log.vlog("Loading config ..."); configDirName = configDirName.expandTilde().absolutePath(); + log.vlog("Using Config Dir: ", configDirName); if (!exists(configDirName)) mkdirRecurse(configDirName); auto cfg = new config.Config(configDirName); cfg.init();