#!/usr/bin/bash
#
# Set the timezone of a VM.
# Copyright (c) 2016 Joyent Inc., All rights reserved.

usage() {
cat << EOF

Usage: $0 TIMEZONE

Arguments:
    
    TIMEZONE         Timezone to set your VM to.

EOF
exit 2;
}

[[ ! $1 ]] && usage;

export LANG=C
PATH="/opt/local/bin:/opt/local/gnu/bin:/opt/local/sbin:/usr/bin:/usr/sbin";
vm_time=$1;
vm_uuid=$(zonename);

# Check if timezone set exists first.
vm_times=$(find /usr/share/lib/zoneinfo -type f | awk -F/usr/share/lib/zoneinfo/ '{ print $2 }' | egrep -v "src/|.tab");
for i in ${vm_times[@]}; do
	if [[ ${vm_time} == ${i} ]]; then
		found="YES";
	fi
done

[[ ! ${found} ]] && echo "ERROR - The timezone ${vm_time} isn't available on this VM." && exit 2;

# Set the new timezone.
echo "* Setting timezone for ${vm_uuid}..";

cat /etc/default/init | grep -v "TZ=" > /etc/default/init.new
echo "TZ=${vm_time}" >> /etc/default/init.new
mv /etc/default/init.new /etc/default/init
rm /etc/TIMEZONE
ln -s /etc/default/init /etc/TIMEZONE

echo "* Changed to timezone to ${vm_time}.  You will need to reboot.";
