#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## test:
## printf '%s\n' "Fri Sep  30 22:20:00 UTC 2016" | /usr/bin/clock-random-manual-cli

set -e
set -o nounset
set -o pipefail
set -o errtrace

## provides: get_random_time
## provides: error_handler
# shellcheck source=./usr/bin/bootclockrandomization
source /usr/bin/bootclockrandomization

trap "error_handler" ERR

delay_plus_or_minus="30"

if [ $# -eq 0 ]; then
  printf '%s\n' "Enter current date. Example:
  $(LC_ALL=C date)"
  read -r date_input_user
else
  date_input_user="$@"
fi

## Example date_input:
## Fri Sep  30 22:20:00 UTC 2016

printf '%s\n' "date_input_user: '$date_input_user'"

OLD_TIME_HUMAN="$date_input_user"

get_random_time

printf '%s\n' "output: '$NEW_TIME_HUMAN' '$NANOSECONDS'"

printf '%s\n' "EXECUTING: LC_ALL=C date --set '@$NEW_TIME_UNIXTIME.$NANOSECONDS'"

## Set new time. Syntax: LC_ALL=C date --set @1396733199.112834496
LC_ALL=C date --set "@$NEW_TIME_UNIXTIME.$NANOSECONDS" #> /dev/null

printf '%s\n' "Success, clock is now randomized. Syncing hardware clock..."

## Sync hardware clock with system clock.
if hwclock --systohc 2>/dev/null; then
  printf '%s\n' "Done."
else
  printf '%s\n' "Hardware clock unavailable. This is normal inside VMs."
fi
