#!/bin/sh

FIRST_LABEL=30002

LABEL_DEVICE="D"
LABEL_ENV="E_"
LABEL_PORT="P_"
LABEL_VOLUME="V"

STRING_DEVICE="-d "
STRING_ENV="-e "
STRING_PORT="-p "
STRING_VOLUME="-v "

add_default() {
    defaults="${defaults}    <setting id=\"${1}\" value=\"${2}\" />\n"
}

add_device() {
    add_label
    default="${1%:*}"
    docker="${1#*:}"
    id="${LABEL_DEVICE}${docker//\//_}"
    add_default "${id}" "${default}"
    add_option "--device=\"\$${id}\""
    add_setting "<setting label=\"${label}\" type=\"folder\" id=\"${id}\" default=\"${default}\" />"
    add_string "${STRING_DEVICE}${docker}"
}

add_env() {
    add_label
    default="${1#*=}"
    docker="${1%%=*}"
    id="${LABEL_ENV}${docker}"
    add_default "${id}" "${default}"
    add_option "-e ${docker}=\"${id}\""
    add_setting "<setting label=\"${label}\" type=\"text\"   id=\"${id}\" default=\"${default}\" />"
    add_string "${STRING_ENV}${docker}"
}

add_label() {
    if [ -z ${label} ]; then
      label=${FIRST_LABEL}
    else
      label=$((label+1))
    fi
}

add_option() {
    if [ ! -z "${options}" ]; then
        options="${options} "'\\\n'
    fi
    options="${options}           ${1}"
}

add_port() {
    add_label
    default="${1%:*}"
    docker="${1#*:}"
    port="${docker%/*}"
    id="${LABEL_PORT}${port}"
    add_default "${id}" "${default}"
    add_option "-p \"\$${id}\":${docker}"
    add_setting "<setting label=\"${label}\" type=\"number\" id=\"${id}\" default=\"${default}\" />"
    add_string "${STRING_PORT}${port}"
}

add_setting() {
    settings="${settings}      ${1}\n"
}

add_string() {
    strings="${strings}\nmsgctxt \"${label}\"\nmsgid   \"${1}\"\nmsgstr  \"\"\n"
}

add_volume() {
    add_label
    default="${1%:*}"
    docker="${1#*:}"
    id="${LABEL_VOLUME}${docker//\//_}"
    add_default "${id}" "${default}"
    add_option "-v \"\$${id}\":${docker}"
    add_setting "<setting label=\"${label}\" type=\"folder\" id=\"${id}\" default=\"${default}\" />"
    add_string "${STRING_VOLUME}${docker}"
}


case "${ARCH}" in
    arm)
        projects="imx6 RPi RPi2"
        ;;
    x86_64)
        projects="Generic"
        ;;
    *)
        echo "Unkown project"
        exit 1
        ;;
esac

while [[ $# -gt 0 ]]; do
    option="${1}"
    shift
    case "${option}" in
        --device=*)
            add_device "${option#--device=}"
            ;;
        --name=*)
            ;;
        --port=*)
            add_port "${option#--port=}"
            ;;
        --volume=*)
            add_volume "${option#--volume=}"
            ;;
        -e)
            add_env "${1}"
            shift
            ;;

        -p)
            add_port "${1}"
            shift
            ;;
        -v)
            add_volume "${1}"
            shift
            ;;
        -*)
            add_option "${option}"
            ;;
        */*)
            add_option "${option}"
            image="${option%:*}"
            version="${option#*:}"
            if [ "${version}" = "${option}" ]; then
                version="latest"
            fi
            if [ ! -z "$*" ]; then
                add_option "$*"
            fi
            break
            ;;
        *)
            add_option "${option}"
            ;;
    esac    
done

if [ -z "${image}" ]; then
    echo "Failed to parse image"
    exit 1
fi

name="${image//\//.}"
dir="packages/addons/docker/${name}"

if [ -d "${dir}" ]; then
    echo "Package already exists"
    exit 1
fi

cp -R config/docker "${dir}"

sed -e "s|@NAME@|${name}|g" \
    -e "s|@VERSION@|${version}|g" \
    -e "s|@ARCH@|${arch}|g" \
    -e "s|@IMAGE@|${image}|g" \
    -e "s|@PROJECTS@|${projects}|g" \
    -i "${dir}/package.mk"


defaults="$(echo -en "${defaults}" | sort)"
echo -en "<settings>\n${defaults}\n</settings>" > "${dir}/source/settings-default.xml"

echo -en "${options}" >> "${dir}/source/bin/docker"

sed -e "s|@SETTINGS@|${settings}|g" \
    -i "${dir}/source/resources/settings.xml"

echo -en "${strings}" >> "${dir}/source/resources/language/English/strings.po"

mv "${dir}/source/bin/docker" "${dir}/source/bin/docker.${name}"
mv "${dir}/source/system.d/docker.service" "${dir}/source/system.d/docker.${name}.service"
