#!/usr/bin/env ruby

require 'fileutils'

bin = '/usr/bin/flatpak'

exit 0 unless File.executable?(bin)

def wait_time
  @wait_times ||= [1, 5]
  @wait_times.pop || 30
end

loop do
  if system(bin, 'remote-add', '--system', '--if-not-exists',
            'flathub', 'https://flathub.org/repo/flathub.flatpakrepo')
    break
  end

  puts 'Failed to add flathub. Trying again in a bit.'
  sleep wait_time
end

FileUtils.mkpath('/var/lib/neon/')
FileUtils.touch('/var/lib/neon/flathub-set-up')
