#!/bin/bash

#export LC_ALL="en_US.UTF-8"
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH

user="postgres"
password="dWSmBVwIWalEhdaMp6"

id $user &> /dev/null
ret=$?
if [ $ret -ne 0 ]; then
	echo "创建用户: $user"
	useradd $user
fi


#
basePath=../../
dataPath=$basePath/data/pg

mkdir -p $dataPath
chmod 00700 $dataPath
chown $user $dataPath

logPath=$basePath/log/pg
mkdir -p $logPath
chmod 00700 $logPath
chown $user $logPath

#
postgresHome=./
find $postgresHome \! -user postgres -exec chown postgres {} \;

#
#su - $user -c '/opt/liankong-runtime-1.0/components/pgsql/bin/initdb -D $dataPath -E UTF-8 --username $user --pwfile=<(printf "%s\n" "$password")'

su - $user <<EOF
cd /opt/liankong-runtime-1.0/components/pgsql;
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
./bin/initdb -D $dataPath -E UTF-8 --username $user --pwfile=<(printf "%s\n" "$password")

EOF
