#!/bin/bash

# Copyright (C) 2015-2018, Ward Mundy & Associates LLC. Licensed for use under GPL2.

# Use this little script to convert FreePBX-produced GPL modules from GitHub and install in FreePBX GUI

### WARNING: Do NOT install or update packages blocked below or you may destroy your server. ###

if [ -z "$1" ]; then
 clear
 echo "ERROR: Name of FreePBX-produced GPL module is required."
 echo "SYNTAX: ./gpl-install-fpbx packagename"
 echo " "
 echo " "
 exit 1
fi

if [ "$1" == "webrtc" ] || [ "$1" == "api" ] || [ "$1" == "calendar" ] || [ "$1" == "devtools" ]; then
 echo "Sorry. $1 cannot be installed."
 exit 2
fi
if [ "$1" == "fax" ] || [ "$1" == "cxpanel" ] || [ "$1" == "speddial" ] || [ "$1" == "filestore" ]; then
 echo "Sorry. $1 cannot be installed."
 exit 2
fi
if [ "$1" == "pm2" ] || [ "$1" == "digium_phones" ] || [ "$1" == "digiumaddoninstaller" ] || [ "$1" == "xmpp" ]; then
 echo "Sorry. $1 cannot be installed."
 exit 2
fi
if [ "$1" == "dashboard" ]; then
 echo "Sorry. $1 cannot be upgraded."
 exit 2
fi
if [ "$1" == "firewall" ] || [ "$1" == "pbdirectory" ] || [ "$1" == "fw_langpacks" ]; then
 echo "Sorry. $1 cannot be installed."
 exit 2
fi

cd /root
rm -rf $1.tar.gz
rm -rf $1
mkdir $1
cd $1
curl -L -O https://github.com/FreePBX/$1/archive/release/13.0.zip
unzip 13.0.zip
clear
rm -f *.zip && mv $1-release-13.0 $1
# fake the module signature
cd $1
touch module.sig
chmod 775 module.sig
cd ..
#
tar -cf $1.tar $1 && gzip $1.tar && rm -rf $1
mv $1.tar.gz ../. && cd .. && rm -rf $1

# Now we're ready to install the GitHub update

cd /var/www/html/admin/modules
tar zxvf /root/$1.tar.gz
amportal a ma install $1
amportal a r

if [ "$1" == "framework" ]; then
 cd /var/www/html/admin/views
 wget http://incrediblepbx.com/incredible-content.tar.gz
 tar zxvf incredible-content.tar.gz
 rm -f incredible-content.tar.gz
fi

cd /root
echo "The downloaded module $1.tar.gz has been installed in the GUI."
echo " "
