#!/usr/bin/env python
from sys import argv, exit
from binascii import unhexlify as hex2chr
import os

def convert(tfile):
  try:
    fstat = os.stat(tfile)
  except OSError:
    print "Could not find file '%s'" % (tfile,)
    exit()
  px = open(tfile, "r").read()
  px1 = px.replace(hex2chr("44534C522D41333030"), hex2chr("44534C522D41323030"))
  o = open(tfile, "w")
  o.write(px1)
  o.close()
  #und jetzt die original-stat setzen
  os.utime(tfile, (fstat.st_atime, fstat.st_mtime))
  print "%s done" % (tfile,)

if __name__=="__main__":
  if len(argv)<2:
  	print "You have to enter the name of a ARW file.\nEnter 'all' to process all ARW files in the current directory\n"
  	exit()
  else:
    if argv[1].strip()=="all":
      for tf in os.listdir("./"):
        if tf.split(".")[-1].lower()=="arw":
          convert(tf)
    else:
      convert(argv[1])
