Percival variational principle for invariant measures and commensurate-incommensurate phase transitions in one-dimensional chains