#!/usr/bin/env python3
'''
    grampsw.exe
'''
import os
from os import environ
from os.path import join
import sys

if getattr(sys, 'frozen', False):
    AIO = os.path.dirname(sys.executable)
    sys.path.insert(1, os.path.join(os.path.dirname(sys.executable), 'lib'))
    environ['SSL_CERT_FILE'] = join(AIO, 'ssl/certs/ca-bundle.trust.crt')
    environ['GI_TYPELIB_PATH'] = join(AIO, 'lib/girepository-1.0')
    environ['G_ENABLE_DIAGNOSTIC'] = '0'
    environ['G_PARAM_DEPRECATED'] = '0'
    environ['GRAMPS_RESOURCES'] = join(AIO, 'share')
    environ['PATH'] = AIO + ';' + environ['PATH']

import atexit
import ctypes

def close():
    ''' Show warning dialog if Gramps is already running'''
    sys.exit('Gramps is already running!')

HANDLE = ctypes.windll.kernel32.CreateMutexW(None, 1, "org.gramps-project.gramps")
ERROR = ctypes.GetLastError()
if ERROR == 183: #ERROR_ALREADY_EXISTS:
    close()

atexit.register(ctypes.windll.kernel32.CloseHandle, HANDLE)
atexit.register(ctypes.windll.kernel32.ReleaseMutex, HANDLE)

import gramps.grampsapp as app
app.main()
