#!/bin/sh

DBNAME=$(/sbin/e-smith/db configuration getprop freepbx DbName)
backuppath='/home/e-smith/files/freepbx'

# check if backup module is enabled and running
# if not solve this...
if [[ ! `/usr/sbin/fwconsole ma list|/bin/grep backup|/bin/grep Enabled` ]] ; then

        if [[ ! `/usr/sbin/fwconsole ma list|/bin/grep backup`  ]] ;then
                /usr/sbin/fwconsole ma download backup -R standard -R extended 2>&1  >/dev/null
                /usr/sbin/fwconsole ma install backup -R standard -R extended 2>&1  >/dev/null
        fi

        if [[ `/usr/sbin/fwconsole ma list|/bin/grep backup| grep 'Not Installed'`  ]] ;then
                /usr/sbin/fwconsole ma install backup -R standard -R extended 2>&1  >/dev/null
        fi

        if [[ `/usr/sbin/fwconsole ma list|/bin/grep backup|/bin/grep Disabled` ]] ; then
                /usr/sbin/fwconsole ma enable backup 2>&1  >/dev/null
        fi

        /usr/sbin/fwconsole r
fi

# check our freepbx backup folder is there and accessible
if [[ ! -d $backuppath ]] ; then
        /bin/mkdir -p $backuppath
fi

# set first backup as the one for SME way
# set destination
mysql -e "use $DBNAME;UPDATE backup_server_details set value = '/home/e-smith/files/freepbx' WHERE server_id = 1;"
# set info for backup 1
#data = a:1:{s:10:"created_by";s:11:"install.php";}
mysql -e "use $DBNAME;INSERT INTO backup (\`id\`, \`name\`, \`description\` ,  \`immortal\` ,\`data\`, \`email\`   )  VALUES (1, 'Default backup', 'Default SME PBX backup; automatically installed', 'true','', 'admin' ) ON DUPLICATE KEY UPDATE   \`description\` = 'Default SME PBX backup; automatically installed',  \`immortal\` = 'true', \`email\` = 'admin' , \`name\`= 'Default backup' ;"

mysql -e "use $DBNAME;ALTER TABLE backup_details ADD UNIQUE uniq ( \`backup_id\` , \`key\` , \`index\` ) ;"
mysql -e "use $DBNAME;INSERT INTO backup_details (\`backup_id\`, \`key\`, \`index\`, \`value\`) VALUES
(1, 'delete_time_type', '', 'days'),
(1, 'delete_amount', '', '2'),
(1, 'desc', '', 'Default SME PBX backup; automatically installed'),
(1, 'cron_schedule', '', 'weekly'),
(1, 'cron_random', '', 'on'),
(1, 'cron_month', '', ''),
(1, 'cron_minute', '', '0'),
(1, 'cron_hour', '', '0'),
(1, 'cron_dow', '', '0'),
(1, 'cron_dom', '', ''),
(1, 'bu_server', '', '0'),
(1, 'delete_time', '', '0'),
(1, 'email', '', 'admin'),
(1, 'emailfailonly', '', '1'),
(1, 'storage_servers', '0', '1') ON DUPLICATE KEY UPDATE  \`value\`=VALUES(\`value\`) ;"
mysql -e "use $DBNAME;ALTER TABLE backup_details DROP INDEX uniq;"
# set what to backup
mysql -e "use $DBNAME;ALTER TABLE backup_items ADD UNIQUE \`uniq\` ( \`backup_id\` , \`type\` , \`path\` ( 15 ) ) ;"
mysql -e "use freepbxdb;INSERT INTO backup_items (\`backup_id\`, \`type\`, \`path\`, \`exclude\`) VALUES
(1, 'dir', '/etc/dahdi', 'a:0:{}'),
     (1, 'dir', '__ASTSPOOLDIR__/voicemail', 'a:0:{}'),
(1, 'mysql', 'server-2', 'a:0:{}'),
(1, 'astdb', 'astdb', 'a:0:{}'),
(1, 'mysql', 'server-3', 'a:0:{}'),
(1, 'dir', '__ASTETCDIR__', 'a:0:{}'),
(1, 'dir', '__AMPWEBROOT__', 'a:0:{}'),
(1, 'dir', '__AMPBIN__', 'a:0:{}'),
(1, 'dir', '/tftpboot', 'a:0:{}') ON DUPLICATE KEY UPDATE  \`exclude\`=VALUES(\`exclude\`) ;";
mysql -e "use $DBNAME;ALTER TABLE backup_items DROP INDEX \`uniq\` ;"

# make sure all perms are ok for backup
/usr/sbin/fwconsole chown 2>&1  >/dev/null

