#!/bin/bash
set -e
source /root/envsetup/global-conf.sh

apt-get install -y dialog build-essential

cd /root
rm -rf burp-custom
git clone https://panel.ubicast.eu/git/mediaserver/burp-custom.git burp-custom

# set conf
sed -i "s@^BURP_STATUS_IP=.*@BURP_STATUS_IP=${BURP_STATUS_IP}@" /root/burp-custom/burp-custom.sh
sed -i "s@^BURPUI_PASSWORD=.*@BURPUI_PASSWORD=${BURPUI_PASSWORD}@" /root/burp-custom/burp-custom.sh
sed -i "s@^BURP_SERVER=.*@BURP_SERVER=${BURP_SERVER}@" /root/burp-custom/burp-custom.sh
sed -i "s/^BURP_MAIL_DEST=.*/BURP_MAIL_DEST=${BURP_MAIL_DEST}/" /root/burp-custom/burp-custom.sh
sed -i "s@^BURP_CLIENT_NAME=.*@BURP_CLIENT_NAME=${BURP_CLIENT_NAME}@" /root/burp-custom/burp-custom.sh
sed -i 's@^echo "password = ${BURP_CLIENT_PASSWORD}".*@@' /root/burp-custom/burp-custom.sh
sed -i 's@^echo "restore_client = ${BURP_CLIENT_NAME}".*@@' /root/burp-custom/burp-custom.sh

# run burp-custom.sh install_fullserver
cd /root/burp-custom
bash /root/burp-custom/burp-custom.sh install_fullserver

# get admin_shell
mv /root/burp-custom/admin_shell.sh /home/admin/admin_shell.sh
# get MS_restore.sh
cp /root/burp-custom/MS_restore.sh /home/admin/MS_restore.sh

# MS_restore ajout clef ssh ??
sed -i "s@scp /root@scp -i /home/admin/.ssh/id_rsa /root@" /home/admin/MS_restore.sh
sed -i "s@ssh ${CLIENT}@ssh -i /home/admin/.ssh/id_rsa root\@${CLIENT}@" /home/admin/MS_restore.sh


# correct rights
echo "bash /home/admin/admin_shell.sh" > /home/admin/.bash_profile

# generate admin ssh key
if ( ! test -f /home/admin/.ssh/id_rsa ); then
	mkdir -p /home/admin/.ssh
	chown -R admin:admin /home/admin/.ssh
	su admin -c 'ssh-keygen -t rsa -N "" -f /home/admin/.ssh/id_rsa'
fi

# permit admin to transmit burp-custom
cp /root/burp-custom/burp-custom.sh /home/admin/
cp /root/burp-custom/burp*.bz2 /home/admin/
# restore permissions
chown -R admin:admin /home/admin/

# mail alias for admin
echo "admin: ${BURP_MAIL_DEST}" >> /etc/aliases
newaliases

# give root rights to admin
#sed -i "s@admin:x:.*@admin:x:0:0::/home/admin:/bin/bash@" /etc/passwd
