#!/usr/bin/env bash

# Auto install astercc package shell
# By Donnie #### donnie@astercc.com last modify 2012-04-11
# By Solo  #### solo@astercc.com last modify 2012-01-13
# autoload g729
#######################################################################
Usage()  
{  
    echo -e "Usage: `basename $0` [-dbu=] [-dbpw=] [-amiu=] [-amipw=] [-allbydefault] \n";  
    exit 1;  
}  

echo "*****************************************************************"
echo "****************** Installing astercc package *******************"
echo "*****************************************************************"

curuser=`whoami`
if [ ${curuser} != "root" ]
then
  echo "must be root user to install astercc"
  exit
fi

if [ -f "/etc/redhat-release" ]
then
	RELEASEVER=$(rpm --eval %rhel)

	#if [[ $RELEASEVER -ne 6 ]] && [[ $RELEASEVER -ne 7 ]]; then
	#	echo "unsupported OS version"
	#	exit 1
	#fi
fi

curpath=`pwd`

dbuser=''
dbpasswd=''
amiuser=''
amisecret=''
allbydefault=0
ARGS="$@"
cp=/bin/cp

for ARG in $ARGS
do 
	iparm=`echo $ARG |sed s/\-dbu=//1`
	if [ "X$iparm" != "X$ARG" ];then
		if [ "X$iparm" != "X" ];then
			#echo "dbuser=$iparm"
			dbuser=$iparm
		fi
		continue
	fi
	iparm=`echo $ARG |sed s/\-dbpw=//1`
	if [ "X$iparm" != "X$ARG" ];then
		if [ "X$iparm" != "X" ];then
			#echo "dbpw=$iparm"
			dbpasswd=$iparm
		fi
		continue
	fi
	iparm=`echo $ARG |sed s/\-amiu=//1`
	if [ "X$iparm" != "X$ARG" ];then
		if [ "X$iparm" != "X" ];then
			#echo "amiuser=$iparm"
			amiuser=$iparm
		fi
		displayamidetail=0
		continue
	fi
	iparm=`echo $ARG |sed s/\-amipw=//1`
	if [ "X$iparm" != "X$ARG" ];then
		if [ "X$iparm" != "X" ];then
			#echo "amisecret=$iparm"
			amisecret=$iparm
		fi
		continue
	fi
	iparm=`echo $ARG |sed s/\-allbydefault//1`
	if [ "X$iparm" != "X$ARG" ];then
		allbydefault=1
		continue
	fi
	echo "Incorrect parameter:$ARG";
	Usage
	exit;
done
if [ "$allbydefault" == "1" ];then
	dbhost='127.0.0.1'
	dbport='3306'
	dbname='astercc10'
	dbtableprefix='cc10_'
	dbbin='/usr/bin'
	astserver='127.0.0.1'
	amiport='5038'
	autocreatedb=1
	mainpath='/var/www/html/asterCC'
	asterisketc='/etc/asterisk'
	autostartflag='y'
	startflag='y'
fi


#echo "###### create GROUP and USER #################"
# if not exist add the asterisk group
grep -c "^asterisk:" /etc/group &> /dev/null
if [ $? = 1 ]
then
echo "Adding group asterisk..."
/usr/sbin/groupadd -r -f asterisk
else
echo "group asterisk already present"
fi

# if not exist add the asterisk user
grep -c "^asterisk:" /etc/passwd &> /dev/null
if [ $? = 1 ]
then
echo "adding user asterisk..."
/usr/sbin/useradd -c "Astercc PBX&CallCenter" -g asterisk \
-r -s /bin/bash -m -d /var/lib/asterisk \
asterisk
else
echo "user asterisk already present"
fi

if [ ! -d "${curpath}/app" ]
then
  echo -n 'app directory not in here, are you sure contiue?(y/n):'
  read ncrmflag
  if [ "X${ncrmflag}" != "Xy" -a "X${ncrmflag}" != "XY" ]
  then
    exit
  fi
fi

if [ ! -d "${curpath}/scripts" ]
then
  echo -n 'scripts directory is not found, are you sure contiue?(y/n):'
  read nscriptflag
  if [ "X${nscriptflag}" != "Xy" -a "X${nscriptflag}" != "XY" ]
  then
    exit
  fi
fi

if [ "X$dbhost" == "X" ];then
	echo Please enter database information 
	echo -n "database host(default 127.0.0.1):"
	read dbhost
	if [ "X$dbhost" == "X" ];then
		dbhost='127.0.0.1'
	fi
else
	echo "database host is $dbhost";
fi

if [ "X$dbuser" == "X" ];then
	echo Please enter database information 
	echo -n "database user(default root):"
	read dbuser
	if [ "X$dbuser" == "X" ];then
		dbuser='root'
	fi
else
	echo "database user is $dbuser";
fi

if [ "X$dbpasswd" == "X" ];then
	echo Please enter database information 
	echo -n "database password(default null):"
	read dbpasswd
else
	echo "database password is $dbpasswd";
fi

if [ "X$dbport" == "X" ];then
	echo -n "database port(default 3306):"
	read dbport
	if [ "X$dbport" == "X" ];then
		dbport='3306'
	fi
else
	echo "database port is $dbport";
fi

if [ "X$dbname" == "X" ];then
	echo -n "database name(default astercc10):"
	read dbname
	if [ "X$dbname" == "X" ];then
		dbname='astercc10'
	fi
else
	echo "database name is $dbname";
fi

if [ "X$dbtableprefix" == "X" ];then
	echo -n "table prefix(default cc10_):"
	read dbtableprefix
	if [ "X$dbtableprefix" == "X" ];then
		dbtableprefix='cc10_'
	fi
else
	echo "table prefix is $dbtableprefix";
fi

if [ "X$dbbin" == "X" ];then
	echo -n "mysql bin path(default /usr/bin):"
	read dbbin
	if [ "X$dbbin" == "X" ];then
		dbbin='/usr/bin'
	fi
else
	echo "mysql bin path is $dbbin";
fi

if [ "X${dbpasswd}" != "X" ];
then
  dbpasswdstr="-p"${dbpasswd}
fi

${dbbin}/mysqladmin --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ping 1>${curpath}/db.test 2>/dev/null
dbtest=`cat ${curpath}/db.test`

if [ "$dbtest" != "mysqld is alive" ]
then
  echo $dbtest
  echo "database connection failed!"
  exit
fi

rm -rf ${curpath}/db.test 2>&1

if [ "$autocreatedb" == "1" ];then
	${dbbin}/mysqladmin --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} drop ${dbname} -f 2&>/dev/null
	${dbbin}/mysqladmin --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} create ${dbname}
