#!/bin/bash -x

SOURCES_LIST=/etc/apt/sources.list
ARCHIVE_URL="http://archive.zentyal.org/zentyal"
PKG_DIR=/var/tmp/zentyal-packages
LOCAL_SOURCES="deb file:$PKG_DIR ./"

create_repository()
{
    # without this apt-ftparchive gives lots of "too many open files" errors
    # for some reason this worked flawlessly in lucid but not in more recent versions
    ulimit -n 30000

    if ! [ -d $PKG_DIR ]; then
        # temporal package directory does not exists, cannot create local repository
        return
    fi
    pushd $PKG_DIR
    apt-ftparchive packages . | gzip > Packages.gz
    popd

    # Link .deb files to cache to avoid downloading them
    pushd /var/cache/apt/archives
    for i in $PKG_DIR/*.deb
    do
        ln -s $i .
    done
    popd

    # Comment source lines by default to make apt-get update faster
    sed -i 's/^deb-src/#deb-src/g' ${SOURCES_LIST}

    # Update the package database with only the local repository
    # just in case we are installing without internet connection
    mv ${SOURCES_LIST} /tmp/sources.list.orig
    echo ${LOCAL_SOURCES} > ${SOURCES_LIST}
    apt-get update
    # Restore the original sources.list
    mv /tmp/sources.list.orig ${SOURCES_LIST}

    echo ${LOCAL_SOURCES} >> ${SOURCES_LIST} # add local sources
}

update_if_network()
{
    # Check if we can connect to the archive url
    if $(wget -T 10 -t 1 $ARCHIVE_URL); then
        echo "Updating package database from the network..."
        apt-get update
    else
        echo "Warning: Can't connect to $ARCHIVE_URL. Updates won't be installed."
    fi
}

gen_locales()
{
    # load LANG variable with default locale
    . /etc/default/locale

    # Append Zentyal support languages to generate to current supported
    # locales
    LOCALES_FILE=/var/lib/locales/supported.d/local
    TMP=/tmp/local.tmp
    cat /tmp/zentyal/locale.gen $LOCALES_FILE > $TMP
    sort $TMP | uniq > $LOCALES_FILE
    rm -f $TMP

    # Install language-pack-$LANG if exists
    suffix=`echo $LANG | cut -d\. -f1 | tr '_' '-' | tr '[A-Z]' '[a-z]'`
    apt-get install -y --force-yes language-pack-zentyal-$suffix
    if [ $? -ne 0 ]
    then
        # Try with xx if xx-yy not exists
        suffix=`echo $suffix | cut -d- -f1`
        apt-get install -y --force-yes language-pack-zentyal-$suffix
    fi

    # Regenerate locales to update the new messages from Zentyal
    /usr/sbin/locale-gen
}

create_repository # Set up local package repository

update_if_network # apt-get update if we are connected to the internet

DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y --force-yes zentyal

gen_locales

if [ -f /var/tmp/.zentyal-disaster-recovery ]
then
    touch /var/lib/zentyal/.disaster-recovery
fi

if [ -f /var/lib/zinstaller-remote/REGISTER_USERNAME ]
then
    plymouth message --text="Registering your server... Please wait."
    /usr/share/zenbuntu-core/remote-register
fi

if [ -d /usr/share/zentyal-custom/scripts ]
then
    plymouth message --text="Running custom installer... Please wait."
    run-parts /usr/share/zentyal-custom/scripts
fi

sync

exit 0
