#!/bin/sh

# Author:  Boris Pek <tehnick-8@yandex.ru>
# License: Public Domain
# Created: 2019-05-30
# Updated: 2019-06-01
# Version: N/A
#
# Dependencies: xdg-open

set -e

BIN_DIR="$(dirname ${0})"
DEST_DIR="${HOME}/.local/share/eiskaltdc++/web"

TEST_DIRS=" \
    /usr/local/share/eiskaltdcpp/web \
    /usr/share/eiskaltdcpp/web \
    ${BIN_DIR}/../share/eiskaltdcpp/web \
    "

CONFIG="config.js"
MAIN_PAGE="index.html"
SRC_FILES="style.css favicon.ico"
SRC_DIRS="images js"

export SRC_DIR=""

FindSourceDirectory()
{
    for DIR in ${TEST_DIRS}
    do
        if [ -e "${DIR}/${MAIN_PAGE}" ]
        then
            export SRC_DIR="${DIR}"
            return 0
        fi
    done
}

ShowError()
{
    if [ -z "${1}" ]
    then
        echo "EiskaltDC++ Web UI is not found in your system!"
    else
        echo "${1}"
    fi
    exit 1
}

if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
then
    echo "Usage:
  $(basename ${0}) <Key>

EiskaltDC++ is a cross-platform program that uses the Direct Connect and ADC protocols.

Keys:
  -h, --help     Show this message"
    exit 0
fi

mkdir -p "${DEST_DIR}"

# Keep ${MAIN_PAGE} file up to date.
if true
then
    [ -z "${SRC_DIR}" ] && FindSourceDirectory
    [ -z "${SRC_DIR}" ] && ShowError

    # Symlink cannot be used here because webrowser will resolve it and will
    # open original path. Hard link cannot be used here because not all
    # file systems support this feature.
    cp -fL "${SRC_DIR}/${MAIN_PAGE}" "${DEST_DIR}/${MAIN_PAGE}"
fi

# Copy ${CONFIG} only one time to let user modify it and use this modified copy.
if [ ! -e "${DEST_DIR}/${CONFIG}" ]
then
    [ -z "${SRC_DIR}" ] && FindSourceDirectory
    [ -z "${SRC_DIR}" ] && ShowError

    [ ! -e "${SRC_DIR}/${CONFIG}" ] && \
        ShowError "File \"${SRC_DIR}/${CONFIG}\" does not exist!"

    cp -L "${SRC_DIR}/${CONFIG}" "${DEST_DIR}/${CONFIG}"
fi

# Other files and directories may be used by symlinks
for FILE in ${SRC_FILES}
do
    if [ ! -e "${DEST_DIR}/${FILE}" ]
    then
        [ -z "${SRC_DIR}" ] && FindSourceDirectory
        [ -z "${SRC_DIR}" ] && ShowError

        [ ! -e "${SRC_DIR}/${FILE}" ] && \
            ShowError "File \"${SRC_DIR}/${FILE}\" does not exist!"

        ln -sf "${SRC_DIR}/${FILE}" "${DEST_DIR}/${FILE}"
    fi
done

for DIR in ${SRC_DIRS}
do
    if [ ! -d "${DEST_DIR}/${DIR}" ]
    then
        [ -z "${SRC_DIR}" ] && FindSourceDirectory
        [ -z "${SRC_DIR}" ] && ShowError

        [ ! -d "${SRC_DIR}/${DIR}" ] && \
            ShowError "Directory \"${SRC_DIR}/${DIR}\" does not exist!"

        ln -sfT "${SRC_DIR}/${DIR}" "${DEST_DIR}/${DIR}"
    fi
done

if ! which xdg-open > /dev/null
then
    ShowError "xdg-open is not installed!"
fi

# If all checks have been passed successfully:
echo "Used configuration file:"
echo "${DEST_DIR}/${CONFIG}"

xdg-open "${DEST_DIR}/index.html"
exit 0

