#!/bin/sh

PKG_MAKE_HOME=`cat ../.PKG_HOME`
PKG_METADIR="./metadir"
echo "### PKG MAKE HOME : $PKG_MAKE_HOME,$PKG_METADIR"
## base script
BASE_PATH="$PKG_MAKE_HOME/basescript"
CLEAR_FILE="$BASE_PATH/clearfile.sh"
CLEAR_OUTPUT="$BASE_PATH/clearoutput.sh"
GET_SVN="$BASE_PATH/getfile.sh"
MAKE_META="$BASE_PATH/createmeta.sh"
COPY_DES="$BASE_PATH/copy2desc.sh"
MAKE_PKG="$BASE_PATH/createpack.sh"
COPY_OUTPUT="$BASE_PATH/copy2output.sh"
USE_OUTCOPY="$BASE_PATH/copy2scp.sh"

## svn
### svn info 
SVN_INF="$BASE_PATH/SVN_INFO"
SVN_SRC_HOME=`grep "SVN_SRC_HOME" $SVN_INF | cut -d '=' -f 2`
SVN_CONF_HOME=`grep "SVN_CONF_HOME" $SVN_INF | cut -d '=' -f 2`
SVN_INS_HOME=`grep "SVN_INS_HOME" $SVN_INF | cut -d '=' -f 2`
### tagging path
SVN_INSH="/tags/release/installsh"
SVN_CONF="/tags/release/conf"
SVN_SRC_TAG="/tags/release/fhs/"
echo "### SVN_INFO : $SVN_INF,$SVN_SRC_HOME,$SVN_CONF_HOME,$SVN_INS_HOME,$SVN_CONF,$SVN_SRC_TAG"

## input vlaue
if [ $# -lt 6 ]
then
    echo "$0 [OSBITS] [OS] [VERSION] [REVISION] [INDEX] [TARGET]"
    exit 1
fi

OSBITS=$1
OS=$2
VER=$3
REVIRSION=$4
INDEX=$5
TARGET=$6

echo "### OS : $OSBITS,$OS"

## pkg config
PKG_NAME="postconfig"
PKG_ORIGIN="solbox/$PKG_NAME"
PKG_VER=`echo "$VER.$REVIRSION\_$INDEX" | sed 's/\\\\//'`
PKG_COMMENT="./c.txt"
PKG_DESC="./d.txt"
PKG_DEPS=""
PKG_PREINS="./preinstall.sh_FreeBSD6.2"
PKG_POSTINS="./postinstall.sh"
PKG_FILELIST="./filelist"
PKG_FULL="$PKG_NAME-$PKG_VER"

echo "### PKG_FULL : $PKG_FULL"

## packge source
WORK_HOME="$PKG_MAKE_HOME/.pkg-scr"
WORK_PATH="$WORK_HOME/$PKG_NAME"
WORK_OUTPKG="../.OUTPUT"
echo "### Work path : $WORK_HOME,$WORK_PATH,$WORK_OUTPKG"

## install path 
INS_HOME="/user/service"
#INS_PATH="$INS_HOME/bin"
INS_CONF="$INS_HOME/etc"
#INS_SCRIPT="$INS_HOME/installsh"
echo "### install path : $INS_HONE,$INS_PATH,$INS_CONF,$INS_SCRIPT" 

### tagging file & config
#FILE_NAME=""
CONF_NAME="postconfig"
#INSH_NAME=""


T=`grep "${TARGET}=" ../.DEPLOYMENT_INFO |  sed -e "s/$(echo $TARGET)=/FUND/" `
if [ -z $T ]; then 
    echo "error unkown target."
    exit 1
fi

POST_CONF_KEY=`grep "${TARGET}-POST-CONF-KEY=" ../.DEPLOYMENT_INFO |  sed -e "s/$(echo $TARGET)-POST-CONF-KEY=//" `
CONF_NAME2="-${POST_CONF_KEY}"


if [ $OS = "FreeBSD10.0" ]; then
	PKG_PREINS="./preinstall.sh_FreeBSD10.0"
elif [ $OS = "FreeBSD12.2" ]; then
	PKG_PREINS="./preinstall.sh_FreeBSD12.2"
	PKG_DEPS=" { }"
fi

## clean 
$CLEAR_FILE  "$WORK_PATH" "$INS_PATH" "$INS_CONF" "$INS_SCRIPT" "$PKG_METADIR"
$CLEAR_OUTPUT "$WORK_OUTPKG" "$TARGET" "$PKG_NAME"
echo "### clean : $WORK_PATH, $INS_PATH, $INS_CONF, $INS_SCRIPT,"$PKG_METADIR", $WORK_OUTPKG/$TARGET/$PKG_NAME*"


## get install files
#$GET_SVN "$WORK_PATH" "$SVN_SRC_HOME" "$SVN_SRC_TAG" "$FILE_NAME" "$OSBITS" "$OS"
$GET_SVN "$WORK_PATH" "$SVN_SRC_HOME" "$SVN_CONF" "$CONF_NAME$CONF_NAME2"
#$GET_SVN "$WORK_PATH" "$SVN_SRC_HOME" "$SVN_INSH" "$INSH_NAME"

## create meta-infomation 
$MAKE_META "$PKG_METADIR" "$PKG_NAME" "$PKG_VER" "$PKG_ORIGIN" "$PKG_COMMENT" "$INS_HOME" "$PKG_DEPS" "$PKG_DESC" "$PKG_PREINS" "$PKG_POSTINS"

## copy destination
### file
#$COPY_DES "TAR" "$WORK_PATH" "$INS_PATH" "$FILE_NAME"
#if [ $? -ne 0 ]; then
#    echo "error copy setup path"
#    exit 1
#fi
### config
$COPY_DES "FILE" "$WORK_PATH" "$INS_CONF" "$CONF_NAME$CONF_NAME2" "$CONF_NAME"
if [ $? -ne 0 ]; then
    echo "error copy install script"
    exit 1
fi
### install shell
#$COPY_DES "FILE" "$WORK_PATH" "$INS_SCRIPT" "$INSH_NAME"
#if [ $? -ne 0 ]; then
#   echo "error copy install script"
#   exit 1
#fi


## creating software package
$MAKE_PKG "$PKG_METADIR" "$PKG_FILELIST" "$WORK_OUTPKG" "$TARGET"
if [ $? -ne 0 ]; then
    echo "error make package"
    exit 1
fi

## copy output server
$COPY_OUTPUT "$USE_OUTCOPY" "$WORK_OUTPKG" "$PKG_NAME" "$TARGET"

