#!/bin/bash

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

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

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


cd /root
rm -rf $1.tar.gz
rm -rf $1
mkdir $1
cd $1
curl -L -O https://github.com/FreePBX-ContributedModules/$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 " "
