#!/bin/sh

# author : Nicolas Chipaux <aegiap@gandi.net> for GANDI
# all rights reserved.

# We load the default functions and configuration for gandi-config
[ -f /etc/gandi/plugins-lib ] && . /etc/gandi/plugins-lib || exit 1
load_config

if [ $CONFIG_TIMEZONE -eq 0 ]; then
    exit 0
fi

params=$(egrep -o 'gandi=[^ ]*' /proc/cmdline | sed -e 's/gandi=//' -e 's/\-/ /g')

if [ "o" = "o$params" ]; then
    tzvalue="Europe/Paris"
else
    set "$params"
    tzone=$( echo "$1" | tr "A-Z" "a-z")
    
    if [ $(expr match "$tzone" "[a-z0-9]*") -ge 3 ]; then
        case "$1" in
            'cet') tzvalue="Europe/Paris" 
                   ;;
            *) # default to UTC
               tzvalue="Etc/UTC"
               ;;
        esac
    fi
fi

echo "$tzvalue" > /etc/timezone
if [ -f "/usr/share/zoneinfo/$tzvalue" ]; then
    if [ -L /etc/localtime ]; then
        ln -sf "/usr/share/zoneinfo/$tzvalue" /etc/localtime
    else
        cp -f "/usr/share/zoneinfo/$tzvalue" /etc/localtime
    fi
fi
exit 0
