/**
 * @author Tres Finocchiaro
 *
 * Copyright (C) 2013 Tres Finocchiaro, QZ Industries
 *
 * IMPORTANT: This software is dual-licensed
 *
 * LGPL 2.1 This is free software. This software and source code are released
 * under the "LGPL 2.1 License". A copy of this license should be distributed
 * with this software. http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * QZ INDUSTRIES SOURCE CODE LICENSE This software and source code *may* instead
 * be distributed under the "QZ Industries Source Code License", available by
 * request ONLY. If source code for this project is to be made proprietary for
 * an individual and/or a commercial entity, written permission via a copy of
 * the "QZ Industries Source Code License" must be obtained first. If you've
 * obtained a copy of the proprietary license, the terms and conditions of the
 * license apply only to the licensee identified in the agreement. Only THEN may
 * the LGPL 2.1 license be voided.
 *
 */

package qz.deploy;

import qz.common.Constants;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.logging.Level;

/**
 *
 * @author Tres Finocchiaro
 */
public class LinuxDeploy extends DeployUtilities {
    // Try using ${linux.icon} first, if it exists
    private static String qzIcon = System.getenv("HOME") + "/" + Constants.ABOUT_TITLE + "/linux-icon.svg";
    private static String defaultIcon = "printer";
    private static boolean useQzIcon = fileExists(qzIcon);

    @Override
    public boolean createStartupShortcut() {
        return createShortcut(System.getProperty("user.home") + "/.config/autostart/");
    }

    @Override
    public boolean createDesktopShortcut() {
        return createShortcut(System.getProperty("user.home") + "/Desktop/");
    }

    @Override
    public boolean removeStartupShortcut() {
        return deleteFile(System.getProperty("user.home") +
                "/.config/autostart/" + getShortcutName() + ".desktop");
    }

    @Override
    public boolean removeDesktopShortcut() {
        return deleteFile(System.getProperty("user.home") + "/Desktop/" +
                getShortcutName() + ".desktop");
    }


    @Override
    public boolean hasStartupShortcut() {
        return fileExists(System.getProperty("user.home") + "/.config/autostart/" +
                getShortcutName() + ".desktop");
    }

    @Override
    public boolean hasDesktopShortcut() {
        return fileExists(System.getProperty("user.home") + "/Desktop/" +
                getShortcutName() + ".desktop");
    }

    @Override
    public String getJarPath() {
        String jarPath = super.getJarPath();
        try {
            jarPath = URLDecoder.decode(jarPath, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.log(Level.WARNING, "Error decoding URL: {0}{1}",
                    new Object[]{jarPath, e.getLocalizedMessage()});
        }
        return jarPath;
    }

    /**
     * Creates a Linux ".desktop" shortcut
     *
     * @param jarPath Absolute path to a jar file
     * @return Whether or not the shortcut was created successfully
     */
    private boolean createShortcut(String folderPath) {
        String workingPath = getParentDirectory();
        String shortcutPath = folderPath + getShortcutName() + ".desktop";

        // Create the shortcut's parent folder if it does not exist
        return createParentFolder(shortcutPath) && writeArrayToFile(shortcutPath, new String[]{
            "[Desktop Entry]",
            "Type=Application",
            "Name=" + getShortcutName(),
            "Exec=java -jar \"" + getJarPath() + "\"",
            workingPath.trim().equals("") ? "" : "Path=" + workingPath,
            //"IconIndex=" + iconIndex,
            "Icon=" + (useQzIcon ? qzIcon : defaultIcon),
            "Terminal=false",
            "Comment=" + getShortcutName()
        });
    }
}

@@ -1202,278 +1202,8 @@
 s %7B%0A
-    // Try using $%7Blinux.icon%7D first, if it exists%0A    private static String qzIcon = System.getenv(%22HOME%22) + %22/%22 + Constants.ABOUT_TITLE + %22/linux-icon.svg%22;%0A    private static String defaultIcon = %22printer%22;%0A    private static boolean useQzIcon = fileExists(qzIcon);%0A%0A
     
@@ -2730,18 +2730,21 @@
  @param 
-ja
+folde
 rPath Ab
@@ -3489,41 +3489,20 @@
 %22 + 
-(useQzIcon ? qzIcon : defaultIcon
+getIconPath(
 ),%0A 
@@ -3591,10 +3591,184 @@
 ;%0A    %7D%0A
-%7D
+%0A    private String getIconPath() %7B%0A        String linuxIcon = getParentDirectory() + %22/linux-icon.svg%22;%0A        return fileExists(linuxIcon) ? linuxIcon : %22printer%22;%0A    %7D%0A%7D%0A
 %0A

Fix desktop icon for Linux