#!/bin/bash

set -e
BEECOMD=`which bee`
show_usage="args: [--runmode=,--serve-addr=,--serve-port=,--etcd-addr=,--etcd-port=,--encrypt-key=,--log-dir=]"

#-o或--options选项后面是可接受的短选项，如ab:c::，表示可接受的短选项为-a -b -c，
#其中-a选项不接参数，-b选项后必须接参数，-c选项的参数为可选的
#-l或--long选项后面是可接受的长选项，用逗号分开，冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o -h --long help,runmode:,serve-addr:,serve-port:,etcd-addr:,etcd-port:,encrypt-key:,out-ip:,log-dir: -n "$0" -- "$@"`

if [ $? != 0 ]; then
    exit 1
fi

#将规范化后的命令行参数分配至位置参数（$1,$2,...)
eval set -- "${ARGS}"

while [ -n "$1" ]
do
    case "$1" in
        -h|--help)
            echo "$show_usage"; exit 1 ;;
        --runmode) 
            RUNMODE=$2; shift ;;
        --serve-addr)
            SERVE_ADDR=$2; shift ;;
        --serve-port)
            SERVE_PORT=$2; shift ;;
        --etcd-addr)
            ETCD_ADDR=$2; shift ;;
        --etcd-port)
            ETCD_PORT=$2; shift ;;
        --encrypt-key)
            ENCRYPT_KEY=$2; shift ;;
        --log-dir)
            LOG_DIR=$2; shift ;;
	--out-ip)
	    OUT_IP=$2; shift ;;
        *) shift ;;
    esac
done

if [ -z $SERVE_PORT ];then
    echo "the server_port must be specified."
    exit 1
fi

if [ -z $OUT_IP ];then
    echo "out ip must be specified."
fi
if [ -z $RUNMODE ];then
    RUNMODE="dev"
fi
if [ -z $SERVE_ADDR ];then
    SERVE_ADDR="0.0.0.0"
fi
if [ -z $ETCD_ADDR ];then
    ETCD_ADDR="127.0.0.1"
fi
if [ -z $ETCD_PORT ];then
    ETCD_PORT="2379"
fi
if [ -z $ENCRYPT_KEY ];then
    ENCRYPT_KEY="a95cbb574bdc905f9bf457820f1fa602"
fi
if [ -z $LOG_DIR ];then
    LOG_DIR="logs"
fi

PROJECT_PATH=$(cd `dirname $0`; pwd)
SERVICE_NAME="${PROJECT_PATH##*/}"
APP_NAME="${PROJECT_PATH##*/}"
APP_CONF=conf/app.conf
GITLINE=`git log | head -1`
GITLOG=gitlog
DISCOVERY_TYPE="etcd"

echo $GITLINE > $GITLOG

#生成配置文件
sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}.in > ${APP_CONF}
sed -i "s/%APP_NAME%/$APP_NAME/g" ${APP_CONF}
sed -i "s/%RUNMODE%/$RUNMODE/g" ${APP_CONF}
sed -i "s/%SERVE_ADDR%/$SERVE_ADDR/g" ${APP_CONF}
sed -i "s/%SERVE_PORT%/$SERVE_PORT/g" ${APP_CONF}
sed -i "s/%ETCD_ADDR%/$ETCD_ADDR/g" ${APP_CONF}
sed -i "s/%ETCD_PORT%/$ETCD_PORT/g" ${APP_CONF}
sed -i "s/%ENCRYPT_KEY%/$ENCRYPT_KEY/g" ${APP_CONF}
sed -i "s/%LOG_DIR%/$LOG_DIR/g" ${APP_CONF}
sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${APP_CONF}
sed -i "s/%DISCOVERY_TYPE%/$DISCOVERY_TYPE/g" ${APP_CONF}
sed -i "s/%OUT_IP%/$OUT_IP/g" ${APP_CONF}


SPEC_FILE=app.spec
APP_SERVICE=conf/app.service
SH_APP=sh/app
TOOLS_ROOT=`pwd`
BUILD="${TOOLS_ROOT}/build"

sed -e "s/%APP_NAME%/$APP_NAME/g" ${APP_SERVICE}.in > conf/${SERVICE_NAME}.service
sed -e "s/%APP_NAME%/$APP_NAME/g" ${SH_APP}.in > ${SH_APP}
sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SH_APP}

$BEECOMD generate docs

VERSION=`cat VERSION`
make version=$VERSION

mkdir -p $BUILD/buildroot/{BUILD,RPMS,S{RPMS,PECS,OURCES}}
rm -f $BUILD/buildroot/SOURCES/*.tgz


tar czf $BUILD/buildroot/SOURCES/${SERVICE_NAME}-$VERSION.tgz   * .[!.]*

sed -e "s/%VERSION%/$VERSION/g" ${SPEC_FILE}.in > ${SPEC_FILE}
sed -i "s/%APP_NAME%/$APP_NAME/g" ${SPEC_FILE}
sed -i "s/%SERVICE_NAME%/$SERVICE_NAME/g" ${SPEC_FILE}
rpmbuild  -bb ${SPEC_FILE} --define "_topdir $BUILD/buildroot/" 

rm -rf ./dist
mkdir ./dist
cp -rf $BUILD/buildroot/RPMS/* ./dist

rm -rf $BUILD


