#!/usr/bin/python 
import os
import subprocess
import glob
from optparse import OptionParser

def install (file, dir):
	f = dest + prefix + dir + '/'
	s = file.rfind ('/')
	if s > -1:
		f += file[s + 1:]
	else:
		f += file
	print ("install: " + f)
	subprocess.check_output ('install -d ' + dest + prefix + dir, shell=True)
	subprocess.check_output ('install ' + file + ' ' + dest + prefix + dir + '/', shell=True)
	installed.write (f + "\n")

if not os.path.exists ("build/configured"):
	print ("Project is not configured")
	exit (1)

if not os.path.exists ("build/installed"):
        print ("Project is not built")
        exit (1)

parser = OptionParser()
parser.add_option ("-p", "--prefix", dest="prefix", help="install prefix", metavar="PREFIX")
parser.add_option ("-d", "--dest", dest="dest", help="install to this directory", metavar="DEST")

(options, args) = parser.parse_args()

if not options.prefix:
	options.prefix = "/usr"

if not options.dest:
	options.dest = ""

prefix = options.prefix
dest = options.dest

# create uninstall file
installed = open ('build/installed', 'w')
installed.write ('build/installed\n')

# install it:
for file in os.listdir('./layout'):
	install ('layout/' + file, '/share/birdfont/layout')

for file in os.listdir('./icons'):
	install ('icons/' + file, '/share/birdfont/icons')

install ('resources/linux/birdfont.desktop', '/share/applications')
install ('resources/linux/birdfont.png', '/share/icons/hicolor/48x48/apps')

install ('build/bin/birdfont', '/bin')
install ('build/bin/birdfont-export', '/bin')
install ('build/libbirdfont.so', '/lib')

install ('build/birdfont.1.gz', '/share/man/man1')
install ('build/birdfont-export.1.gz', '/share/man/man1')

# translations
for lang_dir in glob.glob('build/locale/*'):
	lc = lang_dir.replace ('build/locale/', "")
	install ('build/locale/' + lc + '/LC_MESSAGES/birdfont.mo', '/share/locale/' + lc + '/LC_MESSAGES' );
	
installed.close ()

subprocess.check_output ('ldconfig', shell=True)
