/*
 * Copyright 2015 JAXIO http://www.jaxio.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaxio.celerio;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class Brand {
    private final String celerioLicenseAgreement = "http://www.jaxio.com/documentation/celerio/celerio-license-agreement.html";
    private final String logoFilename = "logo.png";
    private final String logoPath;
    private final String brandingPath;
    private String companyName = "Jaxio";
    private String companyUrl = "http://www.jaxio.com/";
    private String footer = "";
    private String rootPackage = "com.jaxio";
    private final String maintenanceEmail = "support@jaxio.com";
    private boolean acceptEULA;

    public Brand() {
        brandingPath = System.getProperty("user.home") + "/.celerio/branding.properties";
        logoPath = System.getProperty("user.home") + "/.celerio/" + logoFilename;
        try {

            Properties p = new Properties();
            File f = new File(brandingPath);
            if (f.exists()) {
                p.load(new FileInputStream(f));
                companyName = p.getProperty("company_name", companyName);
                companyUrl = p.getProperty("company_url", companyUrl);
                footer = p.getProperty("footer", footer);
                rootPackage = p.getProperty("root_package", rootPackage);
                acceptEULA = "yes".equalsIgnoreCase(p.getProperty("accept_license_agreement_for_celerio", "no").trim());
            } else {
                p.setProperty("accept_license_agreement_for_celerio", "no");
                p.setProperty("root_package", rootPackage);
                p.setProperty("company_name", companyName);
                p.setProperty("company_url", companyUrl);
                p.setProperty("footer", footer);
                if (!f.getParentFile().exists()) {
                    f.getParentFile().mkdirs();
                }
                p.store(new FileOutputStream(f),
                        "CELERIO BRANDING\n" + "" +
                                "End User License Agreement (EULA) can be read here:\n" +
                                celerioLicenseAgreement);
                acceptEULA = false;
            }

            // copy logo if not present
            File logo = new File(logoPath);
            if (!logo.exists()) {
                PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();
                Resource defaultBrandLogo = o.getResource("classpath:/brand-logo.png");
                new FileOutputStream(logo).write(IOUtils.toByteArray(defaultBrandLogo.getInputStream()));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if (!acceptEULA) {
            System.out.println("");
            System.out.println("==========================================================================================================");
            System.out.println(" Before using Celerio, you must accept the License Agreement For Celerio");
            System.out.println(" available here: " + celerioLicenseAgreement);
            System.out.println(" To accept it, open " + brandingPath);
            System.out.println(" and set the property accept_license_agreement_for_celerio to yes instead of no");
            System.out.println(" Then try again...");
            System.out.println("==========================================================================================================");
            System.exit(1);
        }
    }

    public String getBrandingPath() {
        return brandingPath;
    }

    public boolean isAcceptEULA() {
        return acceptEULA;
    }

    public String getCompanyName() {
        return companyName;
    }

    public String getCompanyUrl() {
        return companyUrl;
    }

    public String getMaintenanceEmail() {
        return maintenanceEmail;
    }

    public String getFooter() {
        return footer;
    }

    public String getRootPackage() {
        return rootPackage;
    }

    public String getLogoPath() {
        return logoPath;
    }

    public String getLogoFilename() {
        return logoFilename;
    }
}
@@ -937,136 +937,8 @@
 d %7B%0A
-    private final String celerioLicenseAgreement = %22http://www.jaxio.com/documentation/celerio/celerio-license-agreement.html%22;%0A
     
@@ -1304,40 +1304,8 @@
 om%22;
-%0A    private boolean acceptEULA;
 %0A%0A  
@@ -1971,214 +1971,16 @@
     
-    acceptEULA = %22yes%22.equalsIgnoreCase(p.getProperty(%22accept_license_agreement_for_celerio%22, %22no%22).trim());%0A            %7D else %7B%0A                p.setProperty(%22accept_license_agreement_for_celerio%22, %22no%22);
+%7D else %7B
 %0A   
@@ -2372,243 +2372,28 @@
 (f),
-%0A                        %22CELERIO BRANDING%5Cn%22 + %22%22 +%0A                                %22End User License Agreement (EULA) can be read here:%5Cn%22 +%0A                                celerioLicenseAgreement);%0A                acceptEULA = false
+ %22CELERIO BRANDING%22)
 ;%0A  
@@ -2919,821 +2919,8 @@
    %7D
-%0A%0A        if (!acceptEULA) %7B%0A            System.out.println(%22%22);%0A            System.out.println(%22==========================================================================================================%22);%0A            System.out.println(%22 Before using Celerio, you must accept the License Agreement For Celerio%22);%0A            System.out.println(%22 available here: %22 + celerioLicenseAgreement);%0A            System.out.println(%22 To accept it, open %22 + brandingPath);%0A            System.out.println(%22 and set the property accept_license_agreement_for_celerio to yes instead of no%22);%0A            System.out.println(%22 Then try again...%22);%0A            System.out.println(%22==========================================================================================================%22);%0A            System.exit(1);%0A        %7D
 %0A   
@@ -3001,78 +3001,8 @@
  %7D%0A%0A
-    public boolean isAcceptEULA() %7B%0A        return acceptEULA;%0A    %7D%0A%0A
     

disable license check