#!/bin/bash
#
# (c) Copyright 2001-2014, Server General, Inc. All rights reserved.

# Copyright (c) 2001-2014. The Server General, Inc.
# All Rights Reserved. Contact Server General, Inc., 865
# Merrick Road, Suite 204, NY 11510, (650) 485-1415 for any additional
# information regarding this notice.

# All materials are the intellectual property of Server General,
# Inc. and may not be copied, reproduced, distributed or
# displayed without Server General, Inc.'s express written
# permission.

# IN NO EVENT SHALL SERVER GENERAL BE LIABLE TO ANY PARTY FOR DIRECT,
# INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
# LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
# DOCUMENTATION, EVEN IF SERVER GENERAL HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# SERVER GENERAL SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
# IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". SERVER GENERAL HAS NO
# OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
# MODIFICATIONS.

SERVERG_CONF="/etc/serverg/serverg.conf"
LICENSE_FILE="/tmp/sg-kms-license.txt"

SG_KMS_APP=
if test -f "/usr/share/sg-kms/sg-kms.jar"
then
	SG_KMS_APP=/usr/share/sg-kms/sg-kms.jar
elif test -f "/usr/sbin/sg-kms.jar"
then
        SG_KMS_APP=/usr/sbin/sg-kms.jar
else
	SG_KMS_APP=sg-kms.jar
fi

