#!/bin/bash

# Path to molecules.git dir
SABAYON_MOLECULE_HOME="${SABAYON_MOLECULE_HOME:-/sabayon}"
export SABAYON_MOLECULE_HOME

ACTION="${1}"
if [ "${ACTION}" != "daily" ] && [ "${ACTION}" != "weekly" ] && [ "${ACTION}" != "dailybase" ]; then
	echo "invalid action: ${ACTION}" >&2
	exit 1
fi
shift

for arg in "$@"
do
	[[ "${arg}" = "--push" ]] && DO_PUSH="1"
	[[ "${arg}" = "--stdout" ]] && DO_STDOUT="1"
	[[ "${arg}" = "--sleepnight" ]] && DO_SLEEPNIGHT="1"
	if [ "${arg}" = "--pushonly" ]; then
		DO_PUSH="1"
		DRY_RUN="1"
	fi
done

CUR_DATE=$(date -u +%Y%m%d)
LOG_FILE="/var/log/molecule/autobuild-${CUR_DATE}-${$}.log"
BUILDING_DAILY=1
MAKE_TORRENTS="${MAKE_TORRENTS:-0}"
DAILY_TMPDIR=

# to make ISO remaster spec files working (pre_iso_script)
export CUR_DATE
export ETP_NONINTERACTIVE=1
export BUILDING_DAILY
# Temporarily added to debug Equo in case of deadlock
# export ETP_DEBUG_WATCHDOG=1
# export ETP_DEBUG_WATCHDOG_INTERVAL=60

echo "DO_PUSH=${DO_PUSH}"
echo "DRY_RUN=${DRY_RUN}"
echo "DO_SLEEPNIGHT=${DO_SLEEPNIGHT}"
echo "LOG_FILE=${LOG_FILE}"

# setup default language, cron might not do that
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"

# Sleep until 22pm?
if [ "${DO_SLEEPNIGHT}" = "1" ] && [ "${DO_PUSH}" = "1" ]; then
	target_h=22 # 22pm
	current_h=$(date +%H)
	current_h=${current_h/0} # remove leading 0
	delta_h=$(( target_h - current_h ))
	if [ ${current_h} -ge 0 ] && [ ${current_h} -le 6 ]; then
		# If it's past midnight and no later than 7am
		# just push
		echo "Just pusing out now"
	elif [ ${delta_h} -gt 0 ]; then
		delta_s=$(( delta_h * 3600 ))
		echo "Sleeping for ${delta_h} hours..."
		sleep ${delta_s} || exit 1
	elif [ ${delta_h} -lt 0 ]; then
		# between 22 and 24, run!
		echo "I'm after 22pm, running"
	else
		echo "No need to sleep"
	fi
fi

ARM_SOURCE_SPECS=()
ARM_SOURCE_SPECS_IMG=()

SOURCE_SPECS=()
SOURCE_SPECS_ISO=()

REMASTER_SPECS=()
REMASTER_SPECS_ISO=()
REMASTER_TAR_SPECS=()
REMASTER_TAR_SPECS_TAR=()

if [ "${ACTION}" = "weekly" ] || [ "${ACTION}" = "daily" ]; then

	# Weekly molecules
	if [ "${ACTION}" = "weekly" ]; then
		ARM_SOURCE_SPECS+=(
			"sablink-armel-buffalo_kb_pro-20G.spec"
			"sablink-armel-buffalo_ls_chlv2-20G.spec"
			"sablink-armel-buffalo_ls_pro_live-20G.spec"
			"sablink-armel-buffalo_ls_xhl-20G.spec"
		)
		ARM_SOURCE_SPECS_IMG+=(
			"SabLink_Linux_0_armelv5tel_buffalo_kb_pro_20GB.img"
			"SabLink_Linux_0_armelv5tel_buffalo_ls_pro_live_20GB.img"
			"SabLink_Linux_0_armelv5tel_buffalo_ls_xhl_20GB.img"
			"SabLink_Linux_0_armelv5tel_buffalo_ls_chlv2_20GB.img"
		)
	fi

fi

[[ -d "/var/log/molecule" ]] || mkdir -p /var/log/molecule

cleanup_on_exit() {
	if [ -n "${DAILY_TMPDIR}" ] && [ -d "${DAILY_TMPDIR}" ]; then
		rm -rf "${DAILY_TMPDIR}"
		# don't care about races
		DAILY_TMPDIR=""
	fi
}
trap "cleanup_on_exit" EXIT INT TERM

