#!/bin/sh

# Agents and modules.

AGENTS="imip_person.py imip_person_outgoing.py imip_resource.py"
MODULES="markup.py imip_store.py vCalendar.py vContent.py vRecurrence.py"
INSTALL_DIR=/var/lib/imip-agent
CONFIG_DIR=/etc/imip-agent

if [ ! -e "$INSTALL_DIR" ]; then
    mkdir -p "$INSTALL_DIR"
fi

cp $AGENTS "$INSTALL_DIR"
cp $MODULES "$INSTALL_DIR"

if [ ! -e "$INSTALL_DIR/imiptools" ]; then
    mkdir "$INSTALL_DIR/imiptools"
    if [ ! -e "$INSTALL_DIR/imiptools" ]; then
        mkdir "$INSTALL_DIR/imiptools/handlers"
    fi
fi

# Remove any symbolic link to the config module.

if [ -h "$INSTALL_DIR/imiptools/config.py" ]; then
    rm "$INSTALL_DIR/imiptools/config.py"
fi

# Copy modules into the installation directory.

cp imiptools/*.py "$INSTALL_DIR/imiptools/"
cp imiptools/handlers/*.py "$INSTALL_DIR/imiptools/handlers/"

# Install the config module in a more appropriate location.

if [ ! -e "$CONFIG_DIR" ]; then
    mkdir -p "$CONFIG_DIR"
fi

if [ -e "$CONFIG_DIR/config.py" ]; then
    if [ ! -e "$CONFIG_DIR/config.py.backup" ]; then
        mv "$CONFIG_DIR/config.py" "$CONFIG_DIR/config.py.backup"
    fi
fi

# Replace the config module with a symbolic link.

mv "$INSTALL_DIR/imiptools/config.py" "$CONFIG_DIR/"
ln -s "$CONFIG_DIR/config.py" "$INSTALL_DIR/imiptools/config.py"

# Tools

if [ ! -e "$INSTALL_DIR/tools" ]; then
    mkdir -p "$INSTALL_DIR/tools"
fi

cp tools/make_freebusy.py "$INSTALL_DIR/tools/"

# Web manager interface.

WEB_INSTALL_DIR=/var/www/imip-agent

if [ ! -e "$WEB_INSTALL_DIR" ]; then
    mkdir -p "$WEB_INSTALL_DIR"
fi

cp imip_manager.py "$WEB_INSTALL_DIR"
cp htdocs/styles.css "$WEB_INSTALL_DIR"

if [ ! -e "$WEB_INSTALL_DIR/imipweb" ]; then
    mkdir "$WEB_INSTALL_DIR/imipweb"
fi

cp imipweb/*.py "$WEB_INSTALL_DIR/imipweb/"
