#!/bin/sh
WVIEW_CONF_DIR=/etc/wview
WVIEW_DATA_DIR=/var/wview
WVIEWCONFIG=/opt/wview/bin/wviewconfig
###############################################################################
#
# File:          wviewhtmlconfig
#
# Description:   configure wview web site template
#
# Usage:         wviewhtmlconfig [skin_name|remove]
#
# History:
# Engineer    Date	 Ver  Comments
# MS Teel     02/05/08   1    Original
# MS Teel     09/13/08   2    Modify for SQLite configuration DB
# MS Teel     05/03/09   3    Add non-interactive support
# mwall       19.11.11   4    refactor code; parameterize filenames; use a
#                             separate skins directory; use tmpdir; improve
#                             logic for prompting for skin; parameterize the
#                             default skin name; support either old-style or
#                             new-style skin configurations; provide feedback
#                             about what is happening; make snapshot of web
#                             site as well as template configuration.
#
# Operation:
# Each web site template, or skin, is a self-contained directory.  Skins are
# stored in the skins directory, e.g. /etc/wview/skins.
#
# When a skin is installed, parts are copied from the skin directory into the
# appropriate operating directories.  The base operating parts and locations
# include:
#
#   configuration files:
#     /etc/wview/html-templates.conf
#     /etc/wview/images.conf
#     /etc/wview/images-user.conf
#     /etc/wview/graphics.conf
#   script files:
#     /etc/wview/pre-generate.sh
#     /etc/wview/post-generate.sh
#   template files:
#     /etc/wview/html/index.htx
#   static files:
#     /var/wview/img/wview.css
#     /var/wview/img/wview.js
#
# There are settings in the database that affect skin installation:
#
#   HTMLGEN_IMAGE_PATH     path to the directory that contains generated files
#   HTMLGEN_EXTENDED_DATA  generated extended data?
#   HTMLGEN_METRIC_UNITS   use metric units?
#
# There are two supported skin formats: v1 and v2.  The v1 format has the
# following structure:
#
#   skinname/
#   skinname/plus/
#   skinname/standard/
#   skinname/static/
#
# The v2 format has the following structure:
#
#   skinname/
#   skinname/script/
#   skinname/conf/
#   skinname/static/
#   skinname/template/
#
###############################################################################

TIMESTAMP=`date '+%Y%m%d.%H%M%S'`
CONF_FILES="html-templates.conf images.conf images-user.conf graphics.conf"
SCRIPT_FILES="pre-generate.sh post-generate.sh"
TMP_DIR=/tmp
TMPL_DIR=$WVIEW_CONF_DIR/html
SKINS_DIR=$WVIEW_CONF_DIR/skins
DEFAULT_SKIN="chrome"
OLDTMPL_DIR=$WVIEW_CONF_DIR/site-template-$TIMESTAMP
OLDSITE_DIR=$WVIEW_CONF_DIR/site-$TIMESTAMP

get_wviewconfig()
{
    echo "select value from config where name='HTMLGEN_EXTENDED_DATA';" > $TMP_DIR/cmnd.sql
    echo ".read $TMP_DIR/cmnd.sql" | sqlite3 $WVIEW_CONF_DIR/wview-conf.sdb > $TMP_DIR/value.out
    DATA_EXTENDED=`cat $TMP_DIR/value.out`

    echo "select value from config where name='HTMLGEN_METRIC_UNITS';" > $TMP_DIR/cmnd.sql
    echo ".read $TMP_DIR/cmnd.sql" | sqlite3 $WVIEW_CONF_DIR/wview-conf.sdb > $TMP_DIR/value.out
    STATION_METRIC=`cat $TMP_DIR/value.out`

    echo "select value from config where name='HTMLGEN_IMAGE_PATH';" > $TMP_DIR/cmnd.sql
    echo ".read $TMP_DIR/cmnd.sql" | sqlite3 $WVIEW_CONF_DIR/wview-conf.sdb > $TMP_DIR/value.out
    HTML_DESTINATION=`cat $TMP_DIR/value.out`

    rm -f $TMP_DIR/cmnd.sql
    rm -f $TMP_DIR/value.out
}