else
	echo "If database:'"${dbname}"' is not exists, press 'y' to create," && echo -n "else press 'n' to skip this step:" 
	read dbexisist

	if [ "X${dbexisist}" == "Xy" -o "X${dbexisist}" == "XY" ]
	then
		${dbbin}/mysqladmin --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} create ${dbname}
	else
		echo "Warning: All data could be lost in "${dbname}" by next step," && echo -n "are you sure to continue?[y/n]:" 
		read createTable

		if [ "X${createTable}" != "Xy" -a "X${createTable}" != "XY" ]
		then 
			echo "User cancel"
			exit
		fi
	fi
fi

if [ $? -ne 0 ];
then
  echo "database operation failed!"
  exit
else
  cp -f $curpath/sql/astercc10.sql $curpath/sql/astercc10_n.sql
  cp -f $curpath/sql/astercc_search_fields.sql $curpath/sql/astercc_search_fields_n.sql
  cp -f $curpath/sql/astercc_pbxtmplates.sql $curpath/sql/astercc_pbxtmplates_n.sql
  cp -f $curpath/sql/astercc_privilegesID.sql $curpath/sql/astercc_privilegesID_n.sql
  cp -f $curpath/sql/astercc_role_privilegesID.sql $curpath/sql/astercc_role_privilegesID_n.sql
  cp -f $curpath/sql/astercc_relate_table.sql $curpath/sql/astercc_relate_table_n.sql
  cp -f $curpath/sql/astercc_trashes.sql $curpath/sql/astercc_trashes_n.sql
  cp -f $curpath/sql/astercc_default_leftlist.sql $curpath/sql/astercc_default_leftlist_n.sql
  cp -f $curpath/sql/astercc_statistics.sql $curpath/sql/astercc_statistics_n.sql
  sed -i 's/nat=yes/nat=force_rport,comedia/g' $curpath/sql/astercc_pbxtmplates_n.sql
#  cp -f $curpath/sql/demo_en.sql $curpath/sql/demo_en_n.sql
#  cp -f $curpath/sql/demo_cn.sql $curpath/sql/demo_cn_n.sql
  if [ "X${dbtableprefix}" != "X" ];
  then
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc10_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_search_fields_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_pbxtmplates_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_privilegesID_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_role_privilegesID_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_relate_table_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_trashes_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_default_leftlist_n.sql
#	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/demo_en_n.sql
#	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/demo_cn_n.sql
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/astercc_statistics_n.sql
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_statistics_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc10_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_search_fields_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_pbxtmplates_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_privilegesID_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_role_privilegesID_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_relate_table_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_trashes_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/astercc_default_leftlist_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi
fi

#echo Please choose the system default language 
#echo -n "en or cn (default en):"
#read defaultlang

#if [ "X${defaultlang}" == "X" ];
#then
  defaultlang="en"
#else
#  defaultlang=`echo $defaultlang |tr [:upper:] [:lower:]`
#  if [ "X${defaultlang}" != "Xen" -a "X${defaultlang}" != "Xcn" ];
#  then
#    defaultlang="en"
#  fi
#fi

cp -f $curpath/sql/default_settings_cn.sql $curpath/sql/default_settings_cn_n.sql
if [ "X${dbtableprefix}" != "X" ];
then
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/default_settings_cn_n.sql
fi

cp -f $curpath/sql/default_settings_en.sql $curpath/sql/default_settings_en_n.sql
if [ "X${dbtableprefix}" != "X" ];
then
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/default_settings_en_n.sql
fi

cp -f $curpath/sql/demo_cn.sql $curpath/sql/demo_cn_n.sql
if [ "X${dbtableprefix}" != "X" ];
then
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/demo_cn_n.sql
fi

cp -f $curpath/sql/demo_en.sql $curpath/sql/demo_en_n.sql
if [ "X${dbtableprefix}" != "X" ];
then
	sed -i 's/cc10_/'${dbtableprefix}'/g' $curpath/sql/demo_en_n.sql
fi

if [ "X${defaultlang}" != "Xen" ];then
  
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/default_settings_cn_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi
else
  
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/default_settings_en_n.sql
  if [ $? -ne 0 ];
  then
    exit;
  fi

fi

if [ "X${asterisketc}" == "X" ];then
	echo Please enter absolute path of asterisk etc 
	echo -n "asterisk etc (default /etc/asterisk):"
	read asterisketc

	if [ "X${asterisketc}" == "X" ];
	then
	  asterisketc="/etc/asterisk"
	fi

	while [ 1 ]
	do 
	  if [ ! -d "${asterisketc}/" ]
	  then
		echo "error: Can not found ${asterisketc}"
		echo -n "asterisk etc:"
		read asterisketc
	  else
	    break
	  fi
	done
fi
echo "asterisk etc path is $asterisketc"
if [ "$allbydefault" == "0" ];then
	cat ${asterisketc}/manager.conf
fi

if [ "X$astserver" == "X" ];then
	echo "Please enter the Asterisk infomation:"
	echo -n "Asterisk Host(default 127.0.0.1):"
	read astserver
	if [ "X${astserver}" == "X" ];
	then
	  astserver="127.0.0.1"
	fi
fi

if [ "X$amiport" == "X" ];then
	echo -n "Asterisk Manager API port(default 5038):"
	read amiport
	if [ "X${amiport}" == "X" ];
	then
	  amiport="5038"
	fi
fi


if [ "X${amiuser}" == "X" ];then
	while [ "X${amiuser}" == "X" ]
	do
	  echo -n "AMI User name:"
	  read amiuser	  
	  if [ "X${amiuser}" == "X" ]
	  then
		echo "error: AMI user name can not be blank"
	  fi
	done
else
	echo "AMI user is $amiuser";
fi

if [ "X${amisecret}" == "X" ];then
	while [ "X${amisecret}" == "X" ]
	do
	  echo -n "AMI Secret:"
	  read amisecret	  
	  if [ "X${amisecret}" == "X" ]
	  then
		echo "error: AMI secret name can not be blank"
	  fi
	done
else
	echo "AMI Secret is $amisecret";
fi

####modify config file####
#for astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/dbhost.*/dbhost = '${dbhost}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/dbport.*/dbport = '${dbport}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/dbname.*/dbname = '${dbname}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/username.*/username = '${dbuser}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/password.*/password = '${dbpasswd}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[database\]/,/\[asterisk\]/s/prefix.*/prefix = '${dbtableprefix}'/1' ${curpath}/scripts/astercc.conf

sed -i '/\[asterisk\]/,/\[system\]/s/server.*/server = '${astserver}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[asterisk\]/,/\[system\]/s/port.*/port = '${amiport}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[asterisk\]/,/\[system\]/s/username.*/username = '${amiuser}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[asterisk\]/,/\[system\]/s/secret.*/secret = '${amisecret}'/1' ${curpath}/scripts/astercc.conf