do_dump_license(){
echo "" > $LICENSE_FILE
echo "                     	Server General, Inc." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "                      	865 Merrick Road" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "                      	Baldwin, NY 11510" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "                     	www.servergeneral.com" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "THIS DESCRIPTION SOFTWARE LICENSE AGREEMENT SHALL GOVERN YOUR USE OF AGREEMENT. YOUR USE OF THE SOFTWARE SHALL BE DEEMED ACCEPTANCE OF THE TERMS OF THIS AGREEMENT.  IF YOU DO NOT AGREE TO THE TERMS OF THIS AGREEMENT THEN DO NOT CLICK ON "I Agree with the terms of the license" DURING THE INSTALLATION PROCESS, DO NOT INSTALL THE SOFTWARE AND DO NOT USE THE SOFTWARE.  SERVER GENERAL URGES YOU TO CAREFULLY READ THIS AGREEMENT AND ASSESS YOUR USE OF THE SOFTWARE PRIOR TO INSTALLING THE SOFTWARE" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "THIS SOFTWARE SUBSCRIPTION AGREEMENT, effective as of the "Effective Date", between Server General, Inc., a Delaware corporation ("Server General"), and the "Customer", sets forth the terms and conditions whereby Server General agrees to provide to Customer and Customer agrees to acquire from Server General one or more limited licenses to use the Software owned or licensed by Server General, as set forth on the "Purchase Order" pursuant to which Customer agrees to license the Licensed Material. The foregoing undefined terms are as set forth in Section 1 below. Such Purchase Order is incorporated herein by reference and shall become a part of this Agreement. The Purchase Order together with the terms and conditions of this Agreement, shall constitute and be construed as a single agreement consisting of the terms and conditions contained in the Purchase Order together with the terms of this Agreement. In consideration of the mutual promises and agreements contained herein, the parties hereto also agree as follows:  " >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "1. Definitions -" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) "Agreement" means this Subscription Software License Agreement, together with all Purchase Orders made a part hereof in accordance with the terms of this Agreement and all amendments, modifications, supplements and alterations thereto effected in accordance with the terms of this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) "Confidential Information" means all technical and non-technical information in both tangible and intangible form, including, but not limited to, product design information, software code, technical information, customer information, cost and pricing information, financial information, the terms of this Agreement and the results derived from or methodology employed by Customer in conducting any benchmark testing of the Licensed Software; provided that the term "Confidential Information" shall not include information which the recipient can show by reasonable proof (i) to have been known by the recipient prior to the time of disclosure by the disclosing party, (ii) to have become part of the public domain through no fault or breach of this Agreement by the recipient, (iii) to have been disclosed to the recipient in good faith by a third party who is not under any obligation of confidence or secrecy to the disclosing party at the time such third party discloses the information to the recipient or (iv) to have been compelled to be produced by a court of competent jurisdiction, provided that the recipient shall first give notice of any such request or order of the court to give the disclosing party an opportunity to contest or limit said request or order of the court." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(c) "Critical Errors" means any error, defect or omission that (i) is discovered in the Licensed Software, (ii) is reproducible and (iii) prevents its operation substantially in accordance with the Licensed Documentation." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(d) "Customer" means the customer set forth on the Purchase Order or in the payment portion of the Reseller's licensed purchase process for the Software." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(e) "Effective Date" means the date on which the Customer accepts this Agreement as part of the Software download process." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(f) "Licensed Documentation" means the published user manuals that Server General makes generally available for the Licensed Software." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(g) "Licensed Material" means the Licensed Software and the Licensed Documentation." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(h) "Licensed Software" means the machine-readable object code version of (i) the software specified on each Purchase Order, provided via Internet download site, and (ii) all Updates, revisions, enhancements, improvements and modifications to and programming fix for the Licensed Software that Customer is entitled to receive pursuant to Section 4 of this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(i) "License Term" means the period of time (i) specified on the Purchase Order pursuant to which Customer agreed to license the Licensed Material or (ii) to the extent no such period is specified on the Purchase Order, a one-year period of time, in each case commencing on the Effective Date, together with all subsequent renewals and extensions thereof effected in accordance with the terms of this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(j) "Maintenance Services" means the services set forth in Section 4 hereof." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(k) "Purchase Order" means either Server General's or its Reseller's purchase order form or process to license the Software provided by Reseller." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(l) "Reseller" means the value added reseller that is participating in Server General's hosting program and is making available via download the Software to such reseller's customers." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(m) "Update" means any revision, enhancement, improvement or modification to or programming fix for the Licensed Software or Licensed Documentation which Server General makes generally available, incorporates into and makes a part of the Licensed Software or Licensed Documentation and does not separately price or market." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "2. LICENSE -" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Use - Subject to the terms and conditions of this Agreement and Customer's full compliance herewith and according to the scope, time period and other terms indicated on the applicable Purchase Order, Server General hereby grants Customer and Customer hereby accepts from Server General, a limited, non-exclusive and non-transferable right and license to install and use the Licensed Material licensed by Customer during the License Term on only hardware that is possessed or operated by or on behalf of Customer and to copy the Licensed Material as permitted by this Agreement. Upon expiration of the License Term this Agreement and all rights and licenses granted under this Agreement shall automatically terminate." >> $LICENSE_FILE
echo "(b) Copies and Disaster Recovery -- Customer may install and use each item of Licensed Software during the License Term, as applicable, (i) on one instance per license for all Licensed Software licensed by Server General on a per-instance basis, (ii) or on one CPU with one user per license for all Licensed Software licensed by Server General on a per-user basis or (iii) on one CPU without regard to the number of users for all Licensed Software licensed by Server General on a per-CPU-basis. In no event shall Customer install or operate the Licensed Software on more than one CPU, instance or user, as applicable, per license purchased. Customer is not required to disclose to Server General the serial numbers of the CPUs upon with the Licensed Software is installed; provided, however, that Customer shall not remove the Licensed Software from such CPUs and install them on new CPUs without obtaining the prior consent of Server General and a new License Key. Customer may make a reasonable number of back-up archival copies of the Licensed Software and any related Updates. In the event of any disaster that results in the complete failure of Customer's computer system, Customer's right to use the Licensed Software shall include, during the pendency of such disaster, the temporary right to use the Licensed Software on hardware substantially similar to the hardware upon which the Licensed Software was installed immediately prior to such disaster.  Customer shall reproduce all confidentiality and proprietary notices on each of the copies permitted hereunder and maintain an accurate record of the location of each of the copies. Customer shall not otherwise copy or duplicate the Licensed Material. Customer shall not reverse engineer, disassemble, translate, modify, adapt, or decompile the Licensed Material or apply any procedure or process to the Licensed Material in order to ascertain, derive, and/or appropriate the source code or source listings for the Licensed Software or any trade secret or other proprietary information contained in the Licensed Software." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Renewal of License - Upon expiration of each License Term, all licenses granted under this Agreement shall automatically renew for an additional 12-month License Term and Server General or Reseller, as the case may be, will invoice Customer at the then-current subscription-based list price for such additional License Term unless Server General or Reseller are notified by Customer in writing at least 60 days prior to the expiration of the current License Term that Customer will not renew the licenses granted hereunder for another License Term." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "3. PAYMENT TERMS -" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Payment -- Upon execution of each Purchase Order by both parties, Server General or Reseller shall deliver an invoice to Customer specifying the total license fees payable for the first year of the License Term. Each year thereafter during the License Term Server General or Reseller shall invoice Customer for the amount payable for the next year of the License Term. Customer shall pay all license fees specified in each invoice via credit card provided by Customer during the purchase process to license Software. Any late payment of any amount owing hereunder shall accrue interest at a rate equal to the lesser of (i) 15% per annum and (ii) the maximum rate permitted by law." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) Taxes - All payments referred to in this Agreement are exclusive of value added tax, sales tax and any other applicable taxes, duties or imposts which (with the exception only of those based on Server General's or its Reseller's income) shall also be payable by Customer in accordance with applicable law." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "4. MAINTENANCE - Server General does not warrant that the Licensed Software will operate error-free or may be used error-free. With respect to the Licensed Software specified in each Purchase Order, Server General or its Reseller shall provide Maintenance Services in accordance with this Section 4 only upon Customer's payment of the license fees associated with the License Term purchased by Customer. Server General or its Reseller will provide Maintenance Services for the Licensed Software during each License Term. Maintenance Services includes problem determinations, reasonable problem resolutions, provisioning of software program temporary fixes and new releases. Maintenance Services shall also include the use of Server General's or its Reseller's telephone or email help service for questions or problems associated with the Licensed Materials. Maintenance Services shall be provided by phone support during regular business hours and via pager, email, or other forms of communication during all other hours. Maintenance Services shall entitle Customer to receive, at no additional cost, all Updates." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "5. PROPRIETARY RIGHTS - Customer shall not acquire, by virtue of this Agreement, any other right or license than as expressly provided herein. Customer shall not reproduce the Licensed Material or other confidential or proprietary information of Server General, except as provided in this Agreement. All proprietary rights in and to the Licensed Material, all derivatives, translations, modifications, adaptations, improvements, enhancements or developments thereof and all confidential or proprietary information of Server General, including without limitation, all rights under and with respect to patents, copyrights, trademarks and rights under the trade secret laws of any jurisdiction shall remain the property of Server General or its applicable licensor, whether recognized by or perfected under applicable local law. Customer shall promptly notify Server General of any infringement of Server General's proprietary rights of which it becomes aware." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "6. LIMITED WARRANTIES" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Warranty - Server General warrants to Customer that during the first 30 days after the Effective Date (the "Warranty Period") the Licensed Software shall perform substantially as described in the accompanying Licensed Documentation. Server General does not warrant that (i) the Licensed Material will satisfy or may be customized to satisfy any of Customer's requirements or any other particular use or (ii) the use of the Licensed Material will be uninterrupted or error-free. Laws from time to time in force may imply warranties that cannot be excluded or can only be excluded to a limited extent. This Agreement shall be read and construed subject to any such statutory provisions." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) Remedies - If (i) at any time during a License Term, the Licensed Software contains Critical Errors which make the Licensed Software unable to perform substantially as described in the accompanying Licensed Documentation or (ii) during the Warranty Period, Server General breaches any of the warranties set forth in clause (a) or (b) above, then Customer shall promptly notify Server General or its Reseller of such nonconformance or breach. To the extent that such Critical Errors exist and are solely Server General's responsibility, Server General shall (A) use all commercially reasonable efforts to correct such Critical Error within 30 days of notification or (B) provide Customer within 30 days of notification with a plan acceptable to Customer for correcting such Critical Errors. If the Critical Errors are not corrected or if an acceptable plan for correcting such Critical Errors is not established, Server General shall replace any defective Licensed Software or, if not practicable, accept the return of the defective Licensed Software and refund to Customer the pro rata pre-paid amount for the remaining portion of the License Term.  Server General's obligations under this Section 6(b) shall be waived in the event of (I) any problem with data on disk files or diskettes which have been caused by deficiencies in the hardware on which it was installed, or by improper handling or use of the software media by Customer, or (II) an unauthorized alteration or revision to Licensed Software or to Customer's computer system by Customer or its employees. Customer acknowledges that this Section 6(b) sets forth Customer's sole and exclusive remedy, and Server General's and its Reseller's exclusive liability, for any breach of warranty or other failure of the Licensed Software to function properly." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(d) DISCLAIMER - EXCEPT FOR THE FOREGOING, THE SOFTWARE IS PROVIDED "AS IS, "WITHOUT ADDITIONAL WARRANTY OF ANY KIND, AND SERVER GENERAL AND ITS RESELLERS EXPRESSLY DISCLAIM ALL OTHER WARRANTIES, EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR TITLE, ANY WARRANTIES ARISING FROM A COURSE OF DEALING, USAGE, OR TRADE PRACTICE, OR ANY WARRANTIES OF NON-INFRINGEMENT OF ANY THIRD PARTY'S PATENT(S), TRADE SECRET(S), COPYRIGHT(S) OR OTHER INTELLECTUAL PROPERTY RIGHTS. SERVER GENERAL DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE SOFTWARE WILL MEET CUSTOMER'S REQUIREMENTS, OR THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE.  FURTHERMORE, SERVER GENERAL DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OR THE RESULTS OF THE USE OF THE SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY, OR OTHERWISE. NO ORAL OR WRITTEN INFORMATION OR ADVICE GIVEN BY SERVER GENERAL OR SERVER GENERAL'S RESELLERS SHALL CREATE ANY WARRANTY OR IN ANY WAY INCREASE THE SCOPE OF THIS WARRANTY." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "7. INDEMNITY" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Indemnity - Server General agrees, subject to this Section 7 and Section 8 below, to defend and indemnify Customer from and against all damages, judgments, awards and settlements arising from the violation of any third party's trade secrets or infringement of such third party's copyright or patents by the Licensed Material. Customer shall promptly notify Server General in writing of each alleged violation or infringement upon learning that a claim may be asserted and Server General or its Reseller may, at its option, have sole control over the defense of the claim and any negotiation for its settlement or compromise. In no event shall Customer take any action that is contrary to Server General's interest." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) Alternative Remedy - If a claim described in Paragraph 7(a) may or has been asserted, Customer will permit Server General, at Server General's option and expense, to (i) procure the right to continue using the Licensed Material, (ii) replace or modify the Licensed Material to eliminate the infringement while providing functionally equivalent performance or (iii) accept the return of the Licensed Material and refund to Customer the amount actually paid to Server General or its Reseller for such Licensed Material less the pro rata pre-paid amount for the remaining portion of the License Term." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(c) Limitation - Server General shall have no indemnity obligation to Customer hereunder if the violation or infringement claim results from (i) a correction or modification of the Licensed Material not provided by Server General or its Reseller, (ii) the failure to promptly install an Update or (iii) the combination of the Licensed Software with other non-Server General software." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "8. NO CONSEQUENTIAL DAMAGES - UNDER NO CIRCUMSTANCES WILL Server General OR ITS AUTHORIZED REPRESENTATIVES BE LIABLE FOR ANY CONSEQUENTIAL, INDIRECT, SPECIAL, PUNITIVE, EXEMPLARY OR INCIDENTAL DAMAGES, WHETHER FORESEEABLE OR UNFORESEEABLE, BASED ON CLAIMS BY CUSTOMER OR ANY THIRD PARTY (INCLUDING, BUT NOT LIMITED TO, CLAIMS FOR LOSS OF DATA, GOODWILL, PROFITS, USE OF MONEY OR USE OF THE PRODUCTS, INTERRUPTION IN USE OR AVAILABILITY OF DATA, STOPPAGE OF OTHER WORK OR IMPAIRMENT OF OTHER ASSETS), ARISING OUT OF BREACH OF EXPRESS OR IMPLIED WARRANTY, BREACH OF CONTRACT, BREACH OF ANY INTELLECTUAL PROPERTY RIGHT, MISREPRESENTATION, NEGLIGENCE, STRICT LIABILITY IN TORT OR OTHERWISE, EXCEPT ONLY IN THE CASE OF PERSONAL INJURY WHERE AND TO THE EXTENT THAT APPLICABLE LAW REQUIRES SUCH LIABILITY. IN NO EVENT WILL THE AGGREGATE LIABILITY INCURRED IN ANY ACTION OR PROCEEDING BY Server General OR ITS AUTHORIZED REPRESENTATIVE EXCEED THE TOTAL AMOUNT ACTUALLY PAID BY CUSTOMER FOR THE SPECIFIC LICENSED SOFTWARE THAT DIRECTLY CAUSED THE DAMAGE DURING THE PRECEDING SIX (6) MONTHS PRIOR TO THE CLAIM FOR SUCH DAMAGE." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "9. CONFIDENTIALITY - Customer acknowledges that the Products incorporate confidential and proprietary information developed or acquired by or licensed to Server General and that all results of testing of the Products, whether performed by Customer or another third party, are confidential. In no event will Customer publish or disclose the results of any testing or performance specifications of the Product without Server General's express prior written consent. Customer shall take all reasonable precautions necessary to safeguard the confidentiality of all Confidential Information disclosed by Server General, including those precautions (i) taken by Customer to protect it's own Confidential Information and (ii) which Server General or its Reseller may reasonably request from time to time." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "10. TERMINATION - Upon 30-days prior written notice to Server General, Customer may terminate this Agreement; provided, however that Customer shall not be entitled to a refund (except as expressly set forth in Section 6(b) and 7(b)) of any amounts paid or a release from or cancellation, waiver or novation of any amount payable, and all such amounts payable or promised to be paid through the License Term shall automatically accelerate and be immediately due and payable and shall survive such termination of this Agreement. Server General may terminate this Agreement, without judicial or administrative resolution or obligation to refund any license fees paid by Customer hereunder if either (i) Customer or any of Customer's employees, consultants, authorized representatives or permitted third parties breach any term or condition of this Agreement, or (ii) in the event that the Reseller from which Customer obtained the License Material ceases to participate in Server General's hosting program for the License Material. This Agreement will terminate automatically (i) upon expiration of the License Term, or (ii) if Customer ceases to do business, becomes insolvent, goes or is put into receivership or liquidation, passes a resolution for its winding up (other than for the purpose of reconstruction or amalgamation) or for any of the foregoing, makes an arrangement for the benefit of its creditors, enters into bankruptcy, suspension of payments, moratorium, reorganization or any other proceeding that relates to insolvency or protection of creditors' rights or takes or suffers any similar action in consequence of debt. Upon the termination of this Agreement for any reason, all rights granted to Customer hereunder will cease, and Customer will promptly (i) purge the Licensed Software and any related Updates from the hardware on which it was installed and all of Customer's other computer systems, storage media and other files, (ii) destroy the Licensed Material and all copies thereof and (iii) deliver to Server General an affidavit certifying that Customer has complied with these termination obligations. The provision of Sections 5 and 8 through 12 shall survive the termination of this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "11. COMPLIANCE AND AUDIT RIGHTS" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "You shall bear the responsibility to monitor the use of the Program to ensure compliance with the terms of this Agreement and shall maintain accurate and complete records such that the identity can be determined. Server General shall, either itself or through its designated representative, have the right to conduct annual audits of your use under this Agreement. In the event that any such audit shows any use of the Program is being used on more than the Authorized Number of Servers or unauthorized copying, then you will pay the cost of the audit, and the Termination section above will apply." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "12. U.S. EXPORT RESTRICTIONS - Customer acknowledges that the Licensed Materials and all related technical information, documents and materials are subject to export controls under the U.S. Export Administration Regulations. Customer covenants and agrees to comply with all import and export control regulations of the United States with respect to the Licensed Material." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "13. EQUITABLE RELIEF - The parties recognize that Sections 5, 9 and 11 are necessary for the protection of the business and goodwill of the parties and are considered by the parties to be reasonable for such purpose. The parties agree that any breach of such Sections would cause the other party substantial and irreparable damage and therefore, in the event of any such breach, in addition to other remedies which may be available, the non-breaching party shall have the right to seek specific performance and other injunctive and equitable relief in a court of law." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "14. LICENSE KEY - Customer acknowledges that a special security code owned by Server General (the "License Key") and provided by Server General or its Reseller is required to render the Licensed Software operational during the License Term. Further, Customer acknowledges that the License Key shall expire upon the expiration of the current License Term and that upon such expiration the Licensed Software shall cease to operate unless and until another License Key is obtained from Server General or its Reseller. Customer shall not attempt to crack, alter or otherwise derive the License Key. Server General or its Reseller shall promptly (which in the event of any renewal of a License Term shall be prior to expiration of the current License Term) provide Customer with all necessary License Keys upon renewal of each License Term and upon any authorized transfer of the Licensed Software to any other hardware equipment permitted under Section 2(b) of this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "15. UPGRADES - All pricing is determined based upon Server General's or its Reseller's then-current subscription-based list price at the time of purchase. Copies of Server General's or its Reseller's subscription-based price list are available to the Customer upon request.  Server General reserves the right to charge an additional license fee for each Software license subscribed to by Customer for enhancements to the Software that are more significant than an Update." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "16. ENFORCEABILITY - If for any reason a court of competent jurisdiction finds any provision of this Agreement, or portion thereof, to be unenforceable, void, invalid or illegal, that provision shall be enforced to the maximum extent permissible so as to effect the intent of the parties, and the remainder of this Agreement shall continue in full force and effect." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "17. ENTIRE AGREEMENT" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Customer acknowledges that it has read this Agreement, understands it and agrees to be bound by its terms. Customer and Server General further agree that, subject to clause (b) below, this Agreement, together with the Purchase Order delivered in connection herewith, is the complete and exclusive statement of the agreement between Customer and Server General and supersedes all proposals, oral or written, and all other communications between the parties relating to the subject matter of this Agreement. Subject to clause (b) below, this Agreement may not be amended, modified, supplemented or altered except by a written agreement that is signed by both parties and which expressly refers to this Section 16." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) UNDER NO CIRCUMSTANCES MAY THE TERMS OF THIS AGREEMENT OR ANY SERVER GENERAL PURCHASE ORDER BE AMENDED, MODIFIED, SUPPLEMENTED, ALTERED, SUPERSEDED OR REPLACED BY ANY NON-SERVER GENERAL INVOICE OR NON-SERVER GENERAL PURCHASE ORDER OR OTHER SIMILAR INSTRUMENT DELIVERED BY CUSTOMER TO SERVER GENERAL. EACH PARTY ACKNOWLEDGES AND AGREES THAT, AS A CONVENIENCE TO CUSTOMER AND ONLY FOR CUSTOMER's INTERNAL ACCOUNTING PROCEDURES, CUSTOMER MAY DELIVER TO SERVER GENERAL A CUSTOMER INVOICE OR CUSTOMER PURCHASE ORDER OR OTHER SIMILAR DOCUMENT FOR ANY TRANSACTION CONTEMPLATED HEREUNDER AND THAT NO ACTION BY SERVER GENERAL, INCLUDING SERVER GENERAL'S DELIVERY OF ANY LICENSED MATERIALS OR ACCEPTANCE OF PAYMENT, SHALL BE DEEMED TO BE ACCEPTANCE OF ANY OF THE TERMS OR CONDITIONS CONTAINED IN SUCH CUSTOMER INVOICE OR CUSTOMER PURCHASE ORDER OR OTHER SIMILAR INSTRUMENT AND SUCH TERMS AND CONDITIONS SHALL BE VOID AND OF NO FORCE OR EFFECT, UNLESS ACCEPTED BY SERVER GENERAL PURSUANT TO A WRITTEN INSTRUMENT SIGNED BY BOTH PARTIES AND EXPRESSLY REFERRING TO THIS SECTION 17." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "18. MISCELLANEOUS" >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(a) Customer may not assign, delegate or otherwise transfer this Agreement or any of its rights or obligations hereunder to any other person or entity without Server General's prior written consent. In the event of any merger of Customer or a sale of substantially all of the assets of Customer in which Customer is not the surviving entity, Customer may assign or transfer any licenses granted under this Agreement prior to the date of such merger or sale; provided, however, Customer may not transfer any other rights or obligations hereunder, including but not limited to the right to purchase Maintenance Services or additional Licensed Material under the terms of this Agreement. This Agreement shall be binding upon the parties hereto and shall inure to the benefit of the parties hereto and their respective permitted successors and assigns." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(b) All notices or approvals required or permitted under this Agreement must be given in writing and delivered to the appropriate party at the address set forth below the signatures to this Agreement." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(c) The waiver of compliance with or breach of any term or condition of this Agreement or the failure of a party to exercise any right under this Agreement shall in no event constitute a waiver as to any other failure to comply or breach, whether similar or dissimilar in nature, or prevent the exercise of any right under this Agreement. Unless otherwise specified herein, the rights and remedies of Server General set forth in this Agreement are not exclusive and are in addition to any other rights and remedies available to it at law or in equity." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(d) This Agreement will be governed by the laws of the State of New York, without reference to conflict of laws principles. In any dispute arising out of this Agreement, Server General and you consent to the exclusive jurisdiction and venue of both the state and federal courts of New York, New York." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(e) Unless otherwise specified herein, the rights and remedies of Server General set forth in this Agreement are not exclusive and are in addition to any other rights and remedies available to it at law or in equity." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(f) This Agreement is not intended to be nor shall it be construed as a joint venture, association, partnership or other form of business organization or agency relationship." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(g) Headings used in this Agreement are for reference purposes only and shall not be used to modify the meaning of the terms and conditions of this Agreement. This Agreement may be executed in counterparts, all of which shall constitute one single agreement between the parties hereto." >> $LICENSE_FILE
echo "" >> $LICENSE_FILE
echo "(h) In consideration of the mutual covenants contained herein, including the rights and licenses granted to Customer herein, the parties hereto do hereby agree that for a period of two years following Customer's most recent purchase of any licenses or services, including Maintenance Service, from Server General or its Reseller, Customer shall not solicit, induce, hire, engage, or attempt to hire or engage any employee of Server General, or in any other way interfere with Server General's contractual or employment relations with any of its employees, nor will Customer hire or engage or attempt to hire or engage any individual who was an employee of Server General." >> $LICENSE_FILE
}

