import re


def construct_postscript(*, bwipp_location, issn, sequence, week, header_line):
    """Format a Postscript script ready for passing to ghostscript

    bwipp_location:  pathlib.Path object for the BWIPP Postscript library
    issn:            ISSN number, str matching regex \d{4}-\d{3,4}
    sequence:        2-digit int, usually price code & ISO weekday
    week:            ISO week number, as int
    header_line:     str up to length 24, extra info printed above barcode
    """
    postscript = '''\
%!PS
({bwipp_location}) run

11 5 moveto ({issn} {seq:02} {week:02}) (includetext height=1.07) /issn /uk.co.terryburton.bwipp findresource exec

% Print header line(s)
/Courier findfont
9 scalefont
setfont

newpath
11 86 moveto
({header}) show

showpage
'''
    if not bwipp_location.exists():
        raise ValueError('BWIPP location is incorrect')
    else:
        bwipp_location = bwipp_location.resolve()

    if not re.match(r'^\d{4}-\d{3,4}$', issn):
        raise ValueError('ISSN {0} is in incorrect format'.format(issn))

    if not 0 <= sequence <= 99:
        raise ValueError(
            'Sequence {0} is outside of range 0-99'.format(sequence))

    if not 0 < week < 54:
        raise ValueError(
            'Week {0} is not a valid ISO week. Must be between 1 and 53'
            .format(week))

    return postscript.format(
        bwipp_location=bwipp_location,
        issn=issn,
        seq=sequence,
        week=week,
        header=header_line)