local createVersion = require('/apis/libversion');

local function printUsage()
  print('trapos-upgrade usage:');
  print();
  print('\t\ttrapos-upgrade');
  print('\t\ttrapos-upgrade --reboot');
  print('\t\ttrapos-upgrade version');
  print('\t\ttrapos-upgrade help');
end

local rawArgs = table.pack(...);
local reboot = false;

for i = 1, rawArgs.n do
  local a = rawArgs[i];
  if a == 'version' or a == '-version' or a == '--version' then
    print('v' .. createVersion().forSelf());
    return;
  elseif a == 'help' or a == '-help' or a == '--help' then
    printUsage();
    return;
  elseif a == '--reboot' or a == '-reboot' or a == '-r' then
    reboot = true;
  elseif a ~= nil and a ~= '' then
    printUsage();
    return;
  end
end

shell.execute('ccpm', 'update');
shell.execute('ccpm', 'upgrade');

if reboot then
  print();
  print('=> Rebooting...');
  os.reboot();
end