function disable_apparmor() {
	command -v aa-status >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		APPARMOR_MYSQLD=$(ls /etc/apparmor.d/ | grep -m 1 "mysql" || echo NOT_FOUND)

		if test "$APPARMOR_MYSQLD" != "NOT_FOUND"
		then
			if ! test -f "/etc/apparmor.d/disable/$APPARMOR_MYSQLD"
			then
				sudo ln -s /etc/apparmor.d/$APPARMOR_MYSQLD /etc/apparmor.d/disable/ && sudo apparmor_parser -R /etc/apparmor.d/$APPARMOR_MYSQLD
			
				if [ $? -ne 0 ]; then
					echo "$MYPROG: ERROR: Can't disable AppArmor profile for MySQL, try to disable it manually and restart $MYPROG"
					exit 105
				fi

				echo "The AppArmor profile for MySQL has been disabled!"
			fi
		fi
	fi
}

function disable_selinux() {
	command -v aa-status >/dev/null 2>&1
	if test -f "/etc/selinux/config" 
     	then
		if ! grep -q "SELINUX=disabled" /etc/selinux/config
		then
			sudo bash -c "echo SELINUX=disabled > /etc/selinux/config" && sudo bash -c "echo SELINUXTYPE=targeted >> /etc/selinux/config"
			if [ $? -ne 0 ]; then
				echo "$MYPROG: ERROR: SELinux can't be disabled permanently, try to disable it manually and restart $MYPROG"
				exit 104
			fi
		
			sudo setenforce 0
			if [ $? -ne 0 ]; then
				echo "$MYPROG: ERROR: SELinux can't be disabled, try to disable it manually and restart $MYPROG"
				exit 105
			fi

			echo "SELinux has been disabled!"
		fi
	fi
}

