#!/usr/bin/env python3
import sys
import irank
import optparse

if __name__ == '__main__':
	p = optparse.OptionParser()
	p.add_option('-f', help='force', action='store_true', dest='force')
	for k in irank.KEYS:
		p.add_option('--' + k, default=None, type='int', dest='rating_' + k, help='set ' + k)
	opts, files = p.parse_args()
	if len(files) == 0:
		files = map(lambda line: line.strip("\n"), sys.stdin)
	modifications = {}
	for k in irank.KEYS:
		val = getattr(opts, 'rating_' + k)
		if val is not None:
			modifications[k] = val

	assert (len(modifications) > 0 or opts.force)
	if not opts.force:
		print("Applying modifications: %s\n\nOK [Y/n]? " % (modifications,), file=sys.stderr)
		if input().strip().lower() not in ('', 'y', 'yes'): sys.exit(1)

	for file in files:
		song = irank.Song(file)
		for key, val in modifications.items():
			song.values[key] = val
		song.save()
