#!/usr/bin/python
import os
import socket
import sys
from datetime import date

print 'usage: [system.properties file path] [sub dir name]'

today = date.today().strftime('%Y%m%d')
today = sys.argv[2]

hostnames=[
    'i676.dong.shgt.qihoo.net',
    'i677.dong.shgt.qihoo.net',
    'i678.dong.shgt.qihoo.net',
    'i679.dong.shgt.qihoo.net',
    'i680.dong.shgt.qihoo.net',
    'i681.dong.shgt.qihoo.net',
    'i682.dong.shgt.qihoo.net',
    'i683.dong.shgt.qihoo.net',
    'i684.dong.shgt.qihoo.net',
    'i685.dong.shgt.qihoo.net',
    'i686.dong.shgt.qihoo.net',
    'i687.dong.shgt.qihoo.net',
    'i688.dong.shgt.qihoo.net',
    'i689.dong.shgt.qihoo.net',
    'i690.dong.shgt.qihoo.net',
    'i691.dong.shgt.qihoo.net',
    'i692.dong.shgt.qihoo.net',
    'i693.dong.shgt.qihoo.net',
    'i694.dong.shgt.qihoo.net',
    'i695.dong.shgt.qihoo.net'
]

disks=[#'/data01',
       '/data02','/data03','/data04','/data05','/data06',
       '/data07','/data08','/data09','/data10','/data11','/data12'
       ]

properties={
    'node.current.index':0,
    'node.count':20,
    'monitor.port':2602,
    'node.httpqueryserver.port':8081,
    'node.httpqueryserver.pid':'/tmp/webcache/httpqueryserver.pid',
    'dump.processfile':'/data01/webcache/dump.process',
    'filename_bases':'16,50',
    'file_store_dirs':'/tmp/webcache/',
    'file_blacklist':'/tmp/webcache/blacklist'

}

dirs=[]
for i in disks:
    if(os.system('ls %s/webcache' % (i)) == 0):
        dirs.append('%s/webcache/%s' % (i,today))

properties['file_store_dirs']=','.join(dirs)
if(len(dirs) > 0):
    properties['dump.processfile']=dirs[0]+'/dump.process'
    properties['node.httpqueryserver.pid']=dirs[0]+'/../httpqueryserver.pid'
    properties['file_blacklist']=dirs[0]+'/../blacklist'

hostn = socket.gethostname()
idx=0
try:
    idx =  hostnames.index(hostn)
except:
    print '#invalid host'
properties['node.current.index']=idx


#print properties
f = open(sys.argv[1], 'w')
f.write('#generated by %s\n'%(sys.argv[0]))
for (k,v) in properties.iteritems():
    print k,'=',v
    f.write(k+'='+str(v)+'\n')

