#!/usr/bin/env python3
"""
Aug 26th 2010, Dale Hamel, University of Manitoba
 Description: Determines whether the system is OS X or linux, and
 creates a symbolic link to the appropriate launcher

 Note: this script is partnered with creatlauncher.py
 createlauncher.py makes the launchers at installtime, and
 linklauncher.py creates the links whenever newuser is run.

 Synopsis: python3 linklauncher.py


@modified: May 15, 2023
@author: Dale Hamel, Brian Fristensky
@contact: brian.fristensky@umanitoba.ca

"""


import os
import sys

PROGRAM = "linklauncher.py: "
USAGE = "\n\tUSAGE: linklauncher.py"

def get_desktop():
    """
        DEPRECATED
        Returns the location of the current user's desktop directory
        by reading the environment variable "HOME", and assuming that there
        is a "HOME/Desktop" directory
        """
    home_dir = os.environ.get("HOME")

    if (os.path.exists(home_dir)):
        desktop_dir = home_dir + "/Desktop"

        if (os.path.exists(desktop_dir)):
            print(PROGRAM + "Found desktop at: " + desktop_dir)

        else:
            desktop_dir = None

    return desktop_dir

def link_gnome_launcher(birch_dir, desktop_dir):
    """
        Creates a link to a launcher of the form biolegato.desktop for use on
        linux desktops such as GNOME (v2+) and KDE (v3.5+). May also work
        on other desktops such as qt, etc, but not yet tested

        @desktop_dir: the location of the user's desktop directory,
        where the launcher will be placed
        """

    file_name = birch_dir + "/admin/launchers/birch.desktop"

    if (birch_dir != None and os.path.isdir(desktop_dir)):
        os.chdir(desktop_dir)
        os.system("ln -s "+file_name+" .")
        print("Made symbolic link to launcher on desktop")


def link_apple_launcher(birch_dir, desktop_dir):
    """
        Makes a launcher application for OS X (tested on 10.5.8) of the format
        biolegato.app. The launcher is just a wrapper for an apple script
        that calls the system biolegato, and should use system paths.

        The this method copies a template launcher called "biolegato.app"
        to the desktop that is assumed to be in the current working directory
        in adirectory called "osx_launcher", if this changes then this method
        will need to be updated.

        Once the launcher is copied, all the contents of the BIRCH directory
        specified by the environment variable $BIRCH will be symbolically
        linked into the applications resource directory under:

        ~/biolegato.app/Contents/Resources"

        @desktop_dir: the location of the user's desktop directory,
        where the launcher will be placed
        """

    if (birch_dir != None):
        os.chdir(desktop_dir)
        #os.system("ln -s "+birch_dir+"/admin/launchers/biolegato.app .")
        os.system("ln -s "+birch_dir+"/admin/launchers/birch.app .")
        print(PROGRAM + "Made symbolic link to launcher on desktop")


def link_win_launcher(birch_dir):
    """
        Creates a shortcut to the windows launcher script to run biolegato (birch)

    """

    windir=os.environ.get("HOMEDRIVE")+os.environ.get("HOMEPATH")
    os.chdir(windir+"/Desktop")
    print(os.getcwd())


    #Note: there is a hardcoded link to biolegato here, this should be fixed!
    biolegato=birch_dir+"/java/bioLegato/biolegato.bat"
    birch_icon=birch_dir+"/public_html/birch_black_ico.icns"

    cygwin_bat="/cygwin.bat"

    os.system("mkshortcut -n bioLegato -i "+birch_icon+" "+biolegato)
    os.system("mkshortcut -n birchTerm "+cygwin_bat)


def main(BIRCH, PLATFORM):
    #desktop_dir = get_desktop()
    print("PLATFORM: " + PLATFORM)
    home_dir = os.environ.get("HOME")
    #if (desktop_dir != None):

    #This assumes either OSX or using gnome/kde, if this changes
    #this switch will need to be updated
    if "linux" in PLATFORM or "Linux" in PLATFORM :
        # Make link for GNOME2 type desktops eg. MATE
        desktop_dir= os.path.join(home_dir,"Desktop")
        link_gnome_launcher(BIRCH, desktop_dir)
        # Make linke for GNOME3 type desktops.
        desktop_dir= os.path.join(home_dir,".local","share","applications")
        link_gnome_launcher(BIRCH, desktop_dir)       

    elif "osx" in PLATFORM or "Darwin" in PLATFORM or "macos" in PLATFORM :
        desktop_dir= os.path.join(home_dir,"Desktop")
        link_apple_launcher(BIRCH, desktop_dir)

    elif(PLATFORM=="winxp-32"):
        link_win_launcher(BIRCH)

    else:
        exit()

    print(PROGRAM + "completed execution normally.")

if __name__=="__main__":
    if "-test" in sys.argv():
        pass
    else:
        main(BIRCH, PLATFORM)
