#!/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
 - base-extra
 - cli
 - cli-extra
 - gui
 - gui-extra
 - gnome
 - xfce4
 - kde5
 - mate
 - enlightenment

 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/remote),
    another 10 Gbytes of free space for your /srv/local folder
    and another 6 to 8 Gbytes for your  (temporary) installed NuTyX.
    Also be aware that /srv/local/sources will continuously grow
    until you delete some sources / package files

 c. The installed NuTyX should ALWAYS be under /development or /stable.
    It means if you have to use another (bigger) partition for example
    then you should use a link to it:
    Example:

       ln -sv /home/thierry/chroot-64/stable /stable

    Replaces 'stable' with 'development' if you plan to build
    against the development version of the NuTyX binaries.
    In this example the installed NuTyX will always ends up in
    /home/thierry/chroot-64/development/xxx which is fine because
    its another bigger partition (about 740 Gb in this case).
 
 d. They should be TWO copies of the binaries depots packages.
    One is the original set and is located in '/srv/remote' folder.
    Its a copy of the official availables packages.
    This set can be resync (to receive any updates from upstream)
    automaticaly every hour for example.

    The second set is located in '/srv/local' folder.
    This set is your working copy.
    Means as soon you start some compilations,
    this set will be out of sync with the official one.


 2. HOW DOES THIS WORK ?

  The script will do following steps:

  - install a development 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 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, base-extra, cli, cli-extra, gui, gui-extra, enlightenment,
  gnome, kde5, mate or xfce4.
  You note that even under kde5 (for example)
  we have two collection kde5 and kde5-extra, even then the script
  will automatically build both

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

   build-collection /development/base-extra kernel

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

 3. CONFIGURATION OF THE HOST NuTyX

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

 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
 As we used predefined folders (/DEPOT, /sources, /LOGS)
 we specify all of them in /etc/fstab file:

 Example for the cli-extra collection of the development version
 you should specify following lines:

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

  Copy and adapt thoses four lines for every collection you planned to build.
  Adapt '/home/thierry/git/extra' to your 'extra' git.
  Replace (or add) stable version in case you plan to build
  on the 'stable' version.
  Replace 'x86_64' with 'i686' if you plan to build on a 32 bits platform.
  

 4. Setup all the needed folders and get the official depot into the
    /srv/remote location

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

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

  Replace 'development' with 'stable' if you plan to build on the
  'stable' version.
  Replace 'cli-extra' collection with the one you plan to build

  To get all the binaries locally and used them (highly recommended)
  we need to rsync them, following command
  will do that (takes quite a bit of time the first time):

  rsync -av --delete  \
  rsync://downloads.nutyx.org/nutyx/x86_64/development/ \
  /srv/remote/x86_64/development/

 5. GET the tool

   get rsync

 6. RSYNC the remote depot with your local depot

  You will normaly use this command only ones or every time
  you want to overwrite your work with the official depot.

     rsync -av --delete \
    /srv/remote/x86_64/development/ \
    /srv/local/x86_64/development/

  You are now ready to work.

 7. DONE
  As you are using YOUR COPY of the depot its up to you to rerun
  the rsync command from point 6, means you want to get rid of your work
  you did so far :)

 Enjoy NuTyX :)

******************************************************************************
'|more
}
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/$CHROOT/etc/pkgmk.conf << "EOF"
export CFLAGS="-O2 -pipe"
export CXXFLAGS="${CFLAGS}"
export MAKEFLAGS="-j8"
PKGMK_GROUPS=(devel man doc service)
PKGMK_LOCALES=(fr de it es nl pt da nn sv fi tr ru)
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/$CHROOT"
	if [ "$CHROOT" == "enlightenment" ]; then
		echo "dir /DEPOT/enlightenment-extra
dir /DEPOT/enlightenment
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "xfce4" ]; then
		echo "dir /DEPOT/xfce4-extra
dir /DEPOT/xfce4
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "mate" ]; then
		echo "dir /DEPOT/mate-extra
dir /DEPOT/mate
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "gnome" ]; then
		echo "dir /DEPOT/gnome-extra
