# This script compare ctime of the first and the last
# file in a stamp directory to callculate time duration

function time_duration(){
  tmp_location="/var/tmp/gen2/tmp"
  first_file=$(ls -t ${tmp_location}/* | tail -1)
  last_file=$(ls -rt ${tmp_location}/* | tail -1)

  time_start=$(stat --format="%Y" $first_file)
  time_stop=$(stat --format="%Y" $last_file)
  time_diff=$(($time_stop - $time_start))

  echo "+----------------------------------------------+"
  echo "| Time summary                                 |"
  echo "+----------------------------------------------+"
#  echo "| Start of installation: " $(date -d @${time_start} +"%Y-%m-%d %H:%M:%S") " |"
#  echo "| End of installation: " $(date -d @${time_stop} +"%Y-%m-%d %H:%M:%S") "   |"
  printf "| Start of installation:  %s %s  |\n" $(date -d @${time_start} +"%Y-%m-%d %H:%M:%S")
  printf "| End of installation:  %s %s    |\n" $(date -d @${time_stop} +"%Y-%m-%d %H:%M:%S")
  echo "+----------------------------------------------+"
#  echo "| Duration: " $(date -u -d @${time_diff} +"%T") "                         |"
  printf "| Duration:  %-32s  |\n" $(date -u -d @${time_diff} +"%T")
  echo "+----------------------------------------------+"

}

time_duration
