#!/bin/bash

#export LC_ALL="en_US.UTF-8"
cd `dirname $0`
currentDir=`pwd`

export LD_LIBRARY_PATH=$currentDir/lib:$LD_LIBRARY_PATH

user="postgres"
password="dWSmBVwIWalEhdaMp6"

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


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

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

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

#
find $currentDir \! -user postgres -exec chown postgres {} \;

chmod -R 777 $currentDir/bin

su - $user <<EOF
echo "当前用户:$user"
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
echo "$currentDir/bin/initdb -D $dataPath -E UTF-8 --username $user --pwfile=<(printf "%s\n" "$password")"
$currentDir/bin/initdb -D $dataPath -E UTF-8 --username $user --pwfile=./password.txt

EOF
