#!/bin/sh   
# 
# Updates the adjtime file
#
# This should only be executed after sufficient hwclock systematic drift data 
# to be collected. 
#

# Primary location for file
F=/etc/adjtime

# Check if this is a symlink - required for read-only root file systems
if [ -h "$F" ]; then
    # Follow the symlink chain to determine the file location
    # Note: This will work regardless of whether the file is present or missing.
    S=$(readlink "$F")
    until [ "x$S" == "x" ] ; do
        F=$S
        S=$(readlink "$S")
    done
fi

# Create temporary file in the destination folder and write the data. Move 
# atomically to reduce likelihood of data corruption.
P=$(dirname "$F")
T=$(mktemp --tmpdir="$P")
hwclock -u -w --adjfile="$T"
mv "$T" "$F"
