#!/usr/bin/python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (c) 2012, Peter Levi <peterlevi@peterlevi.com>
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import os
import sys

if os.geteuid() == 0:
    print(
        'Variety is not supposed to run as root.\n'
        'You should NEVER run desktop apps as root, unless they are supposed to make '
        'system-global changes and you know very well what you are doing.\n'
        'Please run it with your normal user.\n'
        '\n'
        'If you are trying to run as root because Variety does not start at all with your normal '
        'user, you may be hitting a file permission issue or a bug.\n'
        'Here is what to do to troubleshoot:\n'
        '\n'
        '1. Open a terminal and run "variety -v" with your normal user.\n'
        'Look for exceptions and hints in the log for what the problem might be.\n'
        '\n'
        '2. You may try to rename ~/.config/variety to ~/.config/variety_bak and try again.\n'
        'This will have Variety start from a clean state.\n'
        'Your old config and images will remain in variety_bak\n'
        '\n'
        '3. If none of these helps, open a bug in https://github.com/varietywalls/variety/issues '
        'and follow the instructions there.'
    )
    exit(1)

# Add project root directory (enable symlink and trunk execution)
PROJECT_ROOT_DIRECTORY = os.path.abspath(
    os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
)

python_path = []
if os.path.abspath(__file__).startswith("/opt"):
    syspath = sys.path[:]  # copy to avoid infinite loop in pending objects
    for path in syspath:
        opt_path = path.replace("/usr", "/opt/extras.ubuntu.com/variety")
        python_path.insert(0, opt_path)
        sys.path.insert(0, opt_path)
if os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, "variety")):
    python_path.insert(0, PROJECT_ROOT_DIRECTORY)
    sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
if python_path:
    os.putenv(
        "PYTHONPATH", "%s:%s" % (os.getenv("PYTHONPATH", ""), ":".join(python_path))
    )  # for subprocesses

import variety  # isort:skip

variety.main()
