#!/bin/bash

MGS=172.17.32.220

declare -A git_repos=(
	[lustre-release]=git://git.hpdd.intel.com/fs/lustre-release.git
	[spl]=https://github.com/zfsonlinux/spl.git
	[zfs]=https://github.com/thegreatgazoo/zfs.git
)

declare -A git_branches=(
	[lustre-release]=b2_9
	[zfs]=draid
	# leave spl at 'master'
)

declare -a zfs_depends=(
	zlib-devel
	uuid-devel
	libuuid-devel
	libblkid-devel
	libattr-devel
)

declare -a lustre_depends=(
)

error() {
	local -i rc=$1
	shift
	echo "$@" >/dev/tty
	exit $rc
}

script="$0"
[ -f "$script" ] || script=$(type -p $0)
[ -f "$script" ] || error 1 "No script '$0'."
name=$(basename "$script")

node="$1"
[ -z "$node" ] && error 0 "Use: $0 <node>"

shift

action="$1"
completed=no
[ -z "$action" ] && action=init

has_mark() {
	test -d ~/.setup-node/. -a -f ~/.setup-node/$1
}

set_mark() {
	[ -d ~/.setup-node/. ] || mkdir ~/.setup-node
	echo $1 $(date '+%F %T.%N %z') > ~/.setup-node/$1
}

finally() {
	[ "$completed" = 'off' ] && return
	[ "$completed" = 'yes' ] && set_mark $action
}
trap finally EXIT

there() {
	ssh -t -X "root@$node" "$@"
}

perform_build() {
	[ -f configure ] || { sh ./autogen.sh; rm -f config.status; }
	[ -f config.status ] || ./configure --enable-debug --prefix=/usr "$@"
	make -j$(nproc)
}

set -e

declare -a auto_actions=(
	setup
	install_sources
	build_sources
)

echo "About to perform '$action'..." >/dev/tty
case "$action" in
init)	# this one is ran locally!
	completed=off
	ssh-copy-id "root@$node" >/dev/null 2>&1
	scp "$script" "root@$node:"
	there "./$name" "$node" auto
	;;
auto)
	yum -y update
	for a in ${auto_actions[*]}; do
		if has_mark $a; then
			echo "Action '$a' has already performed." >/dev/tty
			continue
		fi
		$0 $node $a
	done
	completed=yes
	;;
setup)
	echo "$node" > /etc/hostname
	echo -e "$MGS\tmgs" >> /etc/hosts
	hostname "$node"
	sed -i '/^SELINUX=/s/.*/SELINUX=disabled/' /etc/selinux/config
	if ! systemctl status firewalld | grep disabled; then
		systemctl stop firewalld
		systemctl disable firewalld
	fi
	yum -y groupinstall 'Development Tools'
	yum -y install epel-release
	yum -y install vim mlocate
	yum -y install ${zfs_depends[*]}
	# yum -y install ${lustre_depends[*]}

	centos_release=$(tr -s '[ \t]' '\t' < /etc/centos-release | cut -f4)
	kernel_version=$(uname -r | sed -e '1,$s/\.'`uname -m`'$//g' -)
	src_rpm="http://vault.centos.org/${centos_release}/os/Source/SPackages/kernel-${kernel_version}.src.rpm"
	# rpm -ivh "$src_rpm"

	completed=yes
	;;
install_sources)
	for prog in spl zfs lustre-release; do
		url=${git_repos[$prog]}
		cd ~
		[ -d "$prog/." ] && { echo "'$prog' is already here.">/dev/tty; continue; }
		echo "Cloning '$prog' from '$url' ..." >/dev/tty
		git clone "$url"
		cd $prog
		git fetch --all

		branch=${git_branches[$prog]}
		[ -n "$branch" ] && { git checkout "$branch"; git pull; }
	done
	completed=yes
	;;
build_sources)
	cd ~
	cwd=$(pwd)
	for prog in spl zfs lustre-release; do
		echo "About to build '$prog'...">/dev/tty
		cd ~/$prog/
		declare -a extra_args=()

		case $prog in # specific setup
		spl)	# automagically configured
			;;
		zfs)	# use 'with-linux' as it was in spl build
			krn_src=$(grep '^LINUX = ' $cwd/spl/Makefile | tr -d '[ \t]' | cut -d= -f2)
			extra_args=(	"--with-linux=${krn_src}"	"--with-spl=$cwd/spl"	)
			;;
		lustre-release) # use 'with-linux' as it was in zfs build
			krn_src=$(grep '^LINUX = ' $cwd/zfs/Makefile | tr -d '[ \t]' | cut -d= -f2)
			extra_args=(	"--with-linux=${krn_src}"	"--disable-ldiskfs"
					"--with-spl=$cwd/spl"		"--with-zfs=$cwd/zfs"	)
			;;
		esac

		perform_build ${extra_args[*]}

		case $prog in # specific setup
		spl)	cat $cwd/$prog/$prog.release;;
		zfs)	cat $cwd/$prog/$prog.release;;
		lustre-release) cat $cwd/$prog/LUSTRE-VERSION-FILE;;
		esac
	done
	completed=yes
	;;
install_bins)
	cd ~
	cwd=$(pwd)
	for prog in spl zfs lustre-release; do
		echo "About to install '$prog'...">/dev/tty
		make -s -C ~/$prog/ install
	done
	ldconfig
	modprobe -v zfs
	completed=off
	;;
*)	error 1 "Cannot perform '$action'.";;
esac

echo "Now you may 'ssh root@$node' after '$action' now.">/dev/tty

# EOF #