sed -i '/\[statistics\]/,/\[asterccc\]/s/dbhost.*/dbhost = '${dbhost}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[statistics\]/,/\[asterccc\]/s/dbport.*/dbport = '${dbport}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[statistics\]/,/\[asterccc\]/s/dbname.*/dbname = '${dbname}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[statistics\]/,/\[asterccc\]/s/dbuser.*/dbuser = '${dbuser}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[statistics\]/,/\[asterccc\]/s/dbpassword.*/dbpassword = '${dbpasswd}'/1' ${curpath}/scripts/astercc.conf
sed -i '/\[statistics\]/,/\[asterccc\]/s/prefix.*/prefix = '${dbtableprefix}'/1' ${curpath}/scripts/astercc.conf

if [ "X${dbtableprefix}" != "X" ];
then
  sed -i '/\[database\]/,/\[asterisk\]/s/prefix.*/prefix = '${dbtableprefix}'/1' ${curpath}/scripts/astercc.conf
  sed -i '/var $default/,/encoding/s/'\'prefix\''.*/'\'prefix\'' => '\'${dbtableprefix}\''\,/1' ${curpath}/app/config/database.php
fi

#for app database config
sed -i '/var $default/,/encoding/s/'\'host\''.*/'\'host\'' => '\'${dbhost}\''\,/1' ${curpath}/app/config/database.php
sed -i '/var $default/,/encoding/s/'\'login\''.*/'\'login\'' => '\'${dbuser}\''\,/1' ${curpath}/app/config/database.php
sed -i '/var $default/,/encoding/s/'\'password\''.*/'\'password\'' => '\'${dbpasswd}\''\,/1' ${curpath}/app/config/database.php
sed -i '/var $default/,/encoding/s/'\'database\''.*/'\'database\'' => '\'${dbname}\''\,/1' ${curpath}/app/config/database.php

if [ "X${mainpath}" == "X" ];then
	echo Please enter install directory for astercc
	echo -n "astercc directory(default /var/www/html/asterCC):"
	read mainpath

	if [ "X${mainpath}" == "X" ];
	then
	  mainpath='/var/www/html/asterCC'
	fi
