#!/usr/bin/perl -w
#
# DO NOT EDIT.  This file was tangled from README.org.

use strict;

if (defined $ARGV[0]
    && grep { $_ eq $ARGV[0] } qw(CA config new old pass client)) {
  exec "./Institute/inst", @ARGV;
}

if (defined $ARGV[0] && $ARGV[0] eq "upgrade") {
  shift;
  my @args = ( "-e", "\@Secret/become.yml" );
  if (defined $ARGV[0] && $ARGV[0] eq "-n") {
    shift;
    push @args, "--check", "--diff";
  }
  if (defined $ARGV[0]) {
    my $limit = $ARGV[0];
    shift;
    die "illegal characters: $limit"
      if $limit !~ /^!?[a-z][-a-z0-9,!]+$/;
    push @args, "-l", $limit;
  }
  exec ("ansible-playbook", @args, "playbooks/upgrade.yml");
}

if (defined $ARGV[0] && $ARGV[0] eq "reboots") {
  exec ("ansible-playbook", "-e", "\@Secret/become.yml",
	"playbooks/reboots.yml");
}

if (defined $ARGV[0] && $ARGV[0] eq "versions") {
  exec ("ansible-playbook", "-e", "\@Secret/become.yml",
	"playbooks/versarch.yml");
}

if (defined $ARGV[0] && $ARGV[0] eq "facts") {
  my $line = ("ansible all -m gather_facts -e \@Secret/become.yml"
	      . " >facts");
  print "$line\n";
  my $status = system $line;
  die "status: $status\nCould not run $line: $!\n" if $status != 0;
  exit;
}

if (defined $ARGV[0] && $ARGV[0] eq "tz") {
  exec ("ansible-playbook", "-e", "\@Secret/become.yml",
	"playbooks/timezone.yml");
}

my $ops = ("config,new,old,pass,client,"
           ."upgrade,reboots,versions,facts,tz");
die "usage: $0 [$ops]\n";