display_intro()
{
    echo "################################################################################"
    echo " Web Site Template Configuration for wview"
    echo ""
    echo " This script will prompt for a skin name, then it will save"
    echo " the existing configuration to"
    echo "   $OLDTMPL_DIR"
    echo " and the existing site to"
    echo "   $OLDSITE_DIR"
    echo " before generating the new configuration."
    echo ""
    echo " If this is not what you want to do, hit CTRL-C to abort!"
    echo "################################################################################"
}

save_old()
{
    echo "saving existing template configuration to"
    echo "  $OLDTMPL_DIR"
    mkdir $OLDTMPL_DIR
    if [ ! -d $OLDTMPL_DIR ]; then
        echo "FAIL: cannot create archive directory"
        exit 1;
    fi
    echo "  copying html directory..."
    cp -prf $TMPL_DIR $OLDTMPL_DIR
    echo "  copying config files and scripts..."
    for f in $CONF_FILES $SCRIPT_FILES; do
        if [ -f $WVIEW_CONF_DIR/$f ]; then
            cp -p $WVIEW_CONF_DIR/$f $OLDTMPL_DIR
        fi
    done
    echo "saving existing web site to"
    echo "  $OLDSITE_DIR"
    mkdir $OLDSITE_DIR
    if [ ! -d $OLDSITE_DIR ]; then
        echo "FAIL: cannot create site archive directory"
        exit 1;
    fi
    echo "  copying contents"
    cp -prf $HTML_DESTINATION $OLDSITE_DIR
}

