#!/bin/bash
#  Copyright (c) 2016 by NuTyX team (http://nutyx.org)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
#  USA.
#
usage () {
echo '******************************************************************************
 1. INTRODUCTION

 This script is able to compile any port from any official collections that
 are one of:

 - base, cli ,cli-extra, gui, gui-extra or <yours>

 IMPORTANTS NOTES:

 a. We make a difference only for base, cli and gui
    and their -extra counterparts.
    Example: to compile the all cli-extra collection
    on the stable version, the syntax will be:

    build-collection /stable/cli-extra
    As simple as that :)

 b. You need about 10 Gbytes of free space for the mirror of NuTyX (/srv/www),
    Also be aware that /srv/www/sources will continuously grow.

 c. The installed NuTyX should ALWAYS be under /development, /current or /stable.

 2. HOW DOES THIS WORK ?

  The script will do following steps:

  - install a development, current or stable NuTyX
    (define by the first part of the first argument)
    into a folder (define by the first argument as well)
  - install the devel packages
  - mount the depot needed for the specific collection
    (define by the first argument)
  - mount the ports needed for the specific collection
  - mount the sources and logs folders
  - enter (chroot) in the installed NuTyX
  - run the script found in /tmp folder which is buildup by
    collecting info from arguments
  - will compile what has to be compile with or without success
    but in both cases it will...
  - exit the installed NuTyX
  - umount the depot, the ports, the sources and logs folders.

 3. ARGUMENTS:

 You can pass 1 or 2 arguments:

 Parameter 1 (mandatory):
  Its specify the version we are going to use (currently it can be 'development' or 'stable'
  and its specify the collection which is one of following possible collection:
  base, cli, cli-extra, gui, gui-extra or your specific collection which can be named anything.

 Parameter 2 (optionnal):
  This parameter works only with base, cli, cli-extra, gui, gui-extra collections.
  This parameter specify the port you want to build only.
  Example:

   build-collection /development/cli-extra irssi

  This example will build (or rebuild) the irssi binaries only

 3. CONFIGURATION OF THE HOST NuTyX

 Its absolutly necessary to configure properly your NuTyX host machine otherwise nothing will work like expected.

 Configuration file that is involve in the configuration process:

 - /etc/fstab

 As explained above, we mount a lot of folders, means they HAVE TO BE specified in the /etc/fstab or specified via
 variables (see http://www.nutyx.org/fr/build-bot).
 As we used predefined /DEPOT, /sources and /LGOS folders, they can be specify via variable or all in /etc/fstab file:

 Here an example for the cli-extra collection of the development version
 you should specify following lines, adapted to your needs:

/srv/www/sources /development/cli-extra/sources none bind
/srv/www/x86_64/development /development/cli-extra/DEPOT none bind
/srv/www/logs/x86_64/development/cli-extra /development/cli-extra/LOGS none bind
/home/thierry/git/extra /development/cli-extra/root/extra none bind
  

 4. Setup all the needed folders and get the official depot into the
 /srv/www location (adapt the folder to yours)

  We create all the necessary folders (for our cli-extra collection example):

    mkdir -p /srv/www/sources
    mkdir -p /srv/www/logs/x86_64/development/cli-extra

  To get all the binaries locally and used them (highly recommended) we need to rsync them:
  for DIR in base cli
  do
   rsync -av --delete  \
   rsync://downloads.nutyx.org/nutyx/x86_64/development/$DIR/ \
   /srv/remote/x86_64/development/$DIR/
  done

 Enjoy NuTyX :)

******************************************************************************
'|more
}
interrupted() {
echo "Interrupted !!!"
}
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
umount_all() {
for FOLDER in $MOUNT_POINTS
do
	umount -v $FOLDER || exit 1
done
}
remove_all() {
for FOLDER in $MOUNT_POINTS
do
	rm -d $FOLDER || exit 1
done
[ -z ${1} ] && exit 1
umount ${1}/{run,sys,proc,dev/pts,dev}
for FOLDER in ${1}/*
do
	rm -r $FOLDER || exit 1
	echo "Folder $FOLDER deleted"
done
rm -d $1 || exit 1
}
error() {
	umount_all
	remove_all ${1}
	exit 1
}
setup_pkgmk_conf() {
cat > /$VERSION/$COLLECTION/etc/pkgmk.conf << "EOF"
export CFLAGS="-O2 -pipe"
export CXXFLAGS="${CFLAGS}"
export MAKEFLAGS="-j$(getconf _NPROCESSORS_ONLN)"
PKGMK_GROUPS=(devel man doc service)
PKGMK_LOCALES=(ar fr de it es hi ja nl pt da nn sv fi tr ru pl zh_CN)
PKGMK_CLEAN="no"
PKGMK_KEEP_SOURCES="yes"
PKGMK_SOURCE_DIR="/sources"
PKGMK_WORK_DIR="/tmp/work"
PKGMK_COMPRESS_PACKAGE="yes"
PKGMK_COMPRESSION_MODE="xz"
PKGMK_IGNORE_REPO="no"
PKGMK_IGNORE_COLLECTION="no"
PKGMK_IGNORE_RUNTIMEDEPS="no"
EOF
}
setup_collection() {
	local DIR
	DIR="/$VERSION/$COLLECTION"

	if [ "$COLLECTION" == "base" ]; then
		GIT="base"
		echo "dir /DEPOT/$COLLECTION
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	if [ "$COLLECTION" == "cli" ]; then
		GIT="core"
		echo "dir /DEPOT/$COLLECTION
dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	if [ "$COLLECTION" == "gui" ]; then
		GIT="core"
		echo "dir /DEPOT/$COLLECTION
dir /DEPOT/cli
dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	if [ "$COLLECTION" == "cli-extra" ]; then
		GIT="extra"
		echo "dir /DEPOT/$COLLECTION
dir /DEPOT/cli
dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	if [ "$COLLECTION" == "gui-extra" ]; then
		GIT="extra"
		echo "dir /DEPOT/$COLLECTION
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	if [ "$COLLECTION" == "nos" ]; then
		echo "dir /DEPOT/$COLLECTION
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
		return
	fi
	echo "dir /DEPOT/$COLLECTION-extra
dir /DEPOT/$COLLECTION
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
}
#
#
###############################################################################
# BEGIN HERE
###############################################################################
#
# ROOTMOUNT is set to '/srv/www' by default
# It make it easy to access files if http server is installed
# and configured as default
#
[ -z "$ROOTMOUNT" ] && ROOTMOUNT="/srv/www"

if [ "${1}" == "-h" ] || [ "${1}" == "--help" ];then
	usage
	exit 0
fi


if [ $# -lt 1 ]; then
	usage
	echo "
>>>	Specify the collection
"
	exit  1
fi

if [ -d $1 ]; then
	mount | grep $1
	echo "
>>>	$1 exist ...
"
	exit 1
fi

VERSION=`dirname $1`
VERSION=`basename $VERSION`
COLLECTION=`basename $1`
GIT="$COLLECTION"

mkdir -p $1/etc || exit 1

setup_collection

if [ "$GIT" == "base" ] \
|| [ "$GIT" == "core"  ] \
|| [ "$GIT" == "extra" ] \
|| [ "$GIT" == "nos" ]; then
	DEVEL="cards.devel \
&& cd /root/$GIT \
&& bash scripts/$COLLECTION"
	if [ $# -gt 1 ]; then
		DEVEL="$DEVEL $2"
	else
		DEVEL="$DEVEL -a"
	fi
else
	DEVEL="cards.devel \
&& cd /root/$GIT \
&& bash scripts/$COLLECTION -a \
&& bash scripts/$COLLECTION-extra -a"
fi

setup_pkgmk_conf

export DEVEL VERSION

GIT_PROJ=`grep ^/home /etc/fstab |grep $1/|cut -d " " -f1`
if [ -z "$GIT_PROJ" ]; then
	echo ">>> Cannot rsync, $1 not found in /etc/fstab"
	error $1
fi

echo ""
echo " cards.conf:"
cat /$VERSION/$COLLECTION/etc/cards.conf
echo ""
echo " COLLECTION: $COLLECTION"
echo "GIT Project: $GIT"
echo "    VERSION: $VERSION"
echo "The command: KERNEL="NONE" LFS="$1" DEPOT="/DEPOT" install-nutyx"
echo "      DEVEL: $DEVEL "
echo ""
echo "The rsync  : rsync -a --exclude .git $GIT_PROJ/ $1/root/$GIT/"
echo ""
echo -n "  OK ? (y) : "
read a

if [ "$a" != "y" ]; then
	echo "Answer should be 'y'"
	rm -rv $1
	exit 1
fi

mkdir -p $1/DEPOT || error $1
if [ -d ${ROOTMOUNT}/`uname -m`/${VERSION} ]; then
	mount --bind ${ROOTMOUNT}/`uname -m`/${VERSION} $1/DEPOT || error $1
else
	mount $1/DEPOT || error $1
fi
MOUNT_POINTS="$1/DEPOT"

mkdir -p $1/LOGS
if [ -d ${ROOTMOUNT}/logs/`uname -m`/${VERSION}/${COLLECTION} ];then
	mount --bind ${ROOTMOUNT}/logs/`uname -m`/${VERSION}/${COLLECTION} $1/LOGS || error $1
else
	mount $1/LOGS || error $1
fi
MOUNT_POINTS="$MOUNT_POINTS $1/LOGS"


mkdir -p $1/sources || error $1
if [ -d ${ROOTMOUNT}/sources ]; then
	mount --bind ${ROOTMOUNT}/sources $1/sources || error $1
else
	mount $1/sources || error $1
fi
MOUNT_POINTS="$MOUNT_POINTS $1/sources"

mkdir -p $1/root/$GIT || error $1
rsync -a --exclude .git $GIT_PROJ/ $1/root/$GIT/ || error "rsync"
echo "chown -R 1000:1000 $1/root/$GIT"
chown -R 1000:1000 $1/root/$GIT/

KERNEL="NONE" LFS="$1" DEPOT="/DEPOT" install-nutyx || error $1

umount_all
remove_all "$1"

unset VERSION DEVEL
exit 0
