#!/usr/bin/python

import os, sys

quilt = '/usr/bin/quilt'
saved = '.saved-quilt-top'

def main(args):
    if not os.path.exists(quilt):
        sys.exit('Can\'t find quilt binary "%s"' % quilt)

    requilt()

def load_quilt_top():
    if not os.path.exists(saved): return None
    return open(saved).read().strip()

def requilt():
    top = load_quilt_top()
    if not top: return

    if os.system('quilt push %s > /dev/null' % top) >> 8:
        sys.exit('Failed pushing quilt patches! Reapply manually')

main(sys.argv)
