#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY 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 <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 
# look for the entitlement data from the ZCS license
license=/opt/zimbra/conf/ZCSLicense.xml
if ! [ -f "$license" ]; then
    echo "error: unable to read license information" > /dev/stderr
    exit 1
fi
# pull the license UUID from the license file
key=$(cat $license | grep LicenseId | awk -F= '{print $NF}' | awk -F\" '{print $2}')
# calculate how we were called (e.g. /etc/conary/entitlements/conary.rpath.com)
server=$(basename $0)
case $server in
  products.rpath.com)
    class=zimbra-zcs
    ;;
  conary.rpath.com)
    class=zimbra-zcs
    ;;
  zimbra.liquidsys.com)
    class=zimbra-zcs
    ;;
  *)
    echo "error: script was called as $0. it should only be called via a symlink that matches the server name"
    exit 1
    ;;
esac

cat << EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE entitlement [
<!ELEMENT entitlement (server, class, key)>
<!ELEMENT server (#PCDATA)>
<!ELEMENT class (#PCDATA)>
<!ELEMENT key (#PCDATA)>
]>
<entitlement>
  <server>$server</server>
  <class>$class</class>
  <key>$key</key>
</entitlement>
EOF
