#!/bin/sh

DIRNAME=`dirname "$0"`

if [ -e "$DIRNAME/config.sh" ]; then
    . "$DIRNAME/config.sh"
else
    . /etc/imip-agent/config.sh
fi

if [ "$1" = "--no-locale-dir" ]; then
    NO_LOCALE_DIR=$1
else
    NO_LOCALE_DIR=
fi

# Agents and modules.

AGENTS="imip_person.py imip_person_outgoing.py imip_resource.py"
MODULES="markup.py vCalendar.py vContent.py vRecurrence.py"

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

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

for DIR in "$INSTALL_DIR/imiptools" \
           "$INSTALL_DIR/imiptools/stores" \
           "$INSTALL_DIR/imiptools/stores/database" \
           "$INSTALL_DIR/imiptools/handlers" \
           "$INSTALL_DIR/imiptools/handlers/scheduling" ; do
    if [ ! -e "$DIR" ]; then
        mkdir "$DIR"
    fi
done

# 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/stores/*.py "$INSTALL_DIR/imiptools/stores/"
cp imiptools/stores/database/*.py "$INSTALL_DIR/imiptools/stores/database/"
cp imiptools/handlers/*.py "$INSTALL_DIR/imiptools/handlers/"
cp imiptools/handlers/scheduling/*.py "$INSTALL_DIR/imiptools/handlers/scheduling/"

# Remove migrated modules.

if [ -e "$INSTALL_DIR/imiptools/handlers/scheduling.py" ]; then
    rm "$INSTALL_DIR/imiptools/handlers/scheduling.py"*
fi

if [ -e "$INSTALL_DIR/imip_store.py" ]; then
    rm "$INSTALL_DIR/imip_store.py"*
fi

# Install the config module in a more appropriate location.
# Create new versions of configuration files instead of overwriting.

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

if [ -e "$CONFIG_DIR/config.py" ]; then
    mv "$INSTALL_DIR/imiptools/config.py" "$CONFIG_DIR/config.py.new"
else
    mv "$INSTALL_DIR/imiptools/config.py" "$CONFIG_DIR/config.py"
fi

if [ -e "$CONFIG_DIR/config.sh" ]; then
    cp "tools/config.sh" "$CONFIG_DIR/config.sh.new"
else
    cp "tools/config.sh" "$CONFIG_DIR/config.sh"
fi

# Replace the config module with a symbolic link.

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

# Copy related configuration files.

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

if [ -e "$CONFIG_DIR/postgresql/schema.sql" ]; then
    cp "conf/postgresql/schema.sql" "$CONFIG_DIR/postgresql/schema.sql.new"
else
    cp "conf/postgresql/schema.sql" "$CONFIG_DIR/postgresql/schema.sql"
fi

# Tools

TOOLS="fix.sh init.sh init_user.sh make_freebusy.py set_quota_limit.py update_quotas.py update_scheduling_modules.py"

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

for TOOL in $TOOLS; do
    cp "tools/$TOOL" "$INSTALL_DIR/tools/"
done

# Web manager interface.

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/"

# Locale directory.

if [ ! "$NO_LOCALE_DIR" ] && [ -e "locale" ]; then
    for DIR in "locale/"*"/LC_MESSAGES" ; do
        mkdir -p "$INSTALL_DIR/$DIR"
        cp "$DIR/"*.mo "$INSTALL_DIR/$DIR/"
    done
fi

# Run the scheduling module update tool to regenerate the manifest module.

PYTHONPATH="$INSTALL_DIR" "$INSTALL_DIR/tools/update_scheduling_modules.py"
