#!/bin/sh

################################################################################
#
# Copyright (C) 2014,2015 Cumulus Networks Inc. All rights reserved
#
################################################################################

# This script installs platform specific config files from
# /usr/share/cumulus-platform/<vendor>/<model> into /etc on the target
# file system.
#
# This script uses the facilities from ucf(1) to manage local
# configuration file changes made by the user.
#
# This script is called in the following contexts:
#
# - initramfs: during the first boot after the current platform type
#   is determined.  ucf(1) uses debconf so we have to be careful about
#   which debconf to use when we have no TTY.
#
# - dpkg upgrades: when installing a new version of the cl-platform-config package.

initramfs="${DPKG_RUNNING_VERSION:-yes}"
if [ "$initramfs" = "yes" ] ; then
    # The legacy initramfs calls this script every boot.  Exit if
    # called before.
    [ -e /var/cache/cumulus/platform-config ] && exit 0

    # We have no controlling TTY in initramfs context
    export DEBIAN_FRONTEND=noninteractive
fi

# Use ucf and ucfr to install and register the configuration file with
# the system.
# $1 - source file to install
# $2 - destination file
install_file()
{
    UCF_CMD="ucf --debconf-ok --three-way"
    [ "$quiet" = "y" ] && REDIR="> /dev/null 2>&1"

    eval $UCF_CMD "$1" "$2" $REDIR

    ucfr cumulus-platform "$2"
}

commondir="/usr/share/cumulus-platform/common/etc"
[ -d "$commondir" ] || {
    echo "Error: Unable to find common platform configuration directory: $commondir"
    exit 1
}

# Determine running platform
platform="$(platform-detect)"
vendor=${platform%%,*}
model=${platform#*,}

platformdir="/usr/share/cumulus-platform/${vendor}/${model}"

installdir="/etc"

# Create a confdir using the common files as base, overlaying the
# platform specific files on top.

# clean up for temporary directory
tmp_cleanup()
{
    rm -rf $tmpdir
}

#---------------  Begin default acl files copy  -----------

common_acl_dir="/usr/share/cumulus-platform/common/etc/cumulus/acl/policy.d"

#setup the default acl files
def_acl_dir="/usr/share/cumulus/acl/default"
mkdir -p "$def_acl_dir"
cp -a $common_acl_dir/* $def_acl_dir

platform_confdir="${platformdir}/etc"

#override the common default with platform default if the platform has one
platform_acl_dir=$platform_confdir/cumulus/acl/policy.d
[ -d "$platform_acl_dir" ] && cp -a $platform_acl_dir/* $def_acl_dir

#---------------  End default acl files copy -----------

tmpdir=$(mktemp -d) || {
    echo "Error: Unable to create temp directory."
    exit 1
}

trap tmp_cleanup EXIT QUIT

cp -a $commondir $tmpdir

if [ -d "$platform_confdir" ]; then
    cp -a $platform_confdir $tmpdir
    confdir="$tmpdir/etc"

    for f in $(find $confdir -printf "%P\n") ; do
        if [ -d "$confdir/$f" ] ; then
            [ -d "$installdir/$f" ] || mkdir -p "$installdir/$f"
        elif [ -f "$confdir/$f" ] ; then
            install_file "$confdir/$f" "$installdir/$f"
        else
            echo "Error: Can't handle file $confdir/$f"
            exit 1
        fi
    done
fi

if [ "$initramfs" = "yes" ] ; then
    mkdir -p /var/cache/cumulus
    touch /var/cache/cumulus/platform-config
fi
