#!/scratchbox/tools/bin/python

import sys, os, sb.config

args = sys.argv[:1]
cmd = os.path.basename(args[0])
next_is_mode = False
just_copy = False

for arg in sys.argv[1:]:
	if not just_copy:
		if next_is_mode:
			next_is_mode = False
		elif arg == "-m" or arg == "--mode":
			next_is_mode = True
		elif arg[0] != "-":
			if arg.startswith("/dev/"):
				old_arg = arg
				arg = "/targets/%s%s" % (sb.config.current(), old_arg)

				print >>sys.stderr, "%s wrapper: %s -> %s" \
				      % (cmd, old_arg, arg)

				argdir = os.path.dirname(arg)
				if not os.path.exists(argdir):
					os.makedirs(argdir)

			just_copy = True

	args.append(arg)

os.execv("/scratchbox/tools/bin/%s.bin" % cmd, args)
