#!/usr/bin/env python
#coding: utf-8
#
# juni 順位をつける（Open usp Tukubai版）
# 
# designed by Nobuaki Tounaka
# written by Yoshio Katayama
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "juni [<k1> <k2>] <file>"
_usage1 = "juni -h [<k1> <k2>] <file>"
_version = "Wed Oct 26 03:01:58 JST 2011"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"
_keypat = r'\d+$'

import re
import os
import sys
from getopt import getopt

def error(msg, *arg):
	print >> sys.stderr, 'Error[juni] :', msg % arg
	sys.exit(1)

def usage():
	print >> sys.stderr, "Usage   :", _usage
	print >> sys.stderr, "        :", _usage1
	print >> sys.stderr, "Version :", _version
	sys.exit(1)

class FieldLine:
	def __init__(self, line, allow_z = False):
		self.__allow_zero = allow_z
		line = line.rstrip('\n')
		self.__fields = [ line ]
		self.__fields += [ x for x in line.split(' ') if x ]

	def size(self):
		return len(self.__fields) - 1

	def getFieldNum(self, key):
		if type(key) == type(0):
			return key
		if re.match(r'\d+$', key):
			key = int(key)
		elif key == 'NF':
			key = self.size()
		else:
			key = self.size() - int(key[3:])
			if key <= 0:
				error("NF-x の x が大きすぎます。")
		if key < 0:
			error("フィールド番号が負です。")
		if key == 0 and not self.__allow_zero:
			error("フィールド番号が０です。")
		if key > self.size():
			error("フィールド番号が大きすぎます。")
		return key

	def getField(self, s, e = None):
		s = self.getFieldNum(s)
		if e == None:
			e = s
		else:
			e = self.getFieldNum(e)
		if s <= e:
			return ' '.join(self.__fields[s : e + 1])
		else:
			t = self.__fields[e : s + 1]
			t.reverse()
			return ' '.join(t)

#
# 入力ファイルオープン
#
def open_file(n, mode = 'r'):
	if n >= len(sys.argv) or sys.argv[n] == '-':
		file = sys.stdin
	else:
		try:
			file = open(sys.argv[n], mode)
		except:
			error("ファイル %s をオープンできません。", sys.argv[n])
	return file

#
#メイン関数
#
if __name__ == '__main__':

	if len(sys.argv) > 1 \
	  and (sys.argv[1] == '--help' or sys.argv[1] == '--version'):
		usage()

	#
	# -h
	#
	try:
		opts = getopt(sys.argv[1:], "h")
		if opts[0]:
			h_mode = True
		else:
			h_mode = False
		sys.argv = [ sys.argv[0] ] + opts[1]
	except:
		usage()

	#
	# juni -h [<k1> <k2>] <file>
	#
	if h_mode:
		if len(sys.argv) <= 2:
			n = None
			file = open_file(1)
		elif len(sys.argv) > 4:
			usage()
		else:
			if not re.match(_keypat, sys.argv[1]) \
			 or not re.match(_keypat, sys.argv[2]):
				usage()
			k1 = int(sys.argv[1])
			k2 = int(sys.argv[2])
			if k1 == 0 or k1 > k2:
				error("フィールド番号が正しくありません、")
			n = [ 0 ] * (k2 + 1)
			oldkey = [ None ] * (k2 + 1)
			file = open_file(3)
		for line in file:
			line = FieldLine(line)
			if not n:
				k1 = 1
				k2 = line.size()
				if k2 == 0:
					error("先頭行が空行です。")
				n = [ 0 ] * (k2 + 1)
				oldkey = [ None ] * (k2 + 1)
			elif line.size() < k2:
				error("フィールドが足りません。")
			for i in range(k1, k2 + 1):
				if line.getField(i) != oldkey[i]:
					n[i] += 1
					oldkey[i] = line.getField(i)
					for i in range(i + 1, k2 + 1):
						n[i] = 1
						oldkey[i] = line.getField(i)
					break
			print ' '.join([ str(x) for x in n[k1:] ]), line.getField(0)
		sys.exit(0)

	#
	# juni <file>
	#
	if len(sys.argv) <= 2:
		file = open_file(1)
		n = 1
		for line in file:
			print n, line,
			n = n + 1
		sys.exit(0)

	#
	# juni <k1> <k2> <file>
	#
	if len(sys.argv) > 4 \
	 or not re.match(_keypat, sys.argv[1]) \
	 or not re.match(_keypat, sys.argv[2]):
		usage()
	k1 = int(sys.argv[1])
	k2 = int(sys.argv[2])
	if k1 == 0 or k1 > k2:
		error("フィールド番号が正しくありません、")
	file = open_file(3)
	oldkey = None
	for line in file:
		line = FieldLine(line)
		key = line.getField(k1, k2)
		if key != oldkey:
			n = 1
			oldkey = key
		print n, line.getField(0)
		n = n + 1

	sys.exit(0)
