# FreePBX Distro Kickstart
#
# Copyright (c) 2016 Sangoma Technologies Corporation - All Rights Reserved.
#
# This is NOT Open Source software.
#
# You are NOT permitted to modify, reverse engineer, or redistribute this
# file without explicit permission from Sangoma Technologies Corporation.
#
# Permission is only granted to use this file on an unmodified distribution
# of FreePBX Distro. Any other use is strictly prohibited.
#
# THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# SANGOMA TECHNOLOGIES CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
set -x -v
exec 1>/mnt/sysimage/root/ks-post.log 2>&1

# This runs OUTSIDE of the chroot.

# Update our bootsplash from whatever is on this image
if [ ! -f /run/install/repo/isolinux/splash.png ]; then
  echo "No splash.png found, not installing"
else
  cp /run/install/repo/isolinux/splash.png /mnt/sysimage/boot/splash.png
  cp /run/install/repo/isolinux/splash.png /boot/splash.png
  echo "GRUB_BACKGROUND=/boot/splash.png" >> /mnt/sysimage/etc/default/grub
  echo "GRUB_BACKGROUND=/boot/splash.png" >> /etc/default/grub
  # Update GRUB_TERMINAL_OUTPUT to be gfxterm if it's console.
  # If this is a serial install, it doesn't matter that we've added
  # a background.
  sed -i '/^GRUB_TERMINAL_OUTPUT/ s/console/gfxterm/' /mnt/sysimage/etc/default/grub
  sed -i '/^GRUB_TERMINAL_OUTPUT/ s/console/gfxterm/' /etc/default/grub
fi

# HACK for 1805 ISO
echo "blacklist snd-hdmi-lpe-audio" > /mnt/sysimage/etc/modprobe.d/audio_blacklist.conf
echo "blacklist snd-hdmi-lpe-audio" > /etc/modprobe.d/audio_blacklist.conf

# Now generate some stats to be sent back to the install server.
#
# If you're looking at this, wondering what the smeg, these are
# all anonymous, and are only used for installation tracking.
#
SYSTYPE=$(dmidecode -t 1,2 | gzip | base64 -w0)
INSTALLTIME=$(awk 'NR==1;END{print}' /tmp/anaconda.log | gzip | base64 -w0)
CPUS=$(grep model\ name /proc/cpuinfo | uniq -c | base64 -w0)
DEFGW=$(ip route show | awk '/^default/ { print $5 }')
MACADDR=$(ip -o link show $DEFGW | awk 'match($0, /ether ([^\ ]+)/,m){print m[1]}')
if [ ! "$MACADDR" ]; then
  # Something's wrong, there's no MAC on this device? Send the interface
  # config for debugging
  LINKOUTPUT=$(ip -o link | gzip | base64 -w0)
  MACHASH="error"
else
  MACHASH=$(echo $MACADDR | md5sum | cut -d\  -f1)
fi
# Memory and HDD info
MEMINFO=$(free -w | base64 -w0)
HDDINFO=$(lsblk --raw -b | gzip | base64 -w0)

# This logs how the machine was installed, what params were passed to anaconda
CMDLINE=$(cat /proc/cmdline | gzip | base64 -w0)
FPBXID=$(cat /mnt/sysimage/etc/asterisk/freepbx-id)

# Save them
set > /mnt/sysimage/var/log/install.set

# Get a unique system UUID if we can
UUID=$(curl -s -m30 https://push2.schmoozecom.com/uuid | sed -r 's/\{.uuid.:.(.+)\"\}/\1/')
if [ "$UUID" ]; then
  # This machine has internet access.
  echo $UUID > /mnt/sysimage/etc/asterisk/system-id
  echo $UUID > /mnt/sysimage/etc/sangoma/system-id
  chmod 666 /mnt/sysimage/etc/asterisk/system-id /mnt/sysimage/etc/sangoma/system-id

  # Does the brand need updating?
  BRAND=$(curl -k -s -m 30 https://push2.schmoozecom.com/deployment/brand | awk 'match($0, /"brand":"([^"]+)"/,m){print m[1]}')
  if [ "$BRAND" ]; then
    echo $BRAND > /mnt/sysimage/etc/sangoma/pbx-brand
  fi

  # Send anonymized logs for install stats.
  curl -s -m30 --data-urlencode uuid="${UUID}" --data-urlencode fpbxid="${FPBXID}" --data-urlencode machash="${MACHASH}" --data-urlencode linkoutput="${LINKOUTPUT}" --data-urlencode systype="${SYSTYPE}" --data-urlencode installtime="${INSTALLTIME}" --data-urlencode cpus="${CPUS}" --data-urlencode meminfo="${MEMINFO}" --data-urlencode hddinfo="${HDDINFO}" --data-urlencode cmdline="${CMDLINE}" https://katanafpbx.schmoozecom.com/distro/install

fi