dir /DEPOT/gnome
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base-extra
dir /DEPOT/base
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "kde5" ]; then
		echo "dir /DEPOT/kde5-extra
dir /DEPOT/kde5
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "base" ]; then
		COLLECTION="core"
		echo "dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1

	fi
	if [ "$CHROOT" == "cli" ]; then
		COLLECTION="core"
		echo "dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "gui" ]; then
		COLLECTION="core"
		echo "dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/base-extra
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "systemd" ]; then
		COLLECTION="core"
		echo "dir /DEPOT/systemd
dir /DEPOT/gui
dir /DEPOT/cli
dir /DEPOT/base
dir /DEPOT/base-extra
base /DEPOT/base
base /DEPOT/cli
base /DEPOT/gui
base /DEPOT/systemd
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "base-extra" ]; then
		COLLECTION="extra"
		echo "dir /DEPOT/base-extra
dir /DEPOT/base
base /DEPOT/base
base /DEPOT/base-extra" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "cli-extra" ]; then
		COLLECTION="extra"
		echo "dir /DEPOT/cli-extra
dir /DEPOT/cli
dir /DEPOT/base-extra
dir /DEPOT/base
base /DEPOT/base
base /DEPOT/base-extra
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
	if [ "$CHROOT" == "gui-extra" ]; then
		COLLECTION="extra"
		echo "dir /DEPOT/gui-extra
dir /DEPOT/gui
dir /DEPOT/cli-extra
dir /DEPOT/cli
dir /DEPOT/base-extra
dir /DEPOT/base
base /DEPOT/base
logdir /LOGS" > $DIR/etc/cards.conf || exit 1
	fi
}

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
	usage
	mount | grep $1
	echo "
>>>	$1 exist ...
"
	exit 1
fi

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

mkdir -p $1/etc || exit 1

if [ "$CHROOT" == "base" ] \
|| [ "$CHROOT" == "cli"  ] \
|| [ "$CHROOT" == "gui"  ]; then
	setup_collection
	DEVEL="cards.devel \
	&& cd /root/$COLLECTION \
	&& bash scripts/$CHROOT"
fi
if [ "$CHROOT" == "systemd"  ]; then
	setup_collection
	DEVEL="cards.devel && pkgrm eudev && pkgrm eudev.devel \
	&& cards install systemd && cards install systemd.devel \
	&& cd /root/$COLLECTION && bash scripts/$CHROOT"
fi
if [ "$CHROOT" == "base-extra" ] \
|| [ "$CHROOT" == "cli-extra"  ] \
|| [ "$CHROOT" == "gui-extra"  ]; then
	setup_collection
	DEVEL="cards.devel \
	&& cd /root/$COLLECTION \
	&& bash scripts/$CHROOT"
fi
if [ "$COLLECTION" != "core" ] \
&& [ "$COLLECTION" != "extra"  ]; then
	setup_collection
	DEVEL="cards.devel \
	&& cd /root/$CHROOT \
	&& bash scripts/$COLLECTION -a \
	&& bash scripts/$COLLECTION-extra"
fi

setup_pkgmk_conf


if [ $# -gt 1 ]; then
	DEVEL="$DEVEL $2"
else
	DEVEL="$DEVEL -a"
fi

export DEVEL VERSION

echo "CHROOT: $CHROOT"
echo "COLLECTION: $COLLECTION"
echo "VERSION: $VERSION"
echo "The command: KERNEL="NONE" LFS="$1" DEPOT="/DEPOT" install-nutyx"
echo -n "DEVEL: $DEVEL "
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
mount $1/DEPOT || error $1
MOUNT_POINTS="$1/DEPOT"

mkdir -p $1/LOGS || error $1
mount $1/LOGS || error $1
MOUNT_POINTS="$MOUNT_POINTS $1/LOGS"

mkdir -p $1/sources || error $1
mount $1/sources || error $1
MOUNT_POINTS="$MOUNT_POINTS $1/sources"

mkdir -p $1/root/$COLLECTION || error $1
mount $1/root/$COLLECTION || error $1
MOUNT_POINTS="$MOUNT_POINTS $1/root/$COLLECTION"

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

umount_all
remove_all "$1"

unset VERSION DEVEL
exit 0
