#! /bin/bash
# Setup a context
#
# Copyright (C) 2016 OX Software GmbH
# 
# This file is part of OX Installer Collection.
#
# OX Installer Collection 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, either version 3 of the License, or
# (at your option) any later version.
#
# OX Installer Collection 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 OX Installer Collection. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Martin Heiland <martin.heiland@open-xchange.com>
#
# -o: Overwrite existing context
# -r: Create reseller account, users
# -d: Override the default mail domain
# -s: Override the default mail server
# -l: Override the default login domain
# -c: Override the default context id
# -a: Override the default access combination name
# -v: Enables virtual attachments for context

AS_BIN=/opt/open-xchange/sbin
CTX_ID=10
AS_ADMINMASTER_PASS=secret
AS_ADMIN_PASS=secret
MAIL_DOMAIN=qa.open-xchange.com
MAIL_SERVER=dovecot.qa.open-xchange.com
LOGIN_DOMAIN=defaultcontext
RESELLER=false
OVERWRITE=false
ACN=all
EXTRAS=false
VIRTUAL_ATTACHMENTS=false
CAPABILITIES=""

while getopts "ord:l:c:s:a:ev" opt; do
case ${opt} in
    o)
    OVERWRITE=true
    ;;
    r)
    RESELLER=true
    ;;
    d)
    MAIL_DOMAIN=${OPTARG}
    ;;
    l)
    LOGIN_DOMAIN=${OPTARG}
     ;;
    c)
    CTX_ID=${OPTARG}
    ;;
    s)
    MAIL_SERVER=${OPTARG}
    ;;
    a)
    ACN=${OPTARG}
    ;;
    e)
    EXTRAS=true
    ;;
    v)
    VIRTUAL_ATTACHMENTS=true
    ;;
esac
done

if [ "${RESELLER}" = true ]; then
    CONTEXTID_FILE=ctxid.txt
    "${AS_BIN}"/createadmin -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" -u reseller -d "Reseller" -p "${AS_ADMIN_PASS}"
    "${AS_BIN}"/createcontext -A reseller -P "${AS_ADMIN_PASS}" -u oxadmin -d "Context Admin" -g Admin -s User -p "${AS_ADMIN_PASS}" -L "${LOGIN_DOMAIN}" -e oxadmin@"${MAIL_DOMAIN}" -q 1024 --access-combination-name="${ACN}" 1>"$CONTEXTID_FILE"
    CTX_ID=$(awk -F " " '{print $2}' "$CONTEXTID_FILE")
    rm -f "$CONTEXTID_FILE"
else
    if [[ "${OVERWRITE}" = true ]]; then
        "${AS_BIN}"/deletecontext -c "$CTX_ID" -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" 2>/dev/null
    fi

    "${AS_BIN}"/createcontext -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" -c "$CTX_ID" -u oxadmin -d "Context Admin" -g Admin -s User -p "${AS_ADMIN_PASS}" -L "${LOGIN_DOMAIN}" -e oxadmin@"${MAIL_DOMAIN}" -q 1024 --access-combination-name="${ACN}"

    if [[ $? != 0 ]]; then
        echo "Context could not be created, aborting."
        exit 1
    fi
fi

${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u mheiland -d "Martin Heiland" -g Martin -s Heiland -p secret -e mheiland@"${MAIL_DOMAIN}" --imaplogin mheiland --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u mwagner -d "Markus Wagner" -g Markus -s Wagner -p secret -e mwagner@"${MAIL_DOMAIN}" --imaplogin mwagner --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u bkroening -d "Benedikt Kröning" -g Benedikt -s Kröning -p secret -e bkroening@"${MAIL_DOMAIN}" --imaplogin bkroening --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u tthamm -d "Tobias Thamm" -g Tobias -s Thamm -p secret -e tthamm@"${MAIL_DOMAIN}" --imaplogin tthamm --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u ofelka -d "Olaf Felka" -g Olaf -s Felka -p secret -e ofelka@"${MAIL_DOMAIN}" --imaplogin ofelka --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u mhenschel -d "Maike Henschel" -g Maike -s Henschel -p secret -e mhenschel@"${MAIL_DOMAIN}" --imaplogin mhenschel --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u pschumacher -d "Philipp Schumacher" -g Philipp -s Schumacher -p secret -e pschumacher@"${MAIL_DOMAIN}" --imaplogin pschumacher --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u ostute -d "Olena Stute" -g Olena -s Stute -p secret -e ostute@"${MAIL_DOMAIN}" --imaplogin ostute --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u eahmed -d "Ejaz Ahmed" -g Ejaz -s Ahmed -p secret -e eahmed@"${MAIL_DOMAIN}" --imaplogin eahmed --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u jlaatsch -d "Jorin Laatsch" -g Jorin -s Laatsch -p secret -e jlaatsch@"${MAIL_DOMAIN}" --imaplogin jlaatsch --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u jswitalski -d "Jarek Switalski" -g Jarek -s Switalski -p secret -e jswitalski@"${MAIL_DOMAIN}" --imaplogin jswitalski --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u dpondruff -d "Daniel Pondruff" -g Daniel -s Pondruff -p secret -e dpondruff@"${MAIL_DOMAIN}" --imaplogin dpondruff --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u hnambalirwa -d "Harriet Nambalirwa" -g Harriet -s Nambalirwa -p secret -e hnambalirwa@"${MAIL_DOMAIN}" --imaplogin hnambalirwa --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u kahrens -d "Kai Ahrens" -g Kai -s Ahrens -p secret -e kahrens@"${MAIL_DOMAIN}" --imaplogin kahrens --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u abunkowski -d "Andreas Bunkowski" -g Andreas -s Bunkowski -p secret -e abunkowski@"${MAIL_DOMAIN}" --imaplogin abunkowski --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u mkoch -d "Michael Koch" -g Michael -s Koch -p secret -e mkoch@"${MAIL_DOMAIN}" --imaplogin mkoch --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"
${AS_BIN}/createuser -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -u sdoerr -d "Sonja Dörr" -g Sonja -s Dörr -p secret -e sdoerr@"${MAIL_DOMAIN}" --imaplogin sdoerr --imapserver "${MAIL_SERVER}" --smtpserver "${MAIL_SERVER}"


${AS_BIN}/creategroup -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -d "Test Group 01" -n test_group_01 -a 3,4,5
${AS_BIN}/creategroup -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -d "My Users" -n my_users -a 3,4
${AS_BIN}/createresource -c "$CTX_ID" -A oxadmin -P "${AS_ADMIN_PASS}" -d "Resource 01" -n resource_01 -e resource_01@"${MAIL_DOMAIN}"
${AS_BIN}/changecontext -c "$CTX_ID" -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" --access-combination-name="${ACN}"
if [[ "${EXTRAS}" == true ]]; then
    CAPABILITIES="${CAPABILITIES},feedback"
    if [[ -n "${AS_REPO_GUARD}" ]]; then
        CAPABILITIES="${CAPABILITIES},guard"
    fi
    ${AS_BIN}/changecontext -c "$CTX_ID" -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" --capabilities-to-add "${CAPABILITIES}"
fi
if [[ "${VIRTUAL_ATTACHMENTS}" == true ]]; then
    ${AS_BIN}/changecontext -c "$CTX_ID" -A oxadminmaster -P "${AS_ADMINMASTER_PASS}" --config/com.openexchange.file.storage.mail.enabled=true
fi
