#!/bin/sh
# $Id$
# postinst script for weewx debian package
# Copyright 2013 Matthew Wall
#
# ways this script might be invoked:
#
# postinst configure most-recently-configured-version
# old-postinst abort-upgrade new-version
# conflictor's-postinst abort-remove in-favour package new-version
# postinst abort-remove
# deconfigured's-postinst abort-deconfigure in-favour failed-install-package
#   version [removing conflicting-package version]

# abort if any command returns error
set -e

# get debconf stuff so we can set configuration defaults
. /usr/share/debconf/confmodule

cfgfile=/etc/weewx/weewx.conf
mergeapp=/usr/share/weewx/setup.py
metric_units="\n\
        [[[Units]]]\n\
            [[[[Groups]]]]\n\
                group_altitude     = meter\n\
                group_degree_day   = degree_C_day\n\
                group_pressure     = mbar\n\
                group_radiation    = watt_per_meter_squared\n\
                group_rain         = mm\n\
                group_rainrate     = mm_per_hour\n\
                group_speed        = meter_per_second\n\
                group_speed2       = meter_per_second2\n\
                group_temperature  = degree_C\n"

# insert any configuration variables into the configuration file
configure_weewxconf() {
    db_get weewx/location
    sed -i "s%location =.*%location = \"$RET\"%" $cfgfile

    db_get weewx/latlon
    lat=$(echo $RET | cut -d, -f1)
    lon=$(echo $RET | cut -d, -f2)
    sed -i "s% latitude[ ]*=.*% latitude = $lat%" $cfgfile
    sed -i "s% longitude[ ]*=.*% longitude = $lon%" $cfgfile

    db_get weewx/altitude
    a=$(echo $RET | cut -d, -f1)
    u=$(echo $RET | cut -d, -f2)
    sed -i "s% altitude[ ]*=.*% altitude = $a, $u%" $cfgfile

    db_get weewx/units
    if [ "$RET" = "Metric" ]; then
        sed -i "/\[\[StandardReport\]\]/,/\[\[.*\]\]/ s%skin = Standard%skin = Standard\n${metric_units}%" $cfgfile
    fi

    # FIXME: generalize this so it does not have to be modified every time a
    #        new station type is added or new station options are added.
    db_get weewx/station_type
    if [ "$RET" != "" ]; then
        sed -i "s%station_type[ ]*=.*%station_type = $RET%" $cfgfile
        if [ "$RET" = "Vantage" ]; then
            db_get weewx/vantage_type
            sed -i "/\[Vantage\]/,/\[.*\]/ s% type[ ]*=.*% type = $RET%" $cfgfile
            if [ "$RET" = "serial" ]; then
                db_get weewx/vantage_port
                sed -i "/\[Vantage\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
            else
                db_get weewx/vantage_host
                sed -i "/\[Vantage\]/,/\[.*\]/ s% host[ ]*=.*% host = $RET%" $cfgfile
            fi
        fi
        if [ "$RET" = "WMR100" ]; then
            db_get weewx/wmr100_model
            sed -i "/\[WMR100\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
        fi
        if [ "$RET" = "WMR200" ]; then
            db_get weewx/wmr200_model
            sed -i "/\[WMR200\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
        fi
        if [ "$RET" = "WMR9x8" ]; then
            db_get weewx/wmr9x8_model
            sed -i "/\[WMR9x8\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
            db_get weewx/wmr9x8_port
            sed -i "/\[WMR9x8\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
        fi
        if [ "$RET" = "FineOffsetUSB" ]; then
            db_get weewx/fousb_model
            sed -i "/\[FineOffsetUSB\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
        fi
        if [ "$RET" = "WS23xx" ]; then
            db_get weewx/ws23xx_model
            sed -i "/\[WS23xx\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
            db_get weewx/ws23xx_port
            sed -i "/\[WS23xx\]/,/\[.*\]/ s%[# ]*port[ ]*=.*%    port = $RET%" $cfgfile
        fi
        if [ "$RET" = "WS28xx" ]; then
            db_get weewx/ws28xx_model
            sed -i "/\[WS28xx\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
            db_get weewx/ws28xx_frequency
            sed -i "/\[WS28xx\]/,/\[.*\]/ s%[# ]*transceiver_frequency[ ]*=.*%    transceiver_frequency = $RET%" $cfgfile
        fi
        if [ "$RET" = "TE923" ]; then
            db_get weewx/te923_model
            sed -i "/\[TE923\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
        fi
        if [ "$RET" = "Ultimeter" ]; then
            db_get weewx/ultimeter_model
            sed -i "/\[Ultimeter\]/,/\[.*\]/ s%[# ]*model[ ]*=.*%    model = $RET%" $cfgfile
            db_get weewx/ultimeter_port
            sed -i "/\[Ultimeter\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
        fi
        if [ "$RET" = "WS1" ]; then
            db_get weewx/ws1_port
            sed -i "/\[WS1\]/,/\[.*\]/ s% port[ ]*=.*% port = $RET%" $cfgfile
        fi
    fi
}

# use weewx setup utilities to merge new features into existing weewx.conf
merge_weewxconf() {
    NEWVER=`$mergeapp --version`
    OLDVER=$(get_conf_version $cfgfile)
    if dpkg --compare-versions $OLDVER lt $NEWVER; then
        # this is an old config, so merge it into a new config
        if [ -f $cfgfile.dist ]; then
            DSTVER=$(get_conf_version $cfgfile.dist)
            if dpkg --compare-versions $DSTVER eq $NEWVER; then
                echo saving previous config file as $cfgfile-$OLDVER
                mv $cfgfile $cfgfile-$OLDVER
                echo saving distribution config file as $cfgfile-$NEWVER
                cp -p $cfgfile.dist $cfgfile-$NEWVER
                echo merging previous and distribution into $cfgfile
                $mergeapp --merge-config --install-dir / --a $cfgfile-$NEWVER --b $cfgfile-$OLDVER --c $cfgfile
            else
                echo distribution config file is wrong version for merging
            fi
        else
            echo no distribution config with which to merge
        fi
    else
        # this is a new config, so just insert debconf values into it
        echo using debconf configuration values from previous install
        configure_weewxconf
    fi
}

# get the version number from the specified file, excluding the debian packaging
get_conf_version() {
    echo `grep version $1 | sed -e 's/\s*version\s*=\s*//' | sed -e 's/-.*//'`
}

case "$1" in
configure)
        if [ "$2" != "" ]; then
            # this is an upgrade so attempt to merge
            merge_weewxconf
        else
            # virgin install so insert debconf values into the config file
            configure_weewxconf
        fi

        # precompile the bytecode
        #python -m compileall /usr/share/weewx

        # configure for system startup
        update-rc.d weewx defaults 98 > /dev/null

        # start the weewx daemon
        invoke-rc.d weewx start
        ;;

abort-remove)
        # precompile the bytecode
        #python -m compileall /usr/share/weewx
        ;;
esac

# let debconf know that we are finished
db_stop

exit 0
