#!/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/httpd"
echo "### SVN_INFO : $SVN_INF,$SVN_SRC_HOME,$SVN_CONF_HOME,$SVN_INS_HOME,$SVN_CONF,$SVN_SRC_TAG"

## input vlaue
if [ $# -lt 5 ]
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="httpd"
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_FreeBSD6.2"
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/httpd"
INS_CONF="$INS_PATH/conf"
INS_CONF_EXT="$INS_CONF/extra"
INS_SCRIPT="$INS_HOME/installsh"
echo "### install path : $INS_HONE,$INS_PATH,$INS_CONF,$INS_SCRIPT" 

### tagging file & config
FILE_NAME="httpd.2.4.17.tar"
CONF_NAME="httpd.conf.in"
CONF_EXT_FASTCGI="httpd-fastcgi.conf"
CONF_EXT_SSL="httpd-support-ssl.conf"
INSH_NAME="httpd_config.sh"

if [ $OS = "FreeBSD10.0" ]; then
	PKG_PREINS="./preinstall.sh_FreeBSD10.0"
	PKG_FILELIST="./filelist_FreeBSD10.0"
	#FILE_NAME="httpd.2.4.16.tar"
	PKG_DEPS=" gcc: {origin: lang/gcc, version: "4.6.4"}^ expat: {origin: textproc/expat2, version: "2.1.0"}^ libxml2: {origin: textproc/libxml2, version: "2.8.0_3"}^ openssl: {origin: security/openssl, version: "1.0.1_9"}^ cronolog: {origin: sysutils/cronolog, version: "1.6.2_4"}^ perl5: {origin: lang/perl5.16, version: "5.16.3_6"}^ p5-CGI.pm: {origin: www/p5-CGI.pm, version: "3.63_1,1"}^ p5-FCGI: {origin: www/p5-FCGI, version: "0.77"}"
elif [ $OS = "FreeBSD12.2" ]; then
	PKG_PREINS="./preinstall.sh_FreeBSD12.2"
	PKG_FILELIST="./filelist_FreeBSD12.2"
	FILE_NAME="httpd.2.4.46.tar"
	PKG_DEPS=" { gcc: {origin: lang/gcc, version: "9.3.0"}^ expat: {origin: textproc/expat2, version: "2.2.8"}^ libxml2: {origin: textproc/libxml2, version: "2.9.10_1"}^ cronolog: {origin: sysutils/cronolog, version: "1.6.2_6"} }"
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"
$GET_SVN "$WORK_PATH" "$SVN_SRC_HOME" "$SVN_CONF" "$CONF_EXT_FASTCGI"
$GET_SVN "$WORK_PATH" "$SVN_SRC_HOME" "$SVN_CONF" "$CONF_EXT_SSL"
$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"
if [ $? -ne 0 ]; then
    echo "error copy install script"
    exit 1
fi
$COPY_DES "FILE" "$WORK_PATH" "$INS_CONF_EXT" "$CONF_EXT_FASTCGI"
if [ $? -ne 0 ]; then
    echo "error copy install script"
    exit 1
fi
$COPY_DES "FILE" "$WORK_PATH" "$INS_CONF_EXT" "$CONF_EXT_SSL"
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"

