# Copyright (c) 2014-2015, Clemson University
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of Clemson University nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from django.conf import settings
from . import defaults
import getpass
import socket

VCSREPO_ROOT = settings.VCSREPO_ROOT
VCSREPO_PATH_FUNCTION = getattr(settings, 'VCSREPO_RELPATH_FUNCTION', defaults.path_function)
VCSREPO_CHECK_NESTED_PATHS = getattr(settings, 'VCSREPO_CHECK_NESTED_PATHS', True)
VCSREPO_ALLOW_NESTED_PATHS = getattr(settings, 'VCSREPO_ALLOW_NESTED_PATHS', False)

VCSREPO_USE_USER_RIGHTS = getattr(settings, 'VCSREPO_USE_USER_RIGHTS', True)
VCSREPO_USE_GROUP_RIGHTS = getattr(settings, 'VCSREPO_USE_GROUP_RIGHTS', True)

VCSREPO_USER_MODEL = getattr(settings, 'VCSREPO_USER_MODEL', getattr(settings, 'AUTH_USER_MODEL', 'auth.User'))
VCSREPO_GROUP_MODEL = getattr(settings, 'VCSREPO_GROUP_MODEL', 'auth.Group')

VCSREPO_RIGHTS_FUNCTION = getattr(settings, 'VCSREPO_RIGHTS_FUNCTION', defaults.rights_function)
VCSREPO_USER_ACL_FUNCTION = getattr(settings, 'VCSREPO_USER_ACL_FUNCTION', defaults.user_acl_function if VCSREPO_USE_USER_RIGHTS else None)
VCSREPO_GROUP_ACL_FUNCTION = getattr(settings, 'VCSREPO_GROUP_ACL_FUNCTION', defaults.group_acl_function if VCSREPO_USE_GROUP_RIGHTS else None)

if hasattr(settings, 'VCS_URI_FORMAT'):
  import warnings
  warn = "VCS_URI_FORMAT is deprecated; use VCSREPO_URI_FORMAT"
  warnings.warn(warn, DeprecationWarning)
  VCSREPO_URI_FORMAT = settings.VCS_URI_FORMAT
else:
  VCSREPO_URI_FORMAT = getattr(settings, 'VCSREPO_URI_FORMAT', {})
VCSREPO_URI_FORMAT.setdefault(('git', 'ssh'), '{user}@{hostname}:{path}')
VCSREPO_URI_FORMAT.setdefault(('svn', 'ssh'), 'svn+ssh://{user}@{hostname}/{path}')
VCSREPO_URI_FORMAT.setdefault(('hg', 'ssh'), 'ssh://{user}@{hostname}/{path}')
VCSREPO_URI_FORMAT.setdefault(('git', 'anonymous-ssh'), '{anonymous}@{hostname}:{path}')
VCSREPO_URI_FORMAT.setdefault(('svn', 'anonymous-ssh'), 'svn+ssh://{anonymous}@{hostname}/{path}')
VCSREPO_URI_FORMAT.setdefault(('hg', 'anonymous-ssh'), 'ssh://{anonymous}@{hostname}/{path}')

if hasattr(settings, 'VCS_URI_CONTEXT'):
  import warnings
  warn = "VCS_URI_CONTEXT is deprecated; use VCSREPO_URI_CONTEXT"
  warnings.warn(warn, DeprecationWarning)
  VCSREPO_URI_CONTEXT = settings.VCS_URI_CONTEXT
else:
  VCSREPO_URI_CONTEXT = getattr(settings, 'VCSREPO_URI_CONTEXT', {})
VCSREPO_URI_CONTEXT.setdefault('anonymous', 'anonymous')
VCSREPO_URI_CONTEXT.setdefault('user', getpass.getuser())
VCSREPO_URI_CONTEXT.setdefault('hostname', socket.gethostname())
