#!/bin/bash
service ntpd stop
ntpdate $2
hwclock -w

# $1 本机hostname
# $2 主服务器hostname
# $3 主服务器ip
# $4 数据库ip
dbhost=$4

cat > /etc/ntp.conf << EOF
driftfile /var/lib/ntp/drift
server $2
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 127.127.1.0
fudge 127.127.1.0 stratum 10
EOF

service ntpd start

service nginx stop
chkconfig --level 345 nginx off
service php-fpm stop
chkconfig --level 345 php-fpm off
service mysqld stop
chkconfig --level 345 mysqld off

sed -i '/\[database\]/,/\[asterisk\]/s/dbhost.*/dbhost = '${dbhost}'/1' /etc/astercc.conf


cat > /etc/rsyncd.conf << EOF
uid=asterisk
gid=asterisk
use chroot=no
max connections=10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
secrets file = /etc/rsyncd/server.pass

[asterccsync]
path = /home/asterccsync
commet = stest file
ignore errors
read only = no
write only = no
hosts allow = $3
hosts deny = *
list = false
uid = asterisk
gid = asterisk
auth users = asterccsync
EOF

mkdir -p /etc/rsyncd/

echo "asterccsync:temp123" > /etc/rsyncd/server.pass

chmod -R 600 /etc/rsyncd/

#mkdir -p /home/asterccsync/
#cd /home/asterccsync/
#mv /etc/asterisk/ ./
#ln -s /home/asterccsync/asterisk/   /etc/asterisk
#mv /var/lib/asterisk/mohmp3  ./
#ln -s /home/asterccsync/mohmp3/   /var/lib/asterisk/mohmp3
#rm -rf /var/lib/asterisk/moh
#ln -s /home/asterccsync/mohmp3/   /var/lib/asterisk/moh
#mv /var/lib/asterisk/sounds/  ./
#ln -s /home/asterccsync/sounds/  /var/lib/asterisk/sounds
#mv /opt/asterisk/scripts/astercc/  ./
#ln -s /home/asterccsync/astercc/  /opt/asterisk/scripts/astercc
#mv /var/www/html/asterCC  ./
#ln -s /home/asterccsync/asterCC/  /var/www/html/asterCC
#mv /etc/iaxmodem/  ./
#ln -s /home/asterccsync/iaxmodem/  /etc/iaxmodem

cd /home/asterccsync
chown asterisk.asterisk ./* -R
cp /var/www/html/asterCC/app/views/agents/agentsxindesk.ctp asterCC/app/views/agents/
cp /opt/asterisk/scripts/astercc/*.astercc astercc

cd /var/www/html/
mv asterCC asterCC.bak
ln -s /home/asterccsync/asterCC ./

cd /var/lib/asterisk
mv mohmp3 mohmp3.bak
mv moh moh.bak
ln -s /home/asterccsync/mohmp3/ ./
ln -s /home/asterccsync/mohmp3/ ./moh
mv sounds/ sounds.bak
ln -s /home/asterccsync/sounds/ ./

cd /opt/asterisk/scripts/
mv astercc/ astercc.bak
ln -s /home/asterccsync/astercc/ ./

cd /etc/
mv  iaxmodem  iaxmodem.bak
ln -s  /home/asterccsync/iaxmodem   ./


cat >> /etc/astercc.conf << EOF
[localcheck]
interval=30
maxevent=10
service=asterccc,pbx
asterccc=/tmp/asterccc.pid
pbx=/var/run/asterisk/asterisk.pid
;mysqld=/var/run/mysqld/mysqld.pid
;nginx=/var/run/nginx.pid
;php-fpm=/var/run/php-fpm/php-fpm.pid
;http=80
EOF

echo "/usr/bin/rsync --daemon" >> /etc/rc.d/rc.local

sed -i '/\[system\]/,/\[dialer\]/s/^;astcc_localcheck.*/astcc_localcheck=\/opt\/asterisk\/scripts\/astercc\/astcc_localcheck/1' /etc/astercc.conf

/opt/asterisk/scripts/astercc/asterccc --RNI

service asterccd restart

/usr/bin/rsync --daemon