import os, sqlite

if os.path.exists("data"):
	cx = sqlite.connect("data", autocommit=True)
	cu = cx.cursor()
else:
	cx = sqlite.connect("data", autocommit=True)
	cu = cx.cursor()
	cu.execute("create table pnames(\
			p_id INTEGER PRIMARY KEY,\
			names string NOT NULL,\
			passwd string NOT NULL,\
			has_in string,\
			is_in int,\
			description string,\
			curr string,\
			last_command int,\
			ip_addr string,\
			colors string,\
			email string,\
			str int,\
			dex int,\
			wis int)")

	cu.execute("create table rooms(\
			r_id INTEGER PRIMARY KEY,\
			short_desc string NOT NULL,\
			long_desc string,\
			has_in string,\
			is_in string)")

	# A list of all active instances of everything.
	# id = instance ID
	# sub_id = subcategory for possible faster searching (room,living,object,etc.)
	# parent_id = the parent ID used to create the instance
	cu.execute("create table instances(\
			id INTEGER PRIMARY KEY,\
			sub_id int,\
			parent_id int,\
			is_owned int,\
			is_in int,\
			npc_own int)")

	cu.execute("create table exits(\
			exit_id INTEGER PRIMARY KEY,\
			fromr int,\
			tor int,\
			special_msg string,\
			special_cmd string)")

	# room_id (or instance?), which exit, which direction (north, up)
	cu.execute("create table links(\
			room_id int,\
			exit int,\
			direction string)")

	cu.execute("create table objects(\
			obj_id INTEGER PRIMARY KEY,\
			name string,\
			alias string,\
			description string,\
			flags string)")

	cu.execute("create table npcs(\
			npc_id INTEGER PRIMARY KEY,\
			name string,\
			alias string,\
			description string,\
			flags string)")


	# Create 2 rooms
	cu.execute( "insert into rooms(\
				r_id, short_desc, long_desc, has_in, is_in)\
				values (NULL,'first room','This is the first room',NULL,NULL)")
	cu.execute( "insert into rooms(\
				r_id, short_desc, long_desc, has_in, is_in)\
				values(NULL,'second room','This is the second room',NULL,NULL)")

	# Create a dummy user and add it to room #1
	cu.execute( "insert into pnames(\
				p_id, names, passwd, has_in, is_in)\
				values (NULL, 'tmp', 'tmp', NULL, '0')")
	#cu.execute( "update pnames set has_in = '0' where p_id = 1")

	# Create a two-ways exit between rooms #1 and #2
	cu.execute( "insert into exits(exit_id, fromr, tor, special_msg)\
				values (NULL, 1, 2, 'You walk in.')")
	cu.execute( "insert into exits(exit_id, fromr, tor, special_msg)\
				values (NULL, 2, 1, 'You walk out.')")
	# Create the actual links between the two rooms
	cu.execute( "insert into links(room_id, exit, direction) values\
				(1, 1, 'north')")
	cu.execute( "insert into links(room_id, exit, direction) values\
				(2, 2, 'south')")

	# Create the HELP database.
	cu.execute("create table helps(\
			command string,\
			doc string)")

	# Build basic help content.
	cu.execute("insert into helps(command,doc) values('look', 'Look command.')")
	cu.execute("insert into helps(command,doc) values('drop', 'drop <item>')")
	cu.execute("insert into helps(command,doc) values('say', 'say <text>')")
	cu.execute("insert into helps(command,doc) values('logout', 'Quit the MUD.')")
	cu.execute("insert into helps(command,doc) values('quit', 'Same as logout.')")
	cu.execute("insert into helps(command,doc) values('who', 'Players currently online.')")
	cu.execute("insert into helps(command,doc) values('description', 'description <your player description>')")
	cu.execute("insert into helps(command,doc) values('inv', 'Look at your inventory.')")
	cu.execute("insert into helps(command,doc) values('get', 'Get an item on the floor.')")
	cu.execute("insert into helps(command,doc) values('emote', 'emote <action>')")
	cu.execute("insert into helps(command,doc) values('give', 'give <item> to <player>')")
	cu.execute("insert into helps(command,doc) values('stats', 'Check your statistics.')")
	cu.execute("insert into helps(command,doc) values('setansi', 'Set your terminal to use ANSI colors or not.')")