#!/bin/sh
#########################################################################
#マニュアル設定
#########################################################################
# PostgreSQL用ユーザのセット
postgres_user="xxxxxxxx"
postgres_admin="postgres"

#########################################################################
#PostgreSQLパスワード設定
#########################################################################
pass="xxxxxxxx"

#############################################
## 設定に必要な変数をセット
#############################################
aipo_version="5.0.2.0"

#ユーザ確認
current_user=`whoami`
if [ "${current_user}" != "root" ]; then
    echo "Aipo$aipo_versionのインストールはroot権限で行ってください。"
    exit 0
fi

#カレントディレクトリ
echo ${enc_str}
len=`expr match $0 \\\/.*`
#echo $len
if [ $len -gt 0 ]; then
	script_path=$0
else
	script_path=`pwd`/$0
fi
echo ${script_path} | sed "s/\/bin\/${enc_str}//g" >> temp.txt
script_path=`cat temp.txt`
rm -rf temp.txt
len=`expr match ${script_path} .*\\\/ - 1`
script_path=`expr substr ${script_path} 1 $len`
echo "install directory is ${script_path}"

. ${script_path}/bin/install.conf

addr=""
if [ -e /etc/sysconfig/network-scripts/ifcfg-${netitf} ]; then
	. /etc/sysconfig/network-scripts/ifcfg-${netitf}
	addr=$IPADDR
fi

if [ "$addr" = "" ]; then
	addr=`ifconfig ${netitf} | grep "inet addr" | cut -f 2 -d :| cut -f 1 -d " "`
fi

