#! /usr/bin/gawk -f # Last edited on 2003-09-11 23:13:50 by stolfi BEGIN { usage = ( ARGV[0] " [-v dir=DIRNAME] [-v nextMsg=NNNNN] < FOLDER" ); abort = -1 # Splits a mail folder with non-standard separators, # such as "From VM " lines with initial " " # or ">", or "\014File: NNN", or "=====...= NNN ===". if (dir == "") { dir = "."; } if (nextMsg == "") { nextMsg = 10000; } file = ""; irep=0; start_new_file(); } (abort >= 0) { exit abort; } /[ >]?From VM[ ]/ { hdr = $0; finish_current_file(); start_new_file(); gsub(/^[ >]*/, "", hdr); printf "%s\n", hdr > file; nlines++; next; } /^[\014]File(-Name|):.*[ \/][0-9]+ *$/ { finish_current_file(); start_new_file(); next; } /^============================* [0-9]+ === *$/ { finish_current_file(); start_new_file(); next; } // { print > file; nlines++; next; } END { if (abort >= 0) { exit abort; } finish_current_file(); exit 0; } function start_new_file( lin) { if ($0 != "") { printf "%s\n", $0 > "/dev/stderr"; } while (1) { file = sprintf("%s/%05d-%d", dir, nextMsg, irep); if ((getline lin < file) >= 0) { # File exists: close(file); printf "(skipping over %s)\n", file > "/dev/stderr"; irep++; if (irep > 3) { nextMsg = (nextMsg + 1 + 7*FNR) % 100000; irep = 0; } } else { # File doesn't exist, OK printf "-> %s ... ", file > "/dev/stderr"; break; } } nlines = 0; } function finish_current_file( ) { if (nlines > 0) { printf " %d lines\n", nlines > "/dev/stderr"; close(file); file = ""; nextMsg++; if (nextMsg > 99999) { nextMsg = 0; } irep = 0; } else { printf " no lines, omitted\n" > "/dev/stderr"; } }