#!/bin/bash -e
mkdir -p /etc/3cxpbx
cat > /etc/3cxpbx/setupconfig.xml << "<EOF>"
<?xml version="1.0" encoding="UTF-8"?>
<SetupConfig xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tcxinit>
    <option>
      <code>InstallationType</code>
      <answer>new</answer>
    </option>
    <option>
      <code>LicenseKey</code>
      <answer>XXXX-XXXX-XXXX-XXXX</answer>
    </option>
    <option>
      <code>PublicIP</code>
      <answer>auto</answer>
    </option>
    <option>
      <code>StaticOrDynamicIP</code>
      <answer>static</answer>
    </option>
    <option>
      <code>LocalIP</code>
      <answer>auto</answer>
    </option>
    <option>
      <code>NeedFqdn</code>
      <answer>yes</answer>
    </option>
    <option>
      <code>Hostname</code>
      <answer>3cx-m291</answer>
    </option>
    <option>
      <code>DnsSuffix</code>
      <answer>ch</answer>
    </option>
    <option>
      <code>DomainGroup</code>
      <answer>Europe</answer>
    </option>
    <option>
      <code>HasLocalDns</code>
      <answer>no</answer>
    </option>
    <option>
      <code>HttpsPort</code>
      <answer>443</answer>
    </option>
    <option>
      <code>HttpPort</code>
      <answer>80</answer>
    </option>
    <option>
      <code>SipPort</code>
      <answer>5060</answer>
    </option>
    <option>
      <code>TunnelPort</code>
      <answer>5090</answer>
    </option>
    <option>
      <code>CreateFqdn</code>
      <answer>self-signed</answer>
    </option>
    <option>
      <code>NumberOfExtensions</code>
      <answer>3</answer>
    </option>
    <option>
      <code>AdminEmail</code>
      <answer>a@a.ch</answer>
    </option>
    <option>
      <code>MailServerType</code>
      <answer>3CX</answer>
    </option>
    <option>
      <code>Country</code>
      <answer>Switzerland</answer>
    </option>
    <option>
      <code>Timezone</code>
      <answer>58</answer>
    </option>
    <option>
      <code>OperatorExtension</code>
      <answer>100</answer>
    </option>
    <option>
      <code>OperatorFirstName</code>
      <answer>john</answer>
    </option>
    <option>
      <code>OperatorLastName</code>
      <answer>john</answer>
    </option>
    <option>
      <code>OperatorEmail</code>
      <answer>a@a.ch</answer>
    </option>
    <option>
      <code>OperatorVoicemail</code>
      <answer>999</answer>
    </option>
    <option>
      <code>Promptset</code>
      <answer>English</answer>
    </option>
    <option>
      <code>LicenseContactName</code>
      <answer>john</answer>
    </option>
    <option>
      <code>LicenseCompanyName</code>
      <answer>john</answer>
    </option>
    <option>
      <code>LicenseEmail</code>
      <answer>a@a.ch</answer>
    </option>
    <option>
      <code>LicensePhone</code>
      <answer>+1 555 01234</answer>
    </option>
    <option>
      <code>ResellerId</code>
      <answer />
    </option>
    <option>
      <code>Language</code>
      <answer>DE</answer>
    </option>
    <option>
      <code>AdminUsername</code>
      <answer>admin</answer>
    </option>
    <option>
      <code>AdminPassword</code>
      <answer></answer>
    </option>
  </tcxinit>
  <extensions>
    <extension>
      <Number>100</Number>
      <FirstName>john</FirstName>
      <LastName>john</LastName>
      <EmailAddress>a@a.ch</EmailAddress>
      <AuthPassword></AuthPassword>
      <AuthID>nSaAq8Dd2b</AuthID>
      <AllowLanOnly>true</AllowLanOnly>
      <RecordCalls>false</RecordCalls>
      <Language>English</Language>
      <ProvisionType>RemoteExt</ProvisionType>
      <PbxDeliversAudio>false</PbxDeliversAudio>
      <SupportReinvites>true</SupportReinvites>
      <SupportReplacesHeader>true</SupportReplacesHeader>
      <SipPort>5065</SipPort>
      <AudioFirts>14000</AudioFirts>
      <AudioLast>14009</AudioLast>
      <VMEmailOptions>Notification</VMEmailOptions>
      <CustomParameters>
        <CustomParameter>
          <Name>EMAILMISSEDCALL</Name>
          <Value>1</Value>
          <ParameterType>String</ParameterType>
          <Create>true</Create>
        </CustomParameter>
        <CustomParameter>
          <Name>VOICEMAILPINAUTH</Name>
          <Value>1</Value>
          <ParameterType>String</ParameterType>
          <Create>true</Create>
        </CustomParameter>
        <CustomParameter>
          <Name>SERVICES_ACCESS_HASH</Name>
          <Value>AAAAAMAnCQAQAAAAJCPlQDc42K8KQtQL7g72sSAAAAAZhrbHbLS+nIGKmUg8+MNNWEjIvwCMFEmSiQJT9ehpcQ==</Value>
          <ParameterType>String</ParameterType>
          <Create>true</Create>
        </CustomParameter>
      </CustomParameters>
    </extension>
  </extensions>
  <mcwizard>
    <ExtensionLength>3</ExtensionLength>
    <AdminEmail>a@a.ch</AdminEmail>
    <SmtpEnableSsl>false</SmtpEnableSsl>
    <OperatorExtension>100</OperatorExtension>
    <VoicemailExtension>999</VoicemailExtension>
    <LicenseContactName>john</LicenseContactName>
    <LicenseCompanyName>john</LicenseCompanyName>
    <LicensEmail>a@a.ch</LicensEmail>
    <LicensePhone>+1 555 01234</LicensePhone>
    <LicenseCountry>41</LicenseCountry>
    <TimezoneId>58</TimezoneId>
    <CountryName>Switzerland</CountryName>
    <CountryExitCode>00</CountryExitCode>
    <PromptSetNodeName>English</PromptSetNodeName>
    <RegenerateCertificateExpiredInDays>0</RegenerateCertificateExpiredInDays>
    <TemporarySelfSignedCertificatesGenerated>false</TemporarySelfSignedCertificatesGenerated>
    <SmtpServerSettings>
      <IsTcx>true</IsTcx>
      <IsCustom>false</IsCustom>
    </SmtpServerSettings>
  </mcwizard>
  <CustomParameters>
    <CustomParameter>
      <Name>UPDATE_SCHEDULE_OPTIONS</Name>
      <Value>&lt;Options&gt;&lt;GeneralCheck ScheduleType="Interval" ScheduleEnabled="true" ScheduleInterval="2:00:00" /&gt;&lt;Firmware ScheduleType="Interval" ScheduleEnabled="true" ScheduleInterval="2:00:00" /&gt;&lt;UpdatesGeneral ScheduleType="Daily" ScheduleEnabled="true" ScheduleDays="Saturday" ScheduleTime="02:00" /&gt;&lt;UpdatesPbx ScheduleType="Daily" ScheduleEnabled="true" ScheduleDays="Saturday" ScheduleTime="02:00" /&gt;&lt;UpdatesSecurity ScheduleType="Daily" ScheduleEnabled="true" ScheduleDays="Saturday" ScheduleTime="02:00" /&gt;&lt;/Options&gt;</Value>
      <ParameterType>String</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>SEC_DEFENSE_PROGRAM</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>false</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>DO_NOT_SEND_CONGRATULATIONS_EMAIL</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_FILENAME</Name>
      <Value>3CXScheduledBackup.zip</Value>
      <ParameterType>String</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_LICENSE</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_FQDN</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_PROMPTS</Name>
      <Value>0</Value>
      <ParameterType>Int32</ParameterType>
      <Create>false</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_VOICEMAILS</Name>
      <Value>0</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_RECORDINGS</Name>
      <Value>0</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_CALLHISTORY</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_PHONE_FIRMWARES</Name>
      <Value>0</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_HISTORY_COUNT</Name>
      <Value>1</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_PASSWORD_ENABLED</Name>
      <Value>0</Value>
      <ParameterType>Int32</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>BACKUP_SCHEDULE</Name>
      <Value>0 0 0 ? * 7</Value>
      <ParameterType>String</ParameterType>
      <Create>true</Create>
    </CustomParameter>
    <CustomParameter>
      <Name>PBXERRORCODES</Name>
      <Value />
      <ParameterType>String</ParameterType>
      <Create>true</Create>
    </CustomParameter>
  </CustomParameters>
  <GlobalParameters>
    <GlobalParameter>
      <Name>INST_NOTIFY_SERVICES_DOWN</Name>
      <Value>0</Value>
    </GlobalParameter>
  </GlobalParameters>
</SetupConfig>
<EOF>
apt-get update
dpkg-query -W -f='${Status}' sudo 2>/dev/null | grep -qF "ok installed" || apt-get -y install sudo
dpkg-query -W -f='${Status}' wget 2>/dev/null | grep -qF "ok installed" || apt-get -y install wget
dpkg-query -W -f='${Status}' gnupg2 2>/dev/null | grep -qF "ok installed" || apt-get -y install gnupg2
wget -O- https://repo.3cx.com/key.pub | gpg --dearmor | sudo tee /usr/share/keyrings/3cx-archive-keyring.gpg > /dev/null
echo "deb [arch=$(dpkg --print-architecture) by-hash=yes signed-by=/usr/share/keyrings/3cx-archive-keyring.gpg] http://repo.3cx.com/3cx buster main" | sudo tee /etc/apt/sources.list.d/3cxpbx.list
apt-get update
apt-get -y install nginx
rm -f /etc/nginx/sites-enabled/default
systemctl reload nginx
apt-get -y install 3cxpbx
