#!/usr/bin/env python

from __future__ import print_function

import six
import sys
import tarfile

stdin = getattr(sys.stdin, 'buffer', sys.stdin)
stdout = getattr(sys.stdout, 'buffer', sys.stdout)

try:
    progname, old_shebang, new_shebang = sys.argv
except ValueError:
    sys.exit("usage: replace-tarball-shebang '#!OLDSHEBANG' '#!NEWSHEBANG' < IN.tar[.gz] > OUT.tar")

old_shebang_bytes = old_shebang.encode()
new_shebang_bytes = new_shebang.encode()

with tarfile.open(fileobj=stdin, mode='r|*') as in_tar, \
     tarfile.open(fileobj=stdout, mode='w', format=tarfile.PAX_FORMAT, pax_headers=in_tar.pax_headers) as out_tar:
    for info in in_tar: # type: ignore # https://github.com/python/typeshed/pull/693
        if info.isfile():
            file = in_tar.extractfile(info)
            data = file.read()
            if data.startswith(old_shebang_bytes + b' ') or data.startswith(old_shebang_bytes + b'\n'):
                print('editing', info.name, file=sys.stderr)
                assert info.size == len(data)
                data = new_shebang_bytes + data[len(old_shebang_bytes):]
                info.size = len(data)
            out_tar.addfile(info, six.BytesIO(data))
        else:
            out_tar.addfile(info)
