#! /bin/gawk -f
# Last edited on 2002-01-02 13:24:51 by stolfi

# Process the "Don Quixote" in original spelling

BEGIN {
  abort = -1;
  usage = ( \
    "cat main.org \\\n" \
    "  | preprocess-org \\\n" \
    "  | evt-from-org -v omitControls=1 ... \\\n" \
    "  > main.evt" \
  );

  # Removes "[]" in contents lines

}

(abort >= 0) { exit abort; }

/^ *[#@]/ { 
  lin = $0; gsub(/^ */,"",lin); 
  print;
  next;
}

/.+[#@]/ { data_error("@/# not on column 1"); }

/^ *$/ { next; }

/./ { 
  # General contents line cleanup
  gsub(/^[ \011]+/, "  ", $0);
  gsub(/[ \011]+$/, " ", $0);
  gsub(/[ \011]+/, " ", $0);

  gsub(/[\[\]]/, "", $0);
  print;
  next;
}

END {
  if (abort >= 0) { exit abort; }
}

function arg_error(msg)
{
  printf "%s\n", msg > "/dev/stderr";
  printf "usage: %s\n", usage > "/dev/stderr";
  abort = 1;
  exit 1;
}

function data_error(msg)
{
  printf "line %d: %s\n", FNR, msg > "/dev/stderr";
  abort = 1; exit 1;
}