=begin
 ******************************************************************************
 * Copyright 1990-2014 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Routine to set the system date and time
 * Filename : set_date_time.rb
 * Author   : Henthorn
 * Project  : Benthic Rover/SES
 * Version  : 1
 * Created  : July 2014
 * Modified :
 ******************************************************************************
=end

puts("***************************************")
puts("*                                     *")
puts("*     MBARI/Pelagic-Benthic/barbo     *")
puts("*   Set system date and time routine  *")
puts("*                                     *")
puts("*   Enter UTC time, not local time.   *")
puts("*   Enter 'q' at any prompt to quit.  *")
puts("*                                     *")
puts("**************************************")

##
# Return a value from STDIO
# Use default value, if any.
# Prompt with the provided prompt and default, if any.
# 
def get_value(prompt, default)
  # Prompt user if supplied
  #
  print(prompt) if (prompt)
  print(" (#{default})") if (default)
  print(": ")

  # Get the user response, if any
  #
  val = gets.strip
  val = default.to_s if (val == "")
  return val
end

# Get the current data and time
#
now = Time.now()

# Prompt for date/time components using the current time for defaults.
#
year  = get_value("UTC Year", now.year)
exit if (year.downcase == 'q')
month = get_value("UTC Month", now.month)
exit if (month.downcase == 'q')
day   = get_value("UTC Day", now.day)
exit if (day.downcase == 'q')

hour  = get_value("UTC Hour", now.hour)
exit if (hour.downcase == 'q')
min   = get_value("UTC Minute", now.min)
exit if (min.downcase == 'q')
sec   = get_value("UTC Seconds", now.sec)
exit if (sec.downcase == 'q')

# Build command line to set date and time
#
dt = "date  -s \"#{year}.#{month}.#{day}-#{hour}:#{min}:#{sec}\""

# Ensure user wants to proceed using the supplied date and time
#
puts("\n")
puts(dt)
proceed = get_value("Set date and time using values above?", "y")

if (proceed.downcase == 'y')
  `#{dt}`
  print("Date/Time is "); puts(`date`)
end
