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.
Fix edge case where pos = 1, caused by fact bam1_t is 0 based whilst sam text format is 1 based.
Correct flag check
Add version number to test
Add MQ tags when doing fixmates.
Revise tests for fixmates.
Use STDIN_FILENO constant rather than fileno(stdin) and same for stderr in bam_mate.c.
Port to new htslib architecture
Revise license to MIT.
Amend functionality comment