#!/bin/bash
get_ip()
{
        for x in eth0 eth1 eth2 eth3 wlan0 ens3
        do
        LINK_STATE=`ethtool "$x" 2>/dev/null|sed -n '/Link detected/p'|awk -F ':' '{print $2}'|sed 's/ //'`
        if [ "$LINK_STATE" == "yes" ]; then
                ETH=$x
                break
        fi
        done
		echo $ETH
        IP_ADDR=`ifconfig $ETH|sed -n  '/inet /p'|awk '{print $2}'`
}
# get_ip

configure_psql()
{
	postgresql-setup  initdb
    systemctl restart postgresql
	su postgres -c "createuser -S -D -R dbuser_buildiso;createdb -O dbuser_buildiso db_buildiso" &> /dev/null
	su dbuser_buildiso -c "psql  db_buildiso dbuser_buildiso < /usr/local/ctdy-server/postgresql/init.sql" 2>&1 /dev/null
	cp -f  /usr/local/ctdy-server/postgresql/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf
	systemctl restart postgresql
	systemctl restart httpd
}
configure_fab()
{
	export LC_ALL=en_US.UTF-8
	export FLASK_APP=app 
	flask fab create-admin --username test --firstname test --lastname test  --email test@kylinos.cn --password test
}

shutdown_selinux()
{
	SELINUX="/etc/selinux/config" 
	if [ -e $SELINUX ]; then
		sed -i 's/^SELINUX=.*/SELINUX=disabled/g' $SELINUX
	fi
}

config_redis() 
{
	REDIS_CONFIG="/etc/redis.conf"
	if [ -e $REDIS_CONFIG ]; then
		#sed -i 's/127.0.0.1/'''${IP_ADDR}'''/g' $REDIS_CONFIG
		sed -i 's/127.0.0.1/'''0.0.0.0'''/g' $REDIS_CONFIG
	fi
	systemctl restart redis
}

configure_server()
{
	systemctl disable firewalld
	systemctl enable httpd
	systemctl enable postgresql
	systemctl enable redis
	useradd dbuser_buildiso -M &> /dev/null
	passwd -d dbuser_buildiso &> /dev/null
	
}
configure_server
shutdown_selinux
configure_psql
config_redis