if [ "$addr" = "" ]; then
	addr=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d :| cut -f 1 -d " "`
fi

if [ "$addr" = "" ]; then
	addr=${def_addr}
fi

# インストールフォルダ
aipo_dir=${script_path}
# PosgreSQLポート番号
port_num="5432"
# ソースディレクトリ
src_dir=$aipo_dir"/src"
#Tomcatポート番号
s_port_num=8080

### Directories ###
#Install Directory=/usr/local/aipo
#script_path=/usr/local/aipo
#aipo_dir=/usr/local/aipo
#src_dir=/usr/local/aipo/src
#Original *.sh Directory=/usr/local/aipo/src/bin/utf8
#Target   *.sh Directory=/usr/local/aipo/bin
#db_dir_data=/var/lib/pgsql/data
###################################################
#簡易チェック
###################################################
check_str="AIPO_INSTALL_OK"

if [ -s ~/.bashrc ]; then
	echo "checking bashrc... file exists"
else
	if [ -s  /etc/skel/.bashrc ]; then
		echo "copying .bashrc from /etc/skel diectory"
		cp /etc/skel/.bashrc ~/
	else
		echo "checking bashrc...  file does not exists"
		touch ~/.bashrc
		#check_str="ERROR"
	fi
fi

if [ -s ~/.bash_profile ]; then
	echo "checking bash_profile... file exists"
else
	if [ -s  /etc/skel/.bash_profile ]; then
		echo "copying .bash_profile from /etc/skel diectory"
		cp /etc/skel/.bash_profile ~/
	else
		echo "checking .bash_profile...  file does not exists"
		touch ~/.bash_profile
		#check_str="ERROR"
	fi
fi

sudo -u ${postgres_user} ls ${script_path}
tmp_str=$?

if [ "$tmp_str" = "0" ]; then
	echo "checking permissions... ok"
else
	echo "checking permissions... ERROR invalid permissions"
	echo "${postgres_user} は ${script_path} にアクセスできません。"
	check_str="ERROR"
fi

if [ "$check_str" != "AIPO_INSTALL_OK" ]; then
	if [ "${check_pass}" != "true" ]; then
		echo "Aipo$aipo_versionのインストールに失敗しました"
		exit 0
	fi
fi

#############################################
## 環境変数の設定
#############################################
path_str1='export PATH='$aipo_dir'/bin:"$PATH"'
path_str2='export POSTGRES_HOME=/var/lib/pgsql'
path_str3='export PGLIB=/usr/lib/pgsql'
path_str4='export PGDATA=$POSTGRES_HOME/data'
path_str6='export LD_LIBRARY_PATH=/usr/local/lib:"$LD_LIBRARY_PATH":"$PGLIB"'
# .bash_profileのバックアップ
sudo -u ${postgres_user} cp -p /home/${postgres_user}/.bash_profile /home/${postgres_user}/.bash_profile~
# 各環境変数の設定
sudo -u ${postgres_user} echo $path_str1 >> /home/${postgres_user}/.bash_profile
sudo -u ${postgres_user} echo $path_str2 >> /home/${postgres_user}/.bash_profile
sudo -u ${postgres_user} echo $path_str3 >> /home/${postgres_user}/.bash_profile
sudo -u ${postgres_user} echo $path_str4 >> /home/${postgres_user}/.bash_profile
sudo -u ${postgres_user} echo $path_str6 >> /home/${postgres_user}/.bash_profile
# .bash_profileを再読み込み
#sudo -u ${postgres_user} source /home/${postgres_user}/.bash_profile
db_dir_data=/var/lib/pgsql/data

###########################################
#Tomcatの設定
###########################################
Apr_Str='<Listener className="org.apache.catalina.core.AprLifecycleListener" \/>'
sed -i "s/$Apr_Str/<!--$Apr_Str-->/g" $CATALINA_HOME/conf/server.xml
#最後の行にタイムゾーン設定を追加する
echo 'user.timezone=Asia/Tokyo' >> $CATALINA_HOME"/conf/catalina.properties"
#############################################
#Aipoのインストール
#############################################

# rm -rf "$aipo_dir"/tomcat/webapps/*
cp -rf "$aipo_dir"/src/webapps/aipo "$CATALINA_HOME"/webapps/
cp -f "$aipo_dir"/src/postgresql-8.1-407.jdbc3.jar "$CATALINA_HOME"/common/lib

#driver.xmlファイル書き換え
sed -i "s/password=\"aipo\"/password=\"$pass\"/g" $CATALINA_HOME/webapps/aipo/WEB-INF/conf/postgres/Org001DomainNode.driver.xml
sed -i "s/aipo_postgres/${postgres_user}/g" $CATALINA_HOME/webapps/aipo/WEB-INF/conf/postgres/Org001DomainNode.driver.xml

#データベース初期化とデータ登録
cp -rf $aipo_dir/backup/postgresql/sample_dump/psml $CATALINA_HOME/webapps/aipo/WEB-INF
cp -rf $aipo_dir/backup/postgresql/sample_dump/files $CATALINA_HOME/webapps/aipo/WEB-INF
cp -rf $aipo_dir/backup/postgresql/sample_dump/mail $CATALINA_HOME/webapps/aipo/WEB-INF
sed -i "s/aipo_postgres/${postgres_user}/g" $aipo_dir/backup/postgresql/sample_dump/aipo_db_sql.dump

echo "### db作成 ###"
sudo -u ${postgres_user} createdb org001 -O ${postgres_user} -U ${postgres_user}
echo "### db初期化 ###"
sudo -u ${postgres_user} psql -U ${postgres_user} -d org001 -f $aipo_dir/backup/postgresql/sample_dump/aipo_db_sql.dump
echo "### port番号挿入 ###"
sudo -u ${postgres_user} psql -U ${postgres_user} -d org001 -c "UPDATE EIP_M_COMPANY SET PORT_INTERNAL=${s_port_num}"
echo "### ユーザー無制限 ###"
sudo -u ${postgres_user} psql -U ${postgres_user} -d org001 -c "UPDATE AIPO_LICENSE SET LIMIT_USERS=0"

echo "### .pgpass作成 ###"
sudo -u ${postgres_user} echo "localhost:$port_num:org001:${postgres_user}:$pass" > /home/${postgres_user}/.pgpass
chown ${postgres_user}. /home/${postgres_user}/.pgpass
sudo -u ${postgres_user} chmod 0600 /home/${postgres_user}/.pgpass

############################################
#xregファイルのバックアップ
cp -pf $CATALINA_HOME/webapps/aipo/WEB-INF/conf/*.xreg  $aipo_dir/backup/xreg

############################################
#実行用スクリプト生成
############################################
echo "$aipo_dir" | sed s/'\/'/'\\\/'/g > temp.txt
tmp_str=`cat temp.txt`
rm -rf temp.txt
echo $tmp_str

cd $src_dir/bin/$enc_str
for FILE in *.sh; do
	sed "s/default_folder/$tmp_str/g" $FILE | sed "s/default_user/${postgres_user}/g" | sed "s/default_server_port/$s_port_num/g" | sed "s/default_db_port/$port_num/g" | sed "s/postgres_pass/$pass/g" | sed "s/Aipo_Version/$aipo_version/g" > $aipo_dir/bin/$FILE
done

chmod +x $aipo_dir/bin/*.sh

######################################
#結果表示
######################################

echo ==Aipo"$aipo_version"インストール終了==
echo user:${postgres_user}
echo pass:$pass
echo directory:$aipo_dir
echo "Aipo URL: http://$addr/aipo/"