#!/bin/bash

mysql_datapath="/home/mysql"
ftp_path="/home/upload"
password="12345678"

mysql_5_7=`mysql --version | grep 5.7.`

ldconfig
source /usr/sbin/update_supervisor.sh
echo "update supervisor success"

#set local time
if [ -f "/etc/ntp.conf" ]; then
    mv /etc/ntp.conf /etc/ntp.conf.bak
fi
cp -f /etc/idste/ntp.conf /etc/ntp.conf
#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime || :
service ntp restart || echo $?
echo "set local time success"

# mysql server config
service mysql stop || :

cp -f /usr/lib/idste-server/usr.sbin.mysqld /etc/apparmor.d/
service apparmor restart

if [ -d "/etc/mysql/mysql.conf.d" ]; then
    if [ -f "/etc/mysql/mysql.conf.d/mysqld.cnf" ]; then
        mv /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.conf.back
    fi
    if [[ "$mysql_5_7" != "" ]]; then
        echo "mysql version is 5.7"
        if [ -f "/usr/lib/idste-server/my_57.cnf" ]; then
            cp -f /usr/lib/idste-server/my_57.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
        elif [ -f "/usr/lib/idste-server/my.cnf" ]; then
            cp -f /usr/lib/idste-server/my.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
        else
            echo "not found mysql5.7 config file"
        fi
    else
        echo "mysql version is 8.0"
        rm -rf /etc/mysql/*
        cp -rf /usr/lib/idste-server/mysql/* /etc/mysql/
    fi
fi

if [ -d "/usr/lib/tmpfiles.d" ]; then
    echo "d /var/run/mysqld 0755 mysql mysql -" > /usr/lib/tmpfiles.d/mysql.conf
fi
if [ ! -d "/var/run/mysqld" ]; then
    mkdir -p /var/run/mysqld
    chown mysql:mysql /var/run/mysqld -R
fi

if [ ! -d "$mysql_datapath" ]; then
    mkdir -p $mysql_datapath
    chown mysql:mysql $mysql_datapath/ -R
    mysqld --explicit_defaults_for_timestamp --initialize --user=mysql --datadir=$mysql_datapath >> /dev/null
fi
echo "mysql server config success"

# set crontab
cp /etc/crontab /etc/crontab.back
begin=`grep "idste-crontab-file begin" /etc/crontab -n | head -1 | awk -F ':' '{printf $1}'`
end=`grep "idste-crontab-file end" /etc/crontab -n | head -1 | awk -F ':' '{printf $1}'`
if [ ! -z $begin ]; then
    if [ -z $end ]; then
        sed -i "$begin,\$d" /etc/crontab
    else
        sed -i "$begin,$end d" /etc/crontab
    fi
fi

echo "# idste-crontab-file begin" >> /etc/crontab
cat /usr/lib/idste-server/crontab >> /etc/crontab
echo "# idste-crontab-file end" >> /etc/crontab
echo "set crontab success"

# force reset mysql password
if [ -e "/etc/init/mysql.conf" ]; then
    rm -f /tmp/mysqld.*
    mysqld --skip-grant-tables --socket=/tmp/mysqld.socket --pid_file=/tmp/mysqld.pid>&1 &
    sleep 5
    if [ ! -S "/tmp/mysqld.socket" ]; then
        echo "install idste-server failed, /tmp/mysqld.socket is not found, please try again <<<<<<<<<<"
        /bin/ps aux | grep skip-grant-tables | awk -F ' ' '{print $2}' | xargs kill -9 >>/dev/null 2>&1 || :
        exit $?
    fi
    mysql --socket=/tmp/mysqld.socket -u root -e "update mysql.user set plugin='mysql_native_password', authentication_string=(select concat('*', upper(sha1(unhex(sha1('$password')))))), password_expired='N' where User='root' and Host='localhost';"
    /bin/ps aux | grep skip-grant-tables | awk -F ' ' '{print $2}' | xargs kill -9 >>/dev/null 2>&1 || :
    rm -f /tmp/mysqld.socket
    rm -f /tmp/mysqld.pid
    service mysql start || exit $?
    echo "force reset mysql password success"
else
    echo "mysql config file is not found"
fi

# exec mysql update sql
if [ ! -x "/usr/lib/idste-server/update_sql" ]; then
    chmod +x /usr/lib/idste-server/update_sql
fi
/usr/lib/idste-server/update_sql $password 2>/dev/null
echo "mysql update sql exec success"

# upload media dir process
if [ ! -d "$ftp_path/media" ]; then
    mkdir -p $ftp_path/media
    #chown -R mysql:mysql $ftp_path/media
    if [ -d "$mysql_datapath/media" ]; then
        mv $mysql_datapath/media/* $ftp_path/media
        rm -rf $mysql_datapath/media
        ln -s $ftp_path/media $mysql_datapath/media
    fi
fi
echo "upload media dir process success"

# ftp dir and ftp user (deprecated)
if ! id -u 'upload' >/dev/null 2>'uplaod'; then
    echo "CREATE FTP USER"
    useradd upload -d /home/upload -s /bin/false
fi

chmod a+w $ftp_path/media -R
chmod a-w $ftp_path
chown root:root $ftp_path/mysql
echo "ftp dir and ftp user (deprecated) process success"

# shell exec path
if [ -f "/etc/shells" ]; then
    mv /etc/shells /etc/mysql/shells.bak
fi

cp /usr/lib/idste-server/shells /etc/shells
echo "change shell exec path success"

# dns config
if [ ! -f "/etc/resolvconf/resolv.conf.d/tail" ]; then
    if [ -d "/etc/resolvconf/resolv.conf.d" ]; then
        touch /etc/resolvconf/resolv.conf.d/tail
    fi
fi
echo "dns config success"

# add www-data user to root group
usermod -G root www-data
echo "add www-data user to root group success"

# nginx web set file and dir for process
service nginx stop || exit $?

chown -R www-data:www-data /home/www
chmod g+w /etc
if [ -f "/etc/network/interfaces" ]; then
    chown www-data:root /etc/network/interfaces
    chmod g+w /etc/network
    chmod g+w /etc/network/interfaces
fi
if [ -d "/etc/netplan" ]; then
    for file in `ls /etc/netplan`
    do
        chown www-data:root "/etc/netplan/$file"
        chmod g+w /etc/netplan
        chmod g+w "/etc/netplan/$file"
    done
fi
if [ -f "/etc/ntp.conf" ]; then
    chown www-data:root /etc/ntp.conf
    chmod g+w /etc/ntp.conf
fi

if [ ! -d "/home/upload/temp" ]; then
    mkdir -m 777 /home/upload/temp # audio convert temp
fi

if [ ! -d "/home/upload/custom" ]; then
    mkdir /home/upload/custom
    mkdir /home/upload/custom/image
fi

if [ ! -d "/home/upload/custom/image" ]; then
    mkdir /home/upload/custom/image
fi

chown www-data:www-data /home/upload/custom -R
if [ -f "/usr/sbin/nginx_1.25.1" ]; then
    if [ -f "/usr/sbin/nginx" ]; then
        rm -f "/usr/sbin/nginx"
    fi
    mv /usr/sbin/nginx_1.25.1 /usr/sbin/nginx
    chmod +x /usr/sbin/nginx
fi
if [ -f "/usr/lib/idste-server/nginx.conf" ]; then
    if [ -f "/etc/nginx/nginx.conf" ]; then
        rm -f "/etc/nginx/nginx.conf"
    fi
    mv /usr/lib/idste-server/nginx.conf /etc/nginx/nginx.conf
fi

service nginx restart || :
echo "nginx and web upload dir config success"

# dir and file mod process
chmod +x /usr/bin/idprobe
chmod +x /usr/bin/audio_convert
chmod +x /usr/sbin/iDsteRtmpPusher
chmod +x /usr/sbin/iDsteWebrtc
chmod +x /usr/sbin/iDsteServers
chmod +x /usr/sbin/systeminfo
chmod +x /usr/sbin/update_mosquitto.sh
chmod +x /usr/sbin/logback.sh
chmod 600 /etc/idste/proxy_user.key
chown root:root /etc/idste/proxy_user.key
chown mysql:mysql /usr/local/share/libwebsockets-test-server/*
echo "process dir and file mod success"

# mosquitto
if [ -f "/var/lib/mosquitto/mosquitto.db" ]; then
    rm -f "/var/lib/mosquitto/mosquitto.db"
fi

if [ -f "/usr/sbin/mosquitto_2.0.11" ]; then
    echo "mosquitto 2.0.11 will be install"
    if [ -f "/usr/sbin/mosquitto" ]; then
        rm -f "/usr/sbin/mosquitto"
    fi
    mv /usr/sbin/mosquitto_2.0.11 /usr/sbin/mosquitto

    if [ -f "/etc/mosquitto/mosquitto.conf_2.0.11" ]; then
        if [ -f "/etc/mosquitto/mosquitto.conf" ]; then
            rm -f "/etc/mosquitto/mosquitto.conf"
        fi
        mv /etc/mosquitto/mosquitto.conf_2.0.11 /etc/mosquitto/mosquitto.conf
    fi

    if [ -f "/etc/mosquitto/conf.d/idste.conf_2.0.11" ]; then
        if [ -f "/etc/mosquitto/conf.d/idste.conf" ]; then
            rm -f "/etc/mosquitto/conf.d/idste.conf"
        fi
        mv /etc/mosquitto/conf.d/idste.conf_2.0.11 /etc/mosquitto/conf.d/idste.conf
    fi
fi

chmod +x /usr/sbin/mosquitto
if [ -d "/usr/lib/tmpfiles.d" ]; then
    echo "d /var/run/mosquitto 0755 mosquitto root -" > /usr/lib/tmpfiles.d/mosquitto.conf
fi
echo "mosquitto process success"


# restart mysql
service mysql restart || exit $?
echo "restart mysql success"

# restart supervisor
service supervisor restart || :
echo "install idste-server success ........................"
