require 'fileutils'

class ListVersions_git
  
  
  def self.showVersions(launchpadType, layer, name)

    # Check the payload directory exists
    env_TTC_DEPLOY = ENV['TTC_DEPLOY']
    payloadDir = "#{env_TTC_DEPLOY}/payloads/#{layer}-projects/#{name}"
    #print "payloadDir=>#{payloadDir}\n"
    if not File.exist?(payloadDir)
      print "<error>Unknown payload directory #{payloadDir}</error>\n"
      exit(false)
    end
    # Take a look at the repository
    repoDir = "#{payloadDir}/git-repository"
    if File.exist?(repoDir)
      
      # Get a list of versions from git
      Dir.chdir(repoDir) do
        print "<versions>\n"

        # get branches
        branches = `git branch`
        branches.each do |line|
          #line = line[0..0]
          line = line.chomp
          line = line[2..-1]
          print "  <branch>#{line}</branch>\n"
        end
        
        # Get tags
        tags = `git tag`
        tags.each do |line|
          line = line.chomp
          print "  <tag>#{line}</tag>\n"
        end
        
      $fileDate = 0
       if File.file?("#{repoDir}/FETCH_HEAD")
        $fileDate = File::mtime( "#{repoDir}/FETCH_HEAD")
       else
        if File.file?("#{repoDir}/index")
          $fileDate = File::mtime( "#{repoDir}/index")
        end
       end
       if $fileDate != 0
         $fileDateDiff = Time.now - $fileDate #date diff in seconds
         $DiffDate = ($fileDateDiff / 60) #convert into minutes
             if $DiffDate > 60
               $DiffDate = (($DiffDate) / (60)) #convert into hours
               if $DiffDate > 23
                $DiffDate = ($DiffDate / 24) #convert into days
                if $DiffDate.to_i == 1
                  $Diffsuf = " day ago"
                  $DiffDate = $DiffDate.to_i
                elsif $DiffDate.to_i > 15
                  $DiffDate = $fileDate.strftime("%m/%d/%y")
                  $Diffsuf = ""
                else
                  $Diffsuf = " days ago"
                  $DiffDate = $DiffDate.to_i
                end
               else
                if $DiffDate.to_i == 1
                  $Diffsuf = " hour ago"
                else
                  $Diffsuf = " hours ago"
                end
                $DiffDate = $DiffDate.to_i
               end
             else
                if $DiffDate < 1
                  $Diffsuf = "a few seconds ago"
                  $DiffDate = ""
                elsif $DiffDate.to_i == 1
                  $Diffsuf = " minute ago"
                  $DiffDate = $DiffDate.to_i
                else
                  $Diffsuf = " minutes ago"
                  $DiffDate = $DiffDate.to_i
                end 
            end
            print "  <lastUpdated>Synchronized #{$DiffDate}#{$Diffsuf}</lastUpdated>\n"
        else
            print "  <lastUpdated>Never synced</lastUpdated>\n"
        end
        
        # Work out when the repository was last updated
        print "</versions>\n"
      end
    else
      # There's no repository
      #print "<error>Repository has not been cached - please sync.</error>\n"
      print "<versions>\n"
      print "<lastUpdated>Never syncronized.</lastUpdated>\n"
      print "</versions>\n"
    end
    # Normal exit
    exit(false)

  end
end
