#!/usr/bin/gawk -f

#this saves off any signed message and checks the signature

BEGIN {
getline <"/dev/pid";
fname = "msg"$1;
}

{
  if( $0 ~ /^-----BEGIN PGP SIGNED MESSAGE-----/ ){
    getline;
    getline;
    last2 = $0;
    getline;
    last = $0
    while( $0 !~ /^-----BEGIN PGP SIGNATURE-----/ ){
      getline;
#this canonicalizes text (strip trailing spaces, and EOL is forced to crlf)
      gsub( " $","",last2 );
      print last2 "\r" >fname".txt";
      last2 = last;
      last = $0;
    }
    while( $0 !~ /^-----END PGP SIGNATURE-----/ ){
      print > fname ".sig";
      getline;
    }
    print > fname ".sig";
  }
}

END {
  system( "./minipgp < " fname ".sig | ./minipgp -f " fname ".txt");
  system( "rm " fname ".sig " fname ".txt" );
}
