#!/bin/bash
#
# Reflect /etc/timezone into the container at
# /var/lib/machines/${1}/etc/host-timezone.
#
# Takes a single parameter, the name of the machine to reflect from
# ("debian-buster-64", for example).
#
# Changes will be picked up by a matching reflect-timezone.{path,service}
# pair in the guest, which will then force a reconfigure.
# The net effect of this should be that if you change timezone, it
# will auto-magically carry over into the guest as well.
#
# No equivalent propagation of changes from guest to host is provided.
#
# Intended to be triggered via a path unit watching the
# /etc/timezone file for changes (on the host).
#
# AUTHOR
# ------
#
# Copyright (c) 2019-20 sakaki <sakaki@deciban.com>
#
# License (GPL v3.0)
# ------------------
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#

set -e
set -u
shopt -s nullglob

DS64_NAME="${1:-debian-buster-64}"
DS64_DIR="/var/lib/machines/${DS64_NAME}"

HOST_PREFIX="/etc"
CONTAINER="${DS64_DIR}"
GUEST_PREFIX="${CONTAINER}/etc"
TMFILE="timezone"
HOST_TIMEZONE="${HOST_PREFIX}/${TMFILE}"
GUEST_TIMEZONE="${GUEST_PREFIX}/host-${TMFILE}"

SCRIPT_NAME="$(basename -- "${0}")"

if pidof -o %PPID -x "${SCRIPT_NAME}" &>/dev/null; then
    # already running
    exit 0
fi

# bail out if no container present at all
if ! [[ -d "${CONTAINER}" ]]; then
    exit 1
fi

# copy across the new localtime data into the container
cp -ax "${HOST_TIMEZONE}" "${GUEST_TIMEZONE}"

exit 0