fi
echo "astercc install path is $mainpath"
mkdir $mainpath -p
apppath=${mainpath////\\/}

sed -i '/\[system\]/,/\[dialer\]/s/apppath.*/apppath = '${apppath}'\/app/1' ${curpath}/scripts/astercc.conf
sed -i '/\[backup\]/,/*.*/s/localpath.*/localpath = '${apppath}'\/app\/tmp\/backup/1' ${curpath}/scripts/astercc.conf



mkdir -p ${mainpath}
cp -Rf ${curpath}/app ${mainpath}
cp -Rf ${curpath}/cake ${mainpath}
cp -Rf ${curpath}/data ${mainpath}
cp -Rf ${curpath}/statistics ${mainpath}

#for modules
cp -Rf ${curpath}/modules/* ${mainpath}/data/_cache
ln -s ${mainpath}/data/monitor_download ${mainpath}/app/webroot/monitor_webdownload

mkdir -p ${mainpath}/sql
cp $curpath/sql/default_settings_en_n.sql  ${mainpath}/sql/default_settings_en.sql
cp $curpath/sql/default_settings_cn_n.sql  ${mainpath}/sql/default_settings_cn.sql
cp $curpath/sql/demo_en_n.sql  ${mainpath}/sql/demo_en.sql
cp $curpath/sql/demo_cn_n.sql  ${mainpath}/sql/demo_cn.sql

chmod -R 777 ${mainpath}/sql

mkdir -p /usr/share/fonts/default/ghostscript
cp -f $curpath/data/calendar/arial.ttf  /usr/share/fonts/default/ghostscript/
cp -f $curpath/data/calendar/simkai.ttf  /usr/share/fonts/default/ghostscript/

#for database init
cp -f  ${curpath}/sql/db-init.sql ${mainpath}/app
echo "USE ${dbname};" > ${mainpath}/app/db-init.sql
echo "INSERT INTO ${dbtableprefix}customerrate_buffers SELECT * from ${dbtableprefix}customerrates;" >> ${mainpath}/app/db-init.sql
echo "INSERT INTO ${dbtableprefix}teamrate_buffers SELECT * from ${dbtableprefix}teamrates;" >> ${mainpath}/app/db-init.sql
echo "INSERT INTO ${dbtableprefix}systemrate_buffers SELECT * from ${dbtableprefix}systemrates;" >> ${mainpath}/app/db-init.sql
echo "INSERT INTO ${dbtableprefix}agentrate_buffers SELECT * from ${dbtableprefix}agentrates;" >> ${mainpath}/app/db-init.sql
echo "UPDATE ${dbtableprefix}settings SET itemvalue='yes' where item='init';" >> ${mainpath}/app/db-init.sql
echo "INSERT INTO ${dbtableprefix}curmeetmerecords SET id='10', agentno='0001', queueno='01',team_id='0',sessionid='0000';" >> ${mainpath}/app/db-init.sql
echo "DELETE FROM ${dbtableprefix}curmeetmerecords;" >> ${mainpath}/app/db-init.sql
chmod -R 644 ${mainpath}

mkdir -p /var/lib/mysql/
cp -f ${mainpath}/app/db-init.sql /var/lib/mysql/
chmod 777 /var/lib/mysql/db-init.sql

#${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < ${mainpath}/app/db-init.sql

#change dir permissions.
for chpath in `find $mainpath -type d`
do
  chmod 755 ${chpath}
done

if [[ -f "/etc/debian-version" ]] || [[ -f "/etc/debian_version" ]]
then
	chown -R asterisk.asterisk /etc/php5/fpm/php.ini
	ln -s /etc/php5/fpm/php.ini /etc/php.ini
	chown -R asterisk.asterisk /etc/php.ini
else
	chown -R asterisk.asterisk /etc/php.ini
fi

chown -R asterisk.asterisk ${mainpath}
chmod -R 777 ${mainpath}/app/tmp
chmod -R 777 ${mainpath}/data
chmod -R 777 ${mainpath}/app/webroot/files
chmod -R 777 ${mainpath}/app/webroot/news
chmod -R 777 ${mainpath}/app/webroot/img/picture
chmod -R 777 ${mainpath}/app/webroot/img/teamlogo
chmod -R 777 ${mainpath}/app/locale
chmod +x $mainpath/cake/console/cake

daemonpath=/opt/asterisk/scripts/astercc
mkdir -p ${daemonpath}
bit=`getconf LONG_BIT`
if [ $bit == 32 ]; then
	cp -Rf ${curpath}/scripts/32/* ${daemonpath}
	cp -Rf ${curpath}/data/calendar/wkhtmltoimage_i386  ${mainpath}/data/calendar/wkhtmltoimage
	cp -Rf ${curpath}/data/calendar/wkhtmltopdf_i386  ${mainpath}/data/calendar/wkhtmltopdf
else
	cp -Rf ${curpath}/scripts/64/* ${daemonpath}
	cp -Rf ${curpath}/scripts/64/ffmpeg /usr/bin/ffmpeg
	chmod 777 /usr/bin/ffmpeg
	cp -Rf ${curpath}/scripts/64/libmsc.so /usr/lib64/libmsc.so
	chmod 744 /usr/lib64/libmsc.so
	ldconfig
fi
cp ${curpath}/scripts/* ${daemonpath} -f >/dev/null 2>&1
#cp ${curpath}/scripts/lib ${daemonpath} -Rf

chmod +x ${daemonpath}/* 
chown -R asterisk.asterisk ${daemonpath}/
echo "0" > ${daemonpath}/processmonitorlastid
if [ -f "/etc/astercc.conf" ]
then
mv -f /etc/astercc.conf /etc/astercc.conf.bak
fi
ln -s ${daemonpath}/astercc.conf /etc/astercc.conf

if [ -f "/etc/my.cnf" ]
then
  #sed  -i '/\[mysqld\]/,/\[mysqld_safe\]/s/init-file/#&/' /etc/my.cnf
  sed -i '/init-file/d' /etc/my.cnf
  sed -i '/\[mysqld_safe\]/iinit-file=/var/lib/mysql/db-init.sql' /etc/my.cnf
else
  if [ -d "/etc/mysql/conf.d/" ]
  then
      touch /etc/mysql/conf.d/astercc.cnf
      echo "[mysqld]" > /etc/mysql/conf.d/astercc.cnf
      echo "init-file=/var/lib/mysql/db-init.sql" >> /etc/mysql/conf.d/astercc.cnf
  else
      echo "Warnning: can not find my.cnf in /etc, you should set the 'init-file=/var/lib/mysql/db-init.sql' in your my.cnf mysqld settings "
  fi
fi


#astetcdir=`sed -n '/\[system]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep asterccc[\ ]*=> |grep -v "^\ *#" |cut -d= -f2 |tr -d " "`

asterisketcpath=${asterisketc////\\/}
sed -i 's/define('\'ASTERRISK_CONF_DIR\'',.*/define('\'ASTERRISK_CONF_DIR\'','\'$asterisketcpath\'')\;/1' ${curpath}/app/config/core.php

mv -f $mainpath/app/hostedetc/sip_general_astercc.conf ${asterisketc}
#for asterisk conf
for astconf in `ls $mainpath/app/hostedetc`
do
  if [ -L "${asterisketc}/${astconf}" -o -f "${asterisketc}/${astconf}" ]
  then
    mv -f ${asterisketc}/${astconf} "${asterisketc}/${astconf}.astercc_bak"	
  fi
  ln -s $mainpath/app/hostedetc/${astconf} ${asterisketc}/${astconf}
done

#mv ${asterisketc}/modules.conf ${asterisketc}/modules.conf.astercc_bak
cp -rf ${curpath}/scripts/modules.conf /etc/asterisk
cp -rf ${curpath}/scripts/sms_server.conf /opt/asterisk/scripts/astercc/sms_server.conf

touch ${asterisketc}/sip_hostedpbx_registrations.conf
touch ${asterisketc}/sip_hostedpbx_devices.conf
touch ${asterisketc}/sip_hostedpbx_trunks.conf
touch ${asterisketc}/sip_custom.conf

touch ${asterisketc}/iax_hostedpbx_registrations.conf
touch ${asterisketc}/iax_hostedpbx_devices.conf
touch ${asterisketc}/iax_hostedpbx_trunks.conf
#touch ${asterisketc}/iax_hylafax_faxdevices.conf
touch ${asterisketc}/iax_custom.conf

touch ${asterisketc}/mgcp_hostedpbx_devices.conf
touch ${asterisketc}/mgcp_custom.conf
touch ${asterisketc}/skype_hostedpbx_trunks.conf

touch ${asterisketc}/musiconhold_hostedpbx.conf
touch ${asterisketc}/musiconhold_custom.conf
touch ${asterisketc}/voicemail_hostedpbx.conf
touch ${asterisketc}/voicemail_custom.conf

touch ${asterisketc}/queues_hostedpbx.conf
touch ${asterisketc}/queues_custom.conf

#cp $mainpath/app/hostedetc/extensions_blf.conf ${asterisketc}/extensions_blf.conf
touch ${asterisketc}/extensions_custom.conf
touch ${asterisketc}/chan_dahdi_trunks.conf
touch ${asterisketc}/chan_dahdi_devices.conf

touch ${asterisketc}/featurecodes_hostedpbx.conf
touch ${asterisketc}/featurecodes_applicationmap_hostedpbx.conf
echo ";;;This conf is auto generated by hostedpbx, don't modify!" > ${asterisketc}/featurecodes_applicationmap_hostedpbx.conf
echo "agentConsult => *55,self/callee,Macro,agentConsult" >> ${asterisketc}/featurecodes_applicationmap_hostedpbx.conf
echo "agentConsultDO => *55,self/caller,Macro,agentConsult" >> ${asterisketc}/featurecodes_applicationmap_hostedpbx.conf

chown -R asterisk:asterisk ${asterisketc}

mkdir -p /var/lib/asterisk/sounds/cn
cp -Rf $curpath/sounds/cn/* /var/lib/asterisk/sounds/cn
mkdir -p /var/lib/asterisk/sounds/en
cp -Rf $curpath/sounds/en/* /var/lib/asterisk/sounds/en

if [ -d "/var/lib/asterisk/sounds/announcement_default/" ];then
	rm -rf /var/lib/asterisk/sounds/announcement_default
fi

if [ "${defaultlang}" == "en" ];then
    ln -s /var/lib/asterisk/sounds/en/announcement_default/ /var/lib/asterisk/sounds/announcement_default
 else
   ln -s /var/lib/asterisk/sounds/cn/announcement_default/ /var/lib/asterisk/sounds/announcement_default
fi
chown -R asterisk:asterisk /var/lib/asterisk/sounds

echo "SETTING FILE PERMISSIONS...";

astmoddir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astmoddir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astmoddir}" != "X" ];then
	if [ -d "${astmoddir}/" ]
	  then
		chown -R asterisk:asterisk ${astmoddir}
	fi
fi

astvarlibdir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astvarlibdir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astvarlibdir}" != "X" ];then
	if [ -d "${astvarlibdir}/" ]
	  then
		chown -R asterisk:asterisk ${astvarlibdir}
	fi
fi

astdbdir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astdbdir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astdbdir}" != "X" ];then
	if [ -d "${astdbdir}/" ]
	  then
		chown -R asterisk:asterisk ${astdbdir}
	fi
fi

astkeydir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astkeydir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astkeydir}" != "X" ];then
	if [ -d "${astkeydir}/" ]
	  then
		chown -R asterisk:asterisk ${astkeydir}
	fi
fi

astdatadir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astdatadir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astdatadir}" != "X" ];then
	if [ -d "${astdatadir}/" ]
	  then
		chown -R asterisk:asterisk ${astdatadir}
	fi
fi

astagidir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astagidir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astagidir}" != "X" ];then
	if [ -d "${astagidir}/" ]
	  then
		chown -R asterisk:asterisk ${astagidir}
	fi
fi

astspooldir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astspooldir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astspooldir}" != "X" ];then
	if [ -d "${astspooldir}/" ]
	  then
		chown -R asterisk:asterisk ${astspooldir}
	fi
fi

astrundir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astrundir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astrundir}" != "X" ];then
	if [ -d "${astrundir}/" ]
	  then
		chown -R asterisk:asterisk ${astrundir}
	fi
fi

astlogdir=`sed -n '/\[directories]/,/\[.*\]/p' ${asterisketc}/asterisk.conf |grep astlogdir[\ ]*=\> |grep -v "^\ *#" |cut -d\> -f2 |tr -d " "`

if [ "X${astlogdir}" != "X" ];then
	if [ -d "${astlogdir}/" ]
	  then
		chown -R asterisk:asterisk ${astlogdir}
	fi
fi

#if [ -d "/var/lib/asterisk/moh" ]
#then
#	mv /var/lib/asterisk/moh  /var/lib/asterisk/moh.bak
#	if [ ! -e /var/lib/asterisk/mohmp3 ]
#	then
#		ln -s /var/lib/asterisk/mohmp3 /var/lib/asterisk/moh
#	fi
#fi

#echo Please choose the system default language 
#echo -n "en or cn (default en):"
#read defaultlang

#if [ "X${defaultlang}" == "X" ];
#then
#  defaultlang="en"
#else
#  defaultlang=`echo $defaultlang |tr [:upper:] [:lower:]`
#  if [ "X${defaultlang}" != "Xen" -a "X${defaultlang}" != "Xcn" ];
#  then
#    defaultlang="en"
#  fi
#fi

#if [ "X${defaultlang}" != "Xen" ];then
#  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} -e "USE ${dbname}; UPDATE ${dbtableprefix}settings SET itemvalue='cn' WHERE item='systemlanguage';"
#  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} -e "USE ${dbname}; UPDATE ${dbtableprefix}settings SET itemvalue='2' WHERE item='announcementlanguage';"
#fi

asteriskinit=''


#if [ $RELEASEVER -eq 7 ]; then
if [ -f /etc/init.d/asterisk ];then
	mv /etc/init.d/asterisk ~/asterisk.ccbak
fi
	  cat > /lib/systemd/system/asterisk.service << EOF
[Unit]
Description=Asterisk PBX and telephony daemon
Documentation=man:asterisk(8)
Wants=network.target
After=network.target

[Service]
Type=simple
User=asterisk
Group=asterisk
RuntimeDirectory=asterisk
RuntimeDirectoryMode=0755
Environment=HOME=/var/lib/asterisk
WorkingDirectory=/var/lib/asterisk
ExecStart=/usr/sbin/asterisk -f -C /etc/asterisk/asterisk.conf
ExecStop=/usr/sbin/asterisk -rx 'core stop now'
ExecReload=/usr/bin/asterisk -rx 'core reload'

# safe_asterisk emulation
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF
	asteriskinit='centos7';
	systemctl daemon-reload
	systemctl enable asterisk
#else
	#if [ -f /etc/init.d/asterisk ];then
#		echo "Setting asterisk run as user:asterisk/group:asterisk"
	#	asteriskinit=/etc/init.d/asterisk
	#	sed -i 's/^[#\ ]*AST_USER.*/AST_USER="asterisk"/' /etc/init.d/asterisk
	#	sed -i 's/^[#\ ]*AST_GROUP.*/AST_GROUP="asterisk"/' /etc/init.d/asterisk
	#else
	#	echo "Can't find asterisk init.d script in /etc/init.d/"
	#	echo "Please ensure that asterisk running as user:asterisk/group:asterisk"
	#fi
#fi

#for crontab config

if [ -f "/etc/redhat-release" ]
then
   sed -i '/^[^#].*.-app.*.checkonline.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app checkonline >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.astcc_backup\.sh.*/d' /var/spool/cron/root
   echo "0 * * * * $daemonpath/astcc_backup.sh >/dev/null 2>&1" >> /var/spool/cron/root
   #if [ $RELEASEVER -eq 7 ]; then
		#sed -i '/^[^#].*.systemctl restart astercc.*/d' /var/spool/cron/root
   		#echo "0 3 * * * systemctl restart astercc " >> /var/spool/cron/root
   #else
		#sed -i '/^[^#].*.asterccd.*/d' /var/spool/cron/root
   		#echo "0 3 * * * $daemonpath/asterccd restart >/dev/null 2>&1" >> /var/spool/cron/root
   #fi
   sed -i '/^[^#].*.processmonitors.*/d' /var/spool/cron/root
   echo "30 12 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/root
   echo "30 20 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/root
   echo "30 23 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.monitortar.*/d' /var/spool/cron/root
   echo "* * * * * export LANG=en_US.UTF-8; $mainpath/cake/console/cake -app $mainpath/app monitortar >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.backupmonitor.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app backupmonitor >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.export.*/d' /var/spool/cron/root
   echo "* * * * * export LANG=en_US.UTF-8; $mainpath/cake/console/cake -app $mainpath/app export >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.asterccimport.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app asterccimport >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.movepbxcdr.*/d' /var/spool/cron/root
   #echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app movepbxcdr >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.calculate.*/d' /var/spool/cron/root
   echo "*/5 * * * * $mainpath/cake/console/cake -app $mainpath/app calculate >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.mailman.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app mailman >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.campaignpackgetask.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app campaignpackgetask >/dev/null 2>&1" >> /var/spool/cron/root 
   sed -i '/^[^#].*.-app.*.blacklistfilter.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app blacklistfilter >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.asterccfilter.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app asterccfilter >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.nighttask.*/d' /var/spool/cron/root
   echo "10 2 * * * $mainpath/cake/console/cake -app $mainpath/app nighttask >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.bill.*/d' /var/spool/cron/root
   echo "10 1 * * * $mainpath/cake/console/cake -app $mainpath/app bill >/dev/null 2>&1" >> /var/spool/cron/root   
   sed -i '/^[^#].*.-app.*.voicemail.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app voicemail >/dev/null 2>&1" >> /var/spool/cron/root   
   sed -i '/^[^#].*.agent_report.*/d' /var/spool/cron/root
   echo "10 0 * * * $daemonpath/agent_report >/dev/null 2>&1" >> /var/spool/cron/root
   sed -i '/^[^#].*.agentgroup_report.*/d' /var/spool/cron/root
   echo "20 0 * * * $daemonpath/agentgroup_report >/dev/null 2>&1" >> /var/spool/cron/root
   echo "* * * * * $daemonpath/astcc_historydata -d" >> /var/spool/cron/root
   sed -i '/^[^#].*.-app.*.asterccdelete.*/d' /var/spool/cron/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app asterccdelete >/dev/null 2>&1" >> /var/spool/cron/root   

   sed -i 's/^[^#].*.requiretty.*/#&/' /etc/sudoers
   sed -i '/^[^#].*.\/etc\/init.d\/asterisk.*/d' /etc/sudoers
   sed -i '/^[^#].*.asterccd.*/d' /etc/sudoers
   sed -i '/^[^#].*.asterccc.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_datamover.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_dialer.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_realtime.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_autodialer.*/d' /etc/sudoers
   sed -i '/^[^#].*.network.*/d' /etc/sudoers
   sed -i '/^[^#].*.mysqld.*/d' /etc/sudoers
   sed -i '/^[^#].*.php-fpm.*/d' /etc/sudoers
   sed -i '/^[^#].*.ntpd.*/d' /etc/sudoers
   sed -i '/^[^#].*.ntpdate.*/d' /etc/sudoers
   sed -i '/^[^#].*.\/etc\/init.d\/ntpdate.*/d' /etc/sudoers
   sed -i '/^[^#].*.date.*/d' /etc/sudoers
   sed -i '/^[^#].*.hwclock.*/d' /etc/sudoers
   sed -i '/^[^#].*.zoneinfo.*/d' /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/asterisk" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/asterccd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/asterccd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/asterccc" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_datamover" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_dialer" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_realtime" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_autodialer" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/network" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/mysqld" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/php-fpm" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/ntpd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/sbin/ntpdate" >> /etc/sudoers
   #echo "asterisk ALL=NOPASSWD :/etc/init.d/ntpdate" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/bin/date" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/sbin/hwclock" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/bin/cp /usr/share/zoneinfo/* /etc/localtime" >> /etc/sudoers
   echo "asterisk ALL = NOPASSWD: /usr/bin/reboot" >> /etc/sudoers
   echo "asterisk ALL = NOPASSWD: /sbin/shutdown" >> /etc/sudoers
   echo "asterisk ALL = NOPASSWD: /usr/bin/systemctl" >> /etc/sudoers 
   echo "asterisk ALL = NOPASSWD: /usr/bin/timedatectl" >> /etc/sudoers
   #chmod 777 /etc/sysconfig/network-scripts/ifcfg-eth*
   #chmod 777 /etc/sysconfig/network-scripts
   chmod 777 /etc/NetworkManager/system-connections

else
   sed -i '/^[^#].*.-app.*.checkonline.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app checkonline >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.astcc_backup\.sh.*/d' /var/spool/cron/crontabs/root   
   echo "0 2 * * * $daemonpath/astcc_backup\.sh >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.asterccd.*/d' /var/spool/cron/crontabs/root
   echo "0 3 * * * $daemonpath/asterccd restart >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.processmonitors.*/d' /var/spool/cron/crontabs/root
   echo "30 12 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   echo "30 20 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   echo "30 23 * * * $daemonpath/processmonitors >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.monitortar.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * export LANG=en_US.UTF-8; $mainpath/cake/console/cake -app $mainpath/app monitortar >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.backupmonitor.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app backupmonitor >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.export.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * export LANG=en_US.UTF-8; $mainpath/cake/console/cake -app $mainpath/app export >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.asterccimport.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app asterccimport >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.movepbxcdr.*/d' /var/spool/cron/crontabs/root
   #echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app movepbxcdr >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.calculate.*/d' /var/spool/cron/crontabs/root
   echo "*/5 * * * * $mainpath/cake/console/cake -app $mainpath/app calculate >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.mailman.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app mailman >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.campaignpackgetask.*/d' /var/spool/cron/crontabs/root
   echo "*/5 * * * * $mainpath/cake/console/cake -app $mainpath/app campaignpackgetask >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.blacklistfilter.*/d' /var/spool/cron/crontabs/root
   echo "*/5 * * * * $mainpath/cake/console/cake -app $mainpath/app blacklistfilter >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.asterccfilter.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app asterccfilter >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.nighttask.*/d' /var/spool/cron/crontabs/root
   echo "10 2 * * * $mainpath/cake/console/cake -app $mainpath/app nighttask >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.bill.*/d' /var/spool/cron/crontabs/root
   echo "10 1 * * * $mainpath/cake/console/cake -app $mainpath/app bill >/dev/null 2>&1" >> /var/spool/cron/crontabs/root   
   sed -i '/^[^#].*.-app.*.voicemail.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $mainpath/cake/console/cake -app $mainpath/app voicemail >/dev/null 2>&1" >> /var/spool/cron/crontabs/root   
   sed -i '/^[^#].*.agent_report.*/d' /var/spool/cron/crontabs/root
   echo "10 0 * * * $daemonpath/agent_report >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.agentgroup_report.*/d' /var/spool/cron/crontabs/root
   echo "20 0 * * * $daemonpath/agentgroup_report >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.astcc_historydata.*/d' /var/spool/cron/crontabs/root
   echo "* * * * * $daemonpath/astcc_historydata -d" >> /var/spool/cron/crontabs/root
   sed -i '/^[^#].*.-app.*.asterccdelete.*/d' /var/spool/cron/crontabs/root
   echo "*/5 * * * * $mainpath/cake/console/cake -app $mainpath/app asterccdelete >/dev/null 2>&1" >> /var/spool/cron/crontabs/root
   
   chown root:crontab /var/spool/cron/crontabs/root
   chmod 600 /var/spool/cron/crontabs/root
   
   #touch /etc/sudoers.d/astercc.sudoers
   sed -i 's/^[^#].*.requiretty.*/#&/' /etc/sudoers
   sed -i '/^[^#].*.\/etc\/init.d\/asterisk.*/d' /etc/sudoers
   sed -i '/^[^#].*.asterccd.*/d' /etc/sudoers
   sed -i '/^[^#].*.asterccc.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_datamover.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_dialer.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_realtime.*/d' /etc/sudoers
   sed -i '/^[^#].*.astcc_autodialer.*/d' /etc/sudoers
   sed -i '/^[^#].*.mysqld.*/d' /etc/sudoers
   sed -i '/^[^#].*.php-fpm.*/d' /etc/sudoers
   sed -i '/^[^#].*.ntpd.*/d' /etc/sudoers
   sed -i '/^[^#].*.ntpdate.*/d' /etc/sudoers
   sed -i '/^[^#].*.\/etc\/init.d\/ntpdate.*/d' /etc/sudoers
   sed -i '/^[^#].*.date.*/d' /etc/sudoers
   sed -i '/^[^#].*.hwclock.*/d' /etc/sudoers
   sed -i '/^[^#].*.zoneinfo.*/d' /etc/sudoers   
   echo "asterisk ALL=NOPASSWD :/etc/init.d/asterisk" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/etc/init.d/asterccd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/asterccd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/asterccc" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_datamover" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_dialer" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_realtime" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/opt/asterisk/scripts/astercc/astcc_autodialer" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/etc/init.d/mysqld" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/etc/init.d/php-fpm" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/etc/init.d/ntpd" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/sbin/ntpdate" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/etc/init.d/ntpdate" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/bin/date" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/sbin/hwclock" >> /etc/sudoers
   echo "asterisk ALL=NOPASSWD :/bin/cp /usr/share/zoneinfo/* /etc/localtime" >> /etc/sudoers 
   echo "asterisk ALL = NOPASSWD: /sbin/reboot" >> /etc/sudoers
   echo "asterisk ALL = NOPASSWD: /sbin/shutdown" >> /etc/sudoers   

fi

#echo "Do you want to auto convert wav monitor records to mp3 format every hour?"
#echo -n "Press 'y' to auto convert:"
#read monitorconvertflag

#if [ "X${monitorconvertflag}" == "Xy" -o "X${monitorconvertflag}" == "XY" ]
#then
#  if [ -f "/etc/redhat-release" ]
#  then
#        echo "1 * * * * ${daemonpath}/processmonitors.pl -d" >> /var/spool/cron/root
#  else
#        echo "1 * * * * ${daemonpath}/processmonitors.pl -d" >> /var/spool/cron/crontabs/root
#        chown root:crontab /var/spool/cron/crontabs/root
#        chmod 600 /var/spool/cron/crontabs/root
#  fi
#fi
chmod 777 /var/run
mkdir -p /var/log/php-crontab
touch /var/log/php-crontab/campaign_schduler.log
chmod -R 777 /var/log/php-crontab
#echo "Do you want to load demo data to new astercc"
#echo -n "Press 'y' to load demo data:"
#read demoflag

if [ "X${demoflag}" == "Xy" -o "X${demoflag}" == "XY" ]
then
  ${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < $curpath/sql/demo_${defaultlang}_n.sql
  $mainpath/cake/console/cake -app $mainpath/app device >/dev/null 2>&1
  $mainpath/cake/console/cake -app $mainpath/app trunk >/dev/null 2>&1
  $mainpath/cake/console/cake -app $mainpath/app queue >/dev/null 2>&1
  $mainpath/cake/console/cake -app $mainpath/app featruecode >/dev/null 2>&1

  if [ "${defaultlang}" == "en" ];then
	if [ -d "/var/lib/asterisk/sounds/en/announcement_default/" ];then
		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_1.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_1.wav
		fi

		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_2.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_2.wav
		fi

		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_3.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_3.wav
		fi

		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_4.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_4.wav
		fi
	else
		mkdir -p /var/lib/asterisk/sounds/en/announcement_default/
	fi

    cp ${curpath}/sounds/en/announcement_1.wav /var/lib/asterisk/sounds/en/announcement_default/
    cp ${curpath}/sounds/en/announcement_2.wav /var/lib/asterisk/sounds/en/announcement_default/
	cp ${curpath}/sounds/en/announcement_3.wav /var/lib/asterisk/sounds/en/announcement_default/
	cp ${curpath}/sounds/en/announcement_4.wav /var/lib/asterisk/sounds/en/announcement_default/

    if [ -d "/var/lib/asterisk/sounds/announcement_default/" ];then
		rm -rf /var/lib/asterisk/sounds/announcement_default
    fi

    ln -s /var/lib/asterisk/sounds/en/announcement_default/ /var/lib/asterisk/sounds/announcement_default
     
    if [ -d "/var/lib/asterisk/sounds/cn/announcement_default/" ];then
		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav
		fi
		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav
		fi
		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_4.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_4.wav
		fi
	else
        mkdir -p /var/lib/asterisk/sounds/cn/announcement_default/
    fi

    cp ${curpath}/sounds/cn/announcement_1.wav /var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav
	cp ${curpath}/sounds/cn/announcement_3.wav /var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav
	cp ${curpath}/sounds/cn/announcement_4.wav /var/lib/asterisk/sounds/cn/announcement_default/announcement_4.wav

	cp -Rf ${curpath}/sounds/cn/digits /var/lib/asterisk/sounds/cn/

  else
    if [ -d "/var/lib/asterisk/sounds/cn/announcement_default/" ];then
		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_1.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_1.wav
		fi

		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav
		fi

		if [ -f "/var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav" ];then
			rm -f /var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav
		fi
	else
		mkdir -p /var/lib/asterisk/sounds/cn/announcement_default/
	fi

    cp ${curpath}/sounds/cn/announcement_1.wav /var/lib/asterisk/sounds/cn/announcement_default/
	cp ${curpath}/sounds/cn/announcement_3.wav /var/lib/asterisk/sounds/cn/announcement_default/announcement_2.wav
	cp ${curpath}/sounds/cn/announcement_4.wav /var/lib/asterisk/sounds/cn/announcement_default/announcement_3.wav

    if [ -d "/var/lib/asterisk/sounds/announcement_default/" ];then
		rm -rf /var/lib/asterisk/sounds/announcement_default
    fi
    ln -s /var/lib/asterisk/sounds/cn/announcement_default/ /var/lib/asterisk/sounds/announcement_default
     
    if [ -d "/var/lib/asterisk/sounds/en/announcement_default/" ];then
		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_1.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_1.wav
		fi
		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_2.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_2.wav
		fi
		if [ -f "/var/lib/asterisk/sounds/en/announcement_default/announcement_3.wav" ];then
			rm -f /var/lib/asterisk/sounds/en/announcement_default/announcement_3.wav
		fi
	else
        mkdir -p /var/lib/asterisk/sounds/en/announcement_default/
    fi

    cp ${curpath}/sounds/en/announcement_2.wav /var/lib/asterisk/sounds/en/announcement_default/announcement_1.wav
	cp ${curpath}/sounds/en/announcement_3.wav /var/lib/asterisk/sounds/en/announcement_default/announcement_2.wav
	cp ${curpath}/sounds/en/announcement_4.wav /var/lib/asterisk/sounds/en/announcement_default/announcement_3.wav

	cp -Rf ${curpath}/sounds/cn/digits /var/lib/asterisk/sounds/cn/

  fi
  chown -R asterisk:asterisk /var/lib/asterisk/sounds
fi

${dbbin}/mysql --host=${dbhost} --port=${dbport} -u${dbuser} ${dbpasswdstr} ${dbname} < ${mainpath}/app/db-init.sql


# try load g729
asterisk_module_dir='/usr/lib/asterisk/modules'
if [ ! -e $asterisk_module_dir ]
then
	asterisk_module_dir='/usr/lib64/asterisk/modules'
fi

if [ $bit == 32 ]; then
	cp -Rf ${curpath}/scripts/32/codec_g729-ast18-gcc4-glibc-pentium4.so ${asterisk_module_dir}
	load_result=`asterisk -rx "module load codec_g729-ast18-gcc4-glibc-pentium4.so"`
	if [ "$load_result" == "Loaded codec_g729-ast18-gcc4-glibc-pentium4.so" ]
	then
		echo "load => codec_g729-ast18-gcc4-glibc-pentium4.so" >> ${asterisketc}/modules.conf
		echo "codec g729 loaded"
	fi
else
	cp -Rf ${curpath}/scripts/64/codec_g729-ast18-gcc4-glibc-x86_64-pentium4.so ${asterisk_module_dir}
	load_result=`asterisk -rx "module load codec_g729-ast18-gcc4-glibc-x86_64-pentium4.so"`
	if [ "$load_result" == "Loaded codec_g729-ast18-gcc4-glibc-x86_64-pentium4.so" ]
	then
		echo "load => codec_g729-ast18-gcc4-glibc-x86_64-pentium4.so" >> ${asterisketc}/modules.conf
		echo "codec g729 loaded"
	fi
fi


# get rid of logger
sed -i "s/\;queue_log = no/queue_log = no/" ${asterisketc}/logger.conf

if [ "X${asteriskinit}" != "X" ]
then
	#if [ $RELEASEVER -eq 7 ]; then
		systemctl restart asterisk
	#else
	#	echo "Restarting asterisk..."
	#	${asteriskinit} restart
	#fi
fi

# install modules
#echo "You can choose to install modules you need:"
#for line in `ls modules`
#do
#	cd modules/$line
#	./install.sh
#	cd ../..
#done


#echo "Do you want install some perl modules which is nesseary for mail function?(y/N):"
#read cpanflag
#if [ "X${cpanflag}" == "Xy" -o "X${cpanflag}" == "XY" ]
#then
#	cpan Config::IniFiles
#	cpan Module::Build::Compat
#	cpan MIME::Lite
#	cpan Net::SSLeay
#	cpan IO::Socket::SSL
#	cpan Authen::SASL
#	cpan Devel::CheckLib
#	cpan Email::Date::Format
#	#cpan Config::IniFiles
#	cpan MIME::Lite
#	cpan MIME::Base64
#	cpan Net::SMTP
#	cpan Net::SMTP::SSL
#	cpan Authen::SASL
#	cpan Authen::SASL::XS
#	cpan Encode
#	cpan Data::Dumper
#fi

if [ "X${autostartflag}" != "Xy" ];then
	echo "Do you want to auto start astercc daemon when system startup?"
	#echo "Must be redhat-release system"
	echo -n "Press 'y' to auto start:"
	read autostartflag
fi

if [ "X${autostartflag}" == "Xy" -o "X${autostartflag}" == "XY" ]
then
  if [ -f "/etc/redhat-release" ]
  then
  	#if [ $RELEASEVER -eq 7 ]; then
	  cat > /lib/systemd/system/astercc.service << EOF
[Unit]
Description=The asterCC Call center & CRM system
After=network.target
After=network-online.target
After=asterisk.service
After=redis.service
After=mariadb.service
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/asterccc.pid
ExecStart=/opt/asterisk/scripts/astercc/asterccd start
ExecReload=/opt/asterisk/scripts/astercc/asterccd restart
ExecStop=/opt/asterisk/scripts/astercc/asterccd stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

		systemctl daemon-reload
		systemctl enable astercc
	#else
	#	cp -f ${daemonpath}/asterccd /etc/rc.d/init.d
        #       chmod +x /etc/init.d/asterccd
	#	chkconfig --add asterccd
	#fi
  else
	sudo cp -f ${daemonpath}/asterccd /etc/init.d
	sudo chmod +x /etc/init.d/asterccd
	sudo update-rc.d asterccd start 99 2 3 4 5 . stop 10 0 1 6 .
        #echo "${daemonpath}/asterccd start" >> /etc/rc.local
  fi  
fi


if [ "X${asteriskinit}" != "X" ];then
	if [ "X${startflag}" != "Xy" ];then
		if [ "X${startflag}" != "Xy" ];then
			echo "Do you want to start asterCC daemon now?"
			echo -n "Press 'y' to start:"
			read startflag
		fi
	fi

	if [ "X${startflag}" == "Xy" -o "X${startflag}" == "XY" ]
	then
	  echo "starting asterccd..."
	  
	  #if [ $RELEASEVER -eq 7 ]; then
	  	systemctl start astercc
	  #else
	  #  /bin/bash ${daemonpath}/asterccd start
	  #fi
	fi
	echo "*****************************************************************"
	echo "*******************astercc install finished**********************"
	echo "*****Your astercc web directory at ${mainpath}/app/webroot.******"
	echo "*****Your astercc daemon directory at ${daemonpath}.***"
	echo "*****************************************************************"
	echo
else
	echo "*****************************************************************"
	echo "*******************astercc install finished**********************"
	echo "*****Your astercc web directory at ${mainpath}/app/webroot."
	echo "*****Your astercc daemon directory at ${daemonpath}."
	echo "*****Warnnign:astercc daemon is not running**********************"
	echo "-----you should restart asterisk as user:asterisk/group:asterisk-"
	if [ "X${autostartflag}" == "Xy" -o "X${autostartflag}" == "XY" ];then
		echo "----and start astercc daemon by '/etc/init.d/asterccd start'-----"
	else
		echo "--and start astercc daemon by '"${daemonpath}"/asterccd start'---"
	fi
	echo "*****************************************************************"
fi

exit

