#!/bin/bash

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

#### meta start
#### project Whonix
#### category networking
#### description
## Undocumented.
#### meta end

#set -x
set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

source /usr/libexec/helper-scripts/strings.bsh

SCRIPTNAME="$(basename "$BASH_SOURCE")"

usage() {
   printf '%s\n' "$SCRIPTNAME

Usage: $SCRIPTNAME [-h help] [-d day] [-m month] [-y year] [-i increment in seconds (0-60)] [-r random increment in seconds (0-60)] [-f history folder]
Example: $SCRIPTNAME -d 30 -m 12 -y 2013 -i 10 -f /tmp/$SCRIPTNAMEtest
         sudo $SCRIPTNAME -d 30 -m 12 -y 2013 -r -f /tmp/$SCRIPTNAMEtest"
}

_randomincrement="none"
_increment="none"

while [ -n "${1-}" ]; do
  case "${1-}" in
      -h)
          usage
          exit 0
          ;;
      -d)
          _day="$2"
          shift
          ;;
      -m)
          _month="$2"
          shift
          ;;
      -y)
          _year="$2"
          shift
          ;;
      -i)
          _increment="$2"
          shift
          ;;
      -r)
          _randomincrement="$2"
          shift
          ;;
      -f)
          TIMEDIR="$2"
          shift
          ;;
      *)
          command="$(command -v "${1-}")"
          ## From now on, the complete to-be-wrapped command and its args
          ## are stored in $@, which expands as intended for
          ## handling quoted arguments with whitespace, etc.
          break
  esac
  shift
done

if [ -z "${_day-}" ]; then
   _day="$(date +"%d")"
fi

if [ -z "${_month-}" ]; then
   _month="$(date +"%m")"
fi

if [ -z "${_year-}" ]; then
   _year="$(date +"%Y")"
fi

if [ "${_randomincrement}" = "none" ] && [ "${_increment}" = "none" ]; then
   _increment="1"
fi

if [ "$_randomincrement" = "none" ]; then
   if [ -z "$_increment" ]; then
      _increment="1"
   fi
elif [ "$_increment" = "none" ]; then
   if ! is_whole_number "$_randomincrement"; then
      printf '%s\n' "randomincrement must be a positive number."
      exit 1
   else
      random_number="$(/usr/libexec/helper-scripts/random-between 1 "$_randomincrement")"
      _increment="$random_number"
   fi
else
   printf '%s\n' "You can not combine -r and -i."
   exit 1
fi

if [ -z "${TIMEDIR-}" ]; then
   TIMEDIR=~/.timeprivacy
fi

if ! is_whole_number "$_increment"; then
   printf '%s\n' "increment is not a digit."
   exit 1
fi

if ! is_whole_number "$_year"; then
   printf '%s\n' "year is not a digit."
   exit 1
fi

if ! is_whole_number "$_month"; then
   printf '%s\n' "month is not a digit."
   exit 1
fi

if ! is_whole_number "$_day"; then
   printf '%s\n' "day is not a digit."
   exit 1
fi

SECONDS_FILE="$TIMEDIR/seconds_file"
MINUTES_FILE="$TIMEDIR/minutes_file"
HOURS_FILE="$TIMEDIR/hours_file"

#DAYS_FILE="$TIMEDIR/days_file"
#MONTHS_FILE="$TIMEDIR/months_file"
#YEARS_FILE="$TIMEDIR/years_file"

#true "TIMEDIR: $TIMEDIR"
#true "Year: $_year"
#true "Month: $_month"
#true "Day: $_day"
#true "Random increment: $_randomincrement"
#true "Increment: $_increment"

read_date_file() {
   if [ ! -d "$TIMEDIR" ]; then
      mkdir -p -- "$TIMEDIR"
   fi

   if [ ! -f "$SECONDS_FILE" ]; then
      printf '%s\n' "0" > "$SECONDS_FILE"
   fi

   if [ ! -f "$MINUTES_FILE" ]; then
      printf '%s\n' "0" > "$MINUTES_FILE"
   fi

   if [ ! -f "$HOURS_FILE" ]; then
      printf '%s\n' "0" > "$HOURS_FILE"
   fi

   #if [ ! -f "$DAYS_FILE" ]; then
      #printf '%s\n' "1" > "$DAYS_FILE"
   #fi

   #if [ ! -f "$MONTHS_FILE" ]; then
      #printf '%s\n' "1" > "$MONTHS_FILE"
   #fi

   #if [ ! -f "$YEARS_FILE" ]; then
      #printf '%s\n' "2013" > "$YEARS_FILE"
   #fi

   SECONDS="$(stcat "$SECONDS_FILE")"
   MINUTES="$(stcat "$MINUTES_FILE")"
   HOURS="$(stcat "$HOURS_FILE")"

   if [ -z "$SECONDS" ]; then
      SECONDS="0"
   fi

   if [ -z "$MINUTES" ]; then
      MINUTES="0"
   fi

   if [ -z "$HOURS" ]; then
      HOURS="0"
   fi

   local nodigits="$(printf '%s\n' "$SECONDS" | sed 's/[[:digit:]]//g')"
   if [ ! -z "$nodigits" ]; then
      SECONDS="0"
   fi

   local nodigits="$(printf '%s\n' "$MINUTES" | sed 's/[[:digit:]]//g')"
   if [ ! -z "$nodigits" ]; then
      MINUTES="0"
   fi

   local nodigits="$(printf '%s\n' "$HOURS" | sed 's/[[:digit:]]//g')"
   if [ ! -z "$nodigits" ]; then
      HOURS="0"
   fi

   SECONDS="$(( $SECONDS + $_increment ))"
   if [ "$SECONDS" -ge "60" ]; then
      SECONDS="0"

      MINUTES="$(( $MINUTES + 1 ))"
      if [ "$MINUTES" -ge "60" ]; then
         MINUTES="0"

         HOURS="$(( $HOURS + 1 ))"
         if [ "$HOURS" -ge "24" ]; then
            HOURS="0"
         fi
         printf '%s\n' "$HOURS" > "$HOURS_FILE"

      fi
      printf '%s\n' "$MINUTES" > "$MINUTES_FILE"

   fi

   printf '%s\n' "$SECONDS" > "$SECONDS_FILE"

   #printf '%s\n' "$HOURS $MINUTES $SECONDS"
}

need_new_date() {
   ## Testing
   #while [ 1 ]; do
   #   read_date_file
   #done

   read_date_file

   ## Testing
   #printf '%s\n' "faketime '$_year-$_month-$_day $HOURS:$MINUTES:$SECONDS' /bin/date"
   #faketime "$_year-$_month-$_day $HOURS:$MINUTES:$SECONDS" /bin/date

   printf '%s\n' "$_year-$_month-$_day $HOURS:$MINUTES:$SECONDS"
}

need_new_date
