#!/bin/sh /etc/rc.common

START=98
STOP=05
USE_PROCD=1

add_webdav_user() {
    local username
    local password
    local rw
    local ro
    config_get username $1 username
    [ -z "$username" ] && return 1
    [ "$username" = "everyone" -o "$username" = "users" ] && return 0
    config_get password $1 password
    config_get rw $1 rw
    config_get ro $1 ro
    echo "  - username: $username"
    echo "    password: $password"
    echo "    rules:"

    local r
    for r in $rw; do
        echo "      - path: $r"
        echo "        modify: true"
    done
    for r in `uci -q get webdav2.users.rw`; do
        echo "      - path: $r"
        echo "        modify: true"
    done
    for r in $ro; do
        echo "      - path: $r"
        echo "        allow: true"
    done
    for r in `uci -q get webdav2.users.ro`; do
        echo "      - path: $r"
        echo "        allow: true"
    done
}

config_webdav_header() {
    local r
    local anonymous=false
    [[ $WEBDAV2_G_ANONYMOUS == 1 ]] && anonymous=true
    cat <<-EOF
# Server related settings
address: 0.0.0.0
port: $WEBDAV2_G_PORT
auth: true
anonymous: $anonymous
tls: false
cert: cert.pem
key: key.pem
prefix: /
no_sniff: true
debug: false

# Default user settings (will be merged)
scope: $WEBDAV2_G_ROOT
modify: false
rules:
EOF
    for r in `uci -q get webdav2.everyone.rw`; do
        echo "  - path: $r"
        echo "    modify: true"
    done
    for r in `uci -q get webdav2.everyone.ro`; do
        echo "  - path: $r"
        echo "    allow: true"
    done
    [ "`uci -q get webdav2.everyone.onlyroot`" = "1" ] && cat <<-EOF
  - regex: true
    allow: false
    path: ^/.+
EOF

    cat <<-EOF
  - regex: false
    allow: true
    path: /

# CORS configuration
cors:
  enabled: true
  credentials: true
  allowed_headers:
    - Depth
  allowed_hosts:
    - http://localhost:$WEBDAV2_G_PORT
  allowed_methods:
    - GET
  exposed_headers:
    - Content-Length
    - Content-Range

users:
EOF
}

config_webdav2() {
    rm -rf /var/run/webdav2 2>/dev/null
    mkdir -p /var/run/webdav2 || return 1

    { config_webdav_header ; config_foreach add_webdav_user user ; } > /var/run/webdav2/webdav.yml
}

global_config() {
    local enabled
    local anonymous
    local port
    local root
    config_get enabled $1 enabled
    config_get anonymous $1 anonymous
    config_get port $1 port
    config_get root $1 root
    export -n "WEBDAV2_G_ENABLED=$enabled"
    export -n "WEBDAV2_G_ANONYMOUS=$anonymous"
    export -n "WEBDAV2_G_PORT=$port"
    export -n "WEBDAV2_G_ROOT=$root"
}

start_service() {
    config_load webdav2
    config_foreach global_config global
    [ "$WEBDAV2_G_ENABLED" = "1" ] || return 0
    [ -z "$WEBDAV2_G_PORT" ] && WEBDAV2_G_PORT=8888
    config_webdav2 || return 1

    # start webdav2 daemon
    procd_open_instance
    procd_set_param command /usr/sbin/webdav2 -c /var/run/webdav2/webdav.yml
    procd_set_param respawn
    procd_set_param limits nofile=16384
    procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "webdav2"
}