##############################################################################
# sg-kms must be run as normal user
if test `id -u` == 0; then
    if [ "$SUDO_USER" = "" ]; then
        echo "This command cannot be executed by root"
        echo "Please, log in as \"sgadmin\" and try executing the command again"
    elif [ "$SUDO_USER" == "sgadmin" ]; then
        echo "This command can't be executed using sudo"
        echo "Please, re-run the command without using sudo"
    else 
        echo "This command must be executed as \"sgadmin\" without using sudo"
        echo "Please, log in as \"sgadmin\" and try executing the command again without using sudo"
    fi
    exit 126
elif [ "$USER" != "sgadmin" ]; then
    echo "This command must be executed as \"sgadmin\""
    echo "Please, log in as \"sgadmin\" and try executing the command again"
    exit 1
fi

disable_apparmor
disable_selinux
if [[ $# -gt 0 ]] || [[ -z $DISPLAY ]]
then
	# Check if machine is licensed.
	sudo grep -q "license=" $SERVERG_CONF
	if [ $? -eq 0 ]
	then
        	java -jar $SG_KMS_APP $*
	else
		do_dump_license
        	more $LICENSE_FILE

        	i=1
	        while [ $i -le 3 ]; do
	                read -p "ACCEPT License Agreement? [NO/YES] (default NO)" yn
        	        case $yn in
                	    Yes | YES | yes )
                        	answer="y"
	                        break
	                        ;;
        	            No | NO | no )
                	        answer="n"
                        	break
	                        ;;
        	            * ) echo "Please answer yes or no."
                	        ;;
	        	esac
        	        let i=i+1
	        done

        	if [ "$answer" = "n" ]; then
	                echo "You must accept the license to continue"
        	        exit 1
		else
        		java -jar $SG_KMS_APP console
	        fi
	fi 
else
        java -jar $SG_KMS_APP
fi
