#!/bin/awk -f
#
#                               wingit
#
#                written by Ken Stevens for chainsaw 3.0
#
# DESCRIPTION:
# This script reads the output of the "plane" command and organises your
# planes into wings according to plane type.
#
# INSTALLATION:
# Before you run this script, make sure that you have typed:
# chmod +x wingit
# where wingit is the name of this file.
#
# HOW TO RUN IT:
# Change the values of the "wing" and "name" array to your favourite wing names
# for different types of planes.  Note that this script only uses the
# first two letters of the plane type when assigning wings, and it
# only includes planes which are more than 70% efficient in the wings.
# This script also writes a file called ".af" which lists some info about
# your planes.
#
# In your .eifrc file, put the following line:
# alias wingit "plane * >!.plane ; runfeed wingit .plane ; @cat .af"
# Then in eif you can type "wingit" to sort your planes into wings.
#
# BUG REPORTS:
# mail your bug-reports and comments to:
# stevens@math.utoronto.ca

BEGIN {
  wing["Ze"] = "Z";
  wing["f1"] = "f";
  wing["f2"] = "f";
  wing["f3"] = "f";
  wing["f4"] = "f";
  wing["lb"] = "l";
  wing["nf"] = "n";
  wing["hb"] = "h";
  wing["as"] = "u";
  wing["es"] = "e";
  wing["mb"] = "b";
  wing["tr"] = "t";
  wing["jf"] = "F";
  wing["sr"] = "i";
  wing["jl"] = "L";
  wing["np"] = "N";
  wing["nc"] = "C";
  wing["ir"] = "i";
  wing["mi"] = "M";
  wing["sl"] = "m";
  wing["jm"] = "B";
  wing["jh"] = "H";
  wing["tc"] = "t";
  wing["ac"] = "c";
  wing["jt"] = "T";
  wing["sm"] = "s";
  wing["sp"] = "p";
  wing["la"] = "d";
  wing["ab"] = "a";
  wing["ss"] = "D";
  wing["a-"] = "A";
  wing["ic"] = "I";
  wing["sf"] = "x";
  wing["sb"] = "y";
  name["Z"] = "Zepplins";
  name["f"] = "low tech fighters";
  name["lb"] = "low tech precision bombers";
  name["n"] = "low tech naval fighters";
  name["h"] = "low tech heavy bombers";
  name["u"] = "anti-sub planes";
  name["e"] = "escort planes";
  name["m"] = "medium bombers";
  name["t"] = "low tech transport planes & choppers";
  name["F"] = "jet fighters";
  name["i"] = "low tech land missiles";
  name["L"] = "jet precision bombers";
  name["N"] = "jet naval fighters";
  name["C"] = "naval choppers";
  name["M"] = "anti-ship missiles";
  name["B"] = "jet medium bombers";
  name["H"] = "jet heavy bombers";
  name["c"] = "attack choppers";
  name["T"] = "jet transports";
  name["s"] = "sams";
  name["p"] = "spy planes";
  name["d"] = "land satellites";
  name["a"] = "abm's";
  name["D"] = "spy satellites";
  name["A"] = "anti-satellites";
  name["I"] = "icbm's";
  name["x"] = "stealth fighters";
  name["y"] = "stealth bombers";
  name["z"] = "undefined";
}

{
  if (substr($0,1,1) == " " && $1 != "#" && $2 != "planes")
  {
    t = substr($2,1,2);
    w = wing[t];
    if (!w)
    {
      ++baf["z"];
      printf "Warning: undefined plane %s\n", $2 > ".af";
    }
    else
    {
      ++baf[w];
      if (substr($0, 38, 3) + 0 > 70)
      {
        ++af[w];
        if (flock[w] == "")
          flock[w] = $1;
        else if (length(flock[w]) > 80)
        {
          printf "wing %s %s\n", w, flock[w];
          flock[w] = $1;
        }
        else
          flock[w] = flock[w] "/" $1;
      }
    }
  }
}

END {
  for (w in flock)
    printf "wing %s %s\n", w, flock[w];
  printf "%4s %4s/%-4s %s\n", "wing", "eff", "tot", "type" > ".af";
  printf "-------------------------------------\n" > ".af";
  for (w in baf)
    printf "%4s %4d/%-4d %s\n", w, af[w], baf[w], name[w] > ".af";
}
