Some initial comments whilst I think how to fix this.
Split out syncing of mate flags
First draft of fixed fixmates program
Raise error if give @HD SO:coordinate to 'samtools fixmate'

The (minimal) documentation does tell you 'samtools fixmate' requires query name
sorted reads (in fact grouped by query name is enough), but doesn't actually raise
an error if given coordinate sorted reads (it just silently fails to fix them).

This change raises an explicit error is the @HD line contains SO:coordinate
Second draft of revised fixmates
Add code to disable proper pair check.
Clean up tabs vs spaces so we're consistant for the whole of bam_mate.c.
Polish by adding a little info to manual page, ensuring unmapped unpaired reads are treated the same as paired ones and adding details to how things work comment.
Fix assert > to >=.
Remove extra tid check
Set the PAIRED flag once we have a pair
Fix edge case where last read in BAM is unpaired.
Fix case where cur was updated when pre was meant.
Fix compiler warning about \!
Fix accidently inverted flag check for unmapped
Add a few tests for fixmate
Add GPL header template.
Add comment to bam_template_cigar to explain what it does.
Add brackets around bam_mating core unmapped check to avoid warning.