I only know "metaflac --add-replay-gain *.flac" where *.flac must only match the tracks that you want to group into an album.
from metaflac --help:
--add-replay-gain Calculates the title and album gains/peaks of the given
FLAC files as if all the files were part of one album,
then stores them in the VORBIS_COMMENT block. The tags
are the same as those used by vorbisgain. Existing
ReplayGain tags will be replaced. If only one FLAC file
is given, the album and title gains will be the same.
Since this operation requires two passes, it is always
executed last, after all other operations have been
completed and written to disk. All FLAC files specified
must have the same resolution, sample rate, and number
of channels. The sample rate must be one of 8, 11.025,
12, 16, 22.05, 24, 32, 44.1, or 48 kHz.
As for a script I think "find" with the -execdir command could be used to use metflac on all files in a directory.
There is also normalize-audio for MP3 but that uses RVA2 frames in id3v2. Quod Libet supports this, though. Actually only this, not ReplyGain itself.