#!/usr/bin/env python2.7
# Copyright 2013 oyatsukai.com

import sys
import os
import struct

def usage(r=0):
    print "Usage: patch_word <filename> <decimal-byte-offset> <hex-word>"
    print ""
    exit(r)


if "__main__" == __name__:

    args = sys.argv;
    if 4 != len(args):
        usage(1)

    print "HERE"

    filename = args[1]
    offset = int(args[2])
    value = int(args[3], 16)

    print "filename: %s" % filename
    print "offset: %d (0x%x)" % (offset, offset)
    print "value: %d (0x%x)" % (value, value)

    with open(filename, "r+b") as f:

        f.seek(offset)
        curbytes = f.read(4)
        cur = struct.unpack("<I", curbytes)
        print "current value is: 0x%x" % cur

        f.seek(offset)
        newbytes = struct.pack("<I", value)
        f.write(newbytes)

@@ -297,26 +297,8 @@
 1)%0A%0A
-    print %22HERE%22%0A%0A
     
@@ -316,16 +316,16 @@
 args%5B1%5D%0A
+
     offs
@@ -387,20 +387,27 @@
 nt %22file
-name
+ to patch  
 : %25s%22 %25 
@@ -426,22 +426,31 @@
  print %22
+  
 offset
+       
 : %25d (0x
@@ -476,54 +476,8 @@
 set)
-%0A    print %22value: %25d (0x%25x)%22 %25 (value, value)
 %0A%0A  
@@ -627,24 +627,24 @@
 nt %22
-current
+  original
  value
- is
 : 0x
@@ -721,16 +721,16 @@
  value)%0A
-
         
@@ -743,12 +743,69 @@
 e(newbytes)%0A
+%0A    print %22  new value    : %25d (0x%25x)%22 %25 (value, value)%0A

fix up output