#!/bin/bash set -e # Logging function log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] [WebDAV] $*" } log "Initializing WebDAV service..." # Load configuration user_home="${HOME:-/home/user}" config_file="$user_home/config/webdav.conf" # Default configuration values WEBDAV_HOST="${WEBDAV_HOST:-0.0.0.0}" WEBDAV_PORT="${WEBDAV_PORT:-8081}" WEBDAV_USERNAME="${WEBDAV_USERNAME:-webdav}" WEBDAV_PASSWORD="${WEBDAV_PASSWORD:-webdav123}" WEBDAV_ROOT_DIR="${WEBDAV_ROOT_DIR:-$user_home/webdav}" WEBDAV_ENABLE_UI="${WEBDAV_ENABLE_UI:-true}" # Load config file if exists if [[ -f "$config_file" ]]; then log "Loading WebDAV configuration from: $config_file" source "$config_file" else log "No WebDAV configuration file found, using defaults" fi # Ensure directories exist mkdir -p "$user_home/webdav/data" "$user_home/webdav/config" "$WEBDAV_ROOT_DIR" # Create FlyDAV configuration file webdav_config_file="$user_home/webdav/config/flydav.toml" log "Creating FlyDAV configuration file: $webdav_config_file" # Generate SHA256 hash of the password password_hash=$(echo -n "$WEBDAV_PASSWORD" | sha256sum | cut -d' ' -f1) cat > "$webdav_config_file" << EOF [server] host = "$WEBDAV_HOST" port = $WEBDAV_PORT path = "/webdav" fs_dir = "$WEBDAV_ROOT_DIR" [auth] [[auth.user]] username = "$WEBDAV_USERNAME" sub_fs_dir = "" sub_path = "" password_hash = "$password_hash" password_crypt = "sha256" [log] level = "info" [[log.stdout]] format = "text" output = "stdout" EOF log "WebDAV configuration created successfully" log "Server: $WEBDAV_HOST:$WEBDAV_PORT" log "Root directory: $WEBDAV_ROOT_DIR" log "Username: $WEBDAV_USERNAME" log "WebDAV URL: http://$WEBDAV_HOST:$WEBDAV_PORT/webdav" # Check if FlyDAV is available if ! command -v flydav >/dev/null 2>&1; then log "ERROR: FlyDAV command not found!" log "Please ensure WebDAV installation completed successfully" exit 1 fi # Start WebDAV server log "Starting WebDAV server..." # Add UI flag if enabled UI_FLAG="" if [[ "${WEBDAV_ENABLE_UI}" == "true" ]]; then UI_FLAG="--with-ui" log "WebDAV Web UI enabled" log "Web UI URL: http://$WEBDAV_HOST:$WEBDAV_PORT/" fi log "Executing: flydav -c \"$webdav_config_file\" $UI_FLAG" # Start the service with proper signal handling exec flydav -c "$webdav_config_file" $UI_FLAG