remove_all()
{
    # remove all of the configuration and script files
    for f in $CONF_FILES $SCRIPT_FILES; do
        rm -f $VIEW_CONF_DIR/$f
    done
    # do not simply remove everything in the html directory as older
    # installations keep skins there.
    rm -f $TMPL_DIR/*.*
}

show_skins()
{
    for d in `ls $SKINS_DIR`; do
        if [ -d $SKINS_DIR/$d ]; then
            echo "  $d"
        fi
    done
}

get_user_preferences()
{
    ok=0
    while [ "$ok" = "0" ]; do
        echo ""
        echo "Which skin would you like to use?"
        show_skins
        echo -n "[$SITE_SKIN]: "
        read INVAL
        if [ "" != "$INVAL" ]; then
            SITE_SKIN=$INVAL
        fi
        if [ -d "$SKINS_DIR/$SITE_SKIN" ]; then
            ok=1
        else
            echo ""
            echo "There is no skin called '$SITE_SKIN'"
            SITE_SKIN=$DEFAULT_SKIN
        fi
    done

    echo ""
    echo "using the $SITE_SKIN skin"
    if [ "yes" = "$DATA_EXTENDED" ]; then
        echo "  with extended data graphics"
    else
        echo "  with NO extended data graphics"
    fi
    if [ "yes" = "$STATION_METRIC" ]; then
        echo "  with metric units"
    else
        echo "  with US/Imperial units"
    fi
}

# see if this is a new-style skin or old-style skin, then copy files.
configure_skin()
{
    SKINDIR=$SKINS_DIR/$SITE_SKIN

    echo "configuring the skin $SITE_SKIN"

    # remove anything that was there before.  delete templates.  conf files
    # and scripts will be overwritten.
    echo "  removing existing templates"
    rm -f $TMPL_DIR/*.*

    if [ -d $SKINDIR/template -a -d $SKINDIR/conf ]; then
        echo "  copying templates"
        cp -rf $SKINDIR/template/* $TMPL_DIR
        if [ -d $SKINDIR/static ]; then
            echo "  copying static files"
            cp -rf $SKINDIR/static/* $HTML_DESTINATION
        fi
        echo "  copying config files"
        for f in $CONF_FILES; do
            if [ -f $SKINDIR/conf/$f ]; then
                cp -f $SKINDIR/conf/$f $WVIEW_CONF_DIR
            fi
        done
        echo "  copying scripts"
        if [ -d $SKINDIR/scripts ]; then
            for f in `ls $SKINDIR/scripts`; do
                cp -f $SKINDIR/scripts/$f $WVIEW_CONF_DIR
                chmod +x $WVIEW_CONF_DIR/$f
            done
        fi
        if [ "yes" = "$STATION_METRIC" ]; then
            if [ -f $SKINDIR/conf/images-metric.conf ]; then
                echo "  using metric images"
                mv $SKINDIR/conf/images-metric.conf $WVIEW_CONF_DIR/images.conf
            else
                echo "WARNING: metric requested but skin has no images-metric.conf"
            fi
            if [ -f $TMPL_DIR/awekas_wl.htx-metric ]; then
                echo "  using metric awekas"
                cp -f $TMPL_DIR/awekas_wl.htx-metric $TMPL_DIR/awekas_wl.htx
            fi
        else
            if [ -f $TMPL_DIR/awekas_wl.htx-us ]; then
                echo "  using US awekas"
                cp -f $TMPL_DIR/awekas_wl.htx-us $TMPL_DIR/awekas_wl.htx
            fi
        fi
    else
        # copy everything from the skin source directory to the destinations
        if [ "yes" = "$DATA_EXTENDED" ]; then
            echo "  copying extended templates"
            cp -rf $SKINDIR/plus/* $TMPL_DIR
        else
            echo "  copying standard templates"
            cp -rf $SKINDIR/standard/* $TMPL_DIR
        fi
        if [ -d $SKINDIR/static ]; then
            echo "  copying static files"
            cp -rf $SKINDIR/static/* $HTML_DESTINATION
        fi

        # now move the configuration files into the conf directory
        echo "  copying config files"
        mv $TMPL_DIR/html-templates.conf $WVIEW_CONF_DIR
        mv $TMPL_DIR/images-user.conf $WVIEW_CONF_DIR
        mv $TMPL_DIR/graphics.conf $WVIEW_CONF_DIR
        mv $TMPL_DIR/images.conf $WVIEW_CONF_DIR/images.conf
        echo "  copying scripts"
        if [ -f $TMPL_DIR/pre-generate.sh ]; then
            mv $TMPL_DIR/pre-generate.sh $WVIEW_CONF_DIR
            chmod +x $WVIEW_CONF_DIR/pre-generate.sh
        fi
        if [ -f $TMPL_DIR/post-generate.sh ]; then
            mv $TMPL_DIR/post-generate.sh $WVIEW_CONF_DIR
            chmod +x $WVIEW_CONF_DIR/post-generate.sh
        fi
        if [ "yes" = "$STATION_METRIC" ]; then
            if [ -f $TMPL_DIR/images-metric.conf ]; then
                echo "  using metric images"
                mv -f $TMPL_DIR/images-metric.conf $WVIEW_CONF_DIR/images.conf
            else
                echo "WARNING: metric requested but skin has no images-metric.conf"
            fi
            if [ -f $TMPL_DIR/awekas_wl.htx-metric ]; then
                echo "  using metric awekas"
                cp -f $TMPL_DIR/awekas_wl.htx-metric $TMPL_DIR/awekas_wl.htx
            fi
        else
            if [ -f $TMPL_DIR/awekas_wl.htx-us ]; then
                echo "  using US awekas"
                cp -f $TMPL_DIR/awekas_wl.htx-us $TMPL_DIR/awekas_wl.htx
            fi
        fi
    fi
}




export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin

if [ ! -d $SKINS_DIR ]; then
    echo "no skins found at $SKINS_DIR"
    exit 1
fi

get_wviewconfig

# If a removal or valid skin is requested, then just do it without any
# interaction.  Otherwise, prompt for user input.
if [ "" != "$1" ]; then
    if [ "$1" = "remove" ]; then
        REMOVE_IT=1
        INTERACTIVE=0
    elif [ -d $SKINS_DIR/$1 ]; then
        SITE_SKIN=$1
        INTERACTIVE=0
    else
        SITE_SKIN=$1
        INTERACTIVE=1
    fi
else
    SITE_SKIN=$DEFAULT_SKIN
    INTERACTIVE=1
fi

if [ "$REMOVE_IT" = "1" ]; then
    remove_all
    echo "web site configuration and template files have been removed"
    exit 0
fi

if [ "$INTERACTIVE" = "1" ]; then
    display_intro
    get_user_preferences
    save_old
fi

configure_skin

echo "web site setup complete."
echo "  configuration files are in $WVIEW_CONF_DIR"
echo "  template files are in $TMPL_DIR"
echo "  web site files are in $HTML_DESTINATION"