move_to_pkg_sablink_org() {
	if [ -n "${DO_PUSH}" ] || [ -f "${SABAYON_MOLECULE_HOME}"/DO_PUSH ]; then
		rm -f "${SABAYON_MOLECULE_HOME}"/DO_PUSH
		local executed=
		for ((i=0; i < 5; i++)); do
#			rsync -av --partial --delete-excluded "${SABAYON_MOLECULE_HOME}"/images_ftpsync/*DAILY* \
#				entropy@pkg.sabayon.org:/sabayon/rsync/rsync.sabayon.org/iso/daily \
#				|| { sleep 10; continue; }
#			rsync -av --partial --delete-excluded "${SABAYON_MOLECULE_HOME}"/scripts/gen_html \
#			entropy@pkg.sabayon.org:/sabayon/rsync/iso_html_generator \
#				|| { sleep 10; continue; }
#			ssh entropy@pkg.sabayon.org \
#				/sabayon/rsync/iso_html_generator/gen_html/gen.sh \
#				|| { sleep 10; continue; }
			ftpsync "${SABAYON_MOLECULE_HOME}"/images_ftpsync/ ¸
				ftp://sablink:`cat ${SABAYON_MOLECULE_HOME}"/pkgs.sablink.n-c.org`@ftp.nas-central.org/images
			executed=1
			break
		done
		[[ -n "${executed}" ]] && return 0
		return 1
	fi
	return 0
}

build_sablink() {
	if [ -z "${DRY_RUN}" ]; then

		DAILY_TMPDIR=$(mktemp -d --suffix=.iso_build.sh --tmpdir=/tmp)
		[[ -z "${DAILY_TMPDIR}" ]] && return 1
		DAILY_TMPDIR_REMASTER="${DAILY_TMPDIR}/remaster"
		mkdir "${DAILY_TMPDIR_REMASTER}" || return 1

		local arm_source_specs=""
		for i in ${!ARM_SOURCE_SPECS[@]}
		do
			src="${SABAYON_MOLECULE_HOME}/molecules/${ARM_SOURCE_SPECS[i]}"
			dst="${DAILY_TMPDIR}/${ARM_SOURCE_SPECS[i]}"
			cp "${src}" "${dst}" -p || return 1
			echo >> "${dst}"
			echo "inner_source_chroot_script: ${SABAYON_MOLECULE_HOME}/scripts/inner_source_chroot_update.sh" >> "${dst}"
			# tweak iso image name
			sed -i "s/^#.*image_name/image_name:/" "${dst}" || return 1
			sed -i "s/image_name.*/image_name: ${ARM_SOURCE_SPECS_IMG[i]}/" "${dst}" || return 1
			# tweak release version
			sed -i "s/release_version.*/release_version: ${CUR_DATE}/" "${dst}" || return 1
			echo "${dst}: image: ${ARM_SOURCE_SPECS_IMG[i]} date: ${CUR_DATE}"
			arm_source_specs+="${dst} "
		done

		local done_images=0
		local done_something=0
		if [ -n "${arm_source_specs}" ]; then
			molecule --nocolor ${arm_source_specs} || return 1
			done_something=1
			done_images=1
		fi

		# package phases keep loading dbus, let's kill pids back
		ps ax | grep -- "/usr/bin/dbus-daemon --fork .* --session" | awk '{ print $1 }' | xargs kill 2> /dev/null

		if [ "${done_something}" = "1" ]; then
			if [ "${done_images}" = "1" ]; then
				cp -p "${SABAYON_MOLECULE_HOME}"/images/* "${SABAYON_MOLECULE_HOME}"/images_ftpsync/ || return 1
			fi
			date > "${SABAYON_MOLECULE_HOME}"/images_ftpsync/RELEASE_DATE_WEEKLY
#			if [ "${MAKE_TORRENTS}" != "0" ]; then
#				"${SABAYON_MOLECULE_HOME}"/scripts/make_torrents.sh || return 1
#			fi
		fi
	fi
	return 0
}

mail_failure() {
	local out=${1}
	local log_file=${2}
	echo "Hello there,
iso_build.sh execution failed (miserably) with exit status: ${out}.
Log file is at ${log_file}.

Thanks,
Sun" | /bin/mail -s "ISO build script failure" root
}

out="0"
if [ -n "${DO_STDOUT}" ]; then
	build_sablink
	out=${?}
	if [ "${out}" = "0" ]; then
		move_to_pkg_sablink_org
		out=${?}
	fi
else
	log_file="/var/log/molecule/autobuild-${CUR_DATE}-${$}.log"
	build_sablink &> "${log_file}"
	out=${?}
	if [ "${out}" = "0" ]; then
		move_to_pkg_sablink_org &>> "${log_file}"
		out=${?}
	fi
	if [ "${out}" != "0" ]; then
		# mail root
		mail_failure "${out}" "${log_file}"
	fi
fi
echo "EXIT_STATUS: ${out}"

exit ${out}
