#!/sbin/openrc-run # Copyright 2024 Willy Garidol # Distributed under the terms of the GNU General Public License v3 depend() { need localmount net } # Name of the share WD_SHARE_NAME="${SHARE_NAME}" # Where is the original data WD_DATA_FOLDER="${DATA_FOLDER}" # Where WebDAV temporary stuff will be located WD_TEMP_FOLDER="${TEMP_FOLDER}" WD_ROOT_FOLDER="${WD_TEMP_FOLDER}/root" WD_MOUNT_FOLDER="${WD_TEMP_FOLDER}/root/webdav/${WD_SHARE_NAME}" WD_LOCKS_FOLDER="${WD_TEMP_FOLDER}/locks" WD_TIMEOUT=${TIMEOUT:-5} WD_LOG_PATH="/var/log/webdav" WD_SLOT="${SVCNAME#webdav.}" WD_USER=${USER:-${WD_SLOT}} WD_GROUP=${GROUP:-${WD_SLOT}} description=${DESCRIPTION:-WebDAV starter} pidfile="/run/${RC_SVCNAME}.pid" apache_args=( -c "ServerRoot /usr/lib64/apache2" -c "LoadModule dav_module modules/mod_dav.so" -c "LoadModule dav_fs_module modules/mod_dav_fs.so" -c "LoadModule dav_lock_module modules/mod_dav_lock.so" -c "Include /etc/apache2/modules.d/*.conf" -c "User ${WD_USER}" -c "Group ${WD_GROUP}" -c "DavLockDB ${WD_TEMP_FOLDER}/locks" -c "PidFile ${pidfile}" -c "ErrorLog ${WD_LOG_PATH}/${WD_SLOT}/error.log" -c "CustomLog ${WD_LOG_PATH}/${WD_SLOT}/access.log common" -c "DocumentRoot ${WD_ROOT_FOLDER}" -c "ServerName 127.0.0.1" -c "Listen 127.0.0.1:${PORT}" -c "" -c " DAV On" -c " AllowOverride All" -c " Options -Indexes +FollowSymlinks -ExecCGI -Includes" -c " Require all granted" -c "" -c "SetEnv redirect-carefully" ) start_pre() { # script must be run with ".sharename" symlink: if [ "${WD_SLOT}" = "webdav" ] then ebegin "Error: do not run this script, run a link to it!" eend 255 return 255 fi # Data folder must exist: if [ -z ${WD_DATA_FOLDER} -o ! -d ${WD_DATA_FOLDER} ] then ebegin "Error: DATA_FOLDER must be defined and path must exist!" eend 255 return 255 fi # Create log paths test -e "${WD_LOG_PATH}" || mkdir "${WD_LOG_PATH}" test -e "${WD_LOG_PATH}/${WD_SLOT}" || { ebegin "Creating log path '${WD_LOG_PATH}/${WD_SLOT}'" mkdir "${WD_LOG_PATH}/${WD_SLOT}" } && chown -R ${WD_USER} "${WD_LOG_PATH}/${WD_SLOT}" # Create all temporary paths: for path in ${WD_TEMP_FOLDER} ${WD_ROOT_FOLDER} ${WD_MOUNT_FOLDER} ${WD_LOCKS_FOLDER} do test -e ${path} || { ebegin "Creating '${path}' path" mkdir -p ${path} chown ${WD_USER}:${WD_GROUP} ${path} } done test -z "$(mount | grep ${WD_MOUNT_FOLDER})" && { ebegin "Mounting/binding root path '${WD_DATA_FOLDER}' -> '${WD_MOUNT_FOLDER}'" mount -o bind ${WD_DATA_FOLDER} ${WD_MOUNT_FOLDER} } eend 0 } start() { start-stop-daemon -w ${WD_TIMEOUT} --start --pidfile "${pidfile}" -- \ /usr/bin/apache2 "${apache_args[@]}" eend $? } stop_post() { test -n "$(mount | grep ${WD_MOUNT_FOLDER})" && { ebegin "Unmounting/unbinding root path '${WD_DATA_FOLDER}' -|-> '${WD_MOUNT_FOLDER}'" umount ${WD_MOUNT_FOLDER} } eend 0 }