#!/bin/bash

[[ $LKP_SRC ]] || export LKP_SRC="$(dirname $(dirname $(readlink -e -v $0)))"

. $LKP_SRC/lib/detect-system.sh

usage()
{
	cat <<-EOF

		LKP provides $0 to generate locally and support upload to LKP server the customized installation of external programs and/or system packages. The generated package could be in .cgz(default) or .deb(-d option) formats.
		lkp-tests/pack/default implements default behavior of $0.
		Based on lkp-tests/pack/default, a script is named after an external program, and overrides default implementation of download(), build(), and install() functions.
		The script named after an external program works in conjunction with $0

		Usage: $0 [-a <arch>] [-c] [-f] [-d <distro>] <BM_NAME>

		[-a <arch>]       Optional parameter: specific architecture of <BM_NAME>, used as suffixe in .cgz file name, overrides default value.
		[-c]              Optional parameter: work environment is cleaned up after execution.
		[-d <distro>]     Optional parameter: If specified, will generates customized installation in .deb/.rpm format (default is .cgz format) for <BM_NAME> - simplifies later installation on debian systems.
		[-s <dest>]       Optional parameter: If specified, will sync generated package to destination given.
		<BM_NAME>         Mandatory parameter: generates customized installation package for <BM_NAME>, default architecture of the compiling machine is used as suffixe in .cgz file name
		[-f]              Optional parameter: force to build benchmark, if this mode is not specified, we will skip this packing if we have packed it before (check whether /lkb/benchmark/BM_NAME is empty or not)
		Note:
		[-a <arch>] and [-d <distro>] optional parameters do not work together, and <arch> cannot be used as suffixe in .deb file name.

		Examples:
		$ pack my_program
		Output: /tmp/build-my_program/my_program/
		Output: /lkp/benchmarks/my_program/
		Output: /lkp/benchmarks/my_program-<default_arch>.cgz
		Output: inn:/lkp/benchmarks/my_program-<default_arch>.cgz

		$ pack -a my_arch -c my_program
		Output: /lkp/benchmarks/my_program/
		Output: /lkp/benchmarks/my_program-my_arch.cgz
		Output: inn:/lkp/benchmarks/my_program-my_arch.cgz

		$ pack -d debian my_program
		Output: /tmp/build-my_program/my_program/
		Output: /tmp/my-program-LKP/
		Output: /tmp/my-program-LKP.deb
		Output: /lkp/benchmarks/my_program/

	EOF
	exit 1
}

while getopts "a:cfd:s:" opt
do
	case $opt in
	a ) arch="$OPTARG" ;;
	c ) opt_clean=true ;;
	d ) distro="$OPTARG" ;;
	s ) sync_dest="$OPTARG" ;;
	f ) force_mode=true ;;
	? ) usage ;;
	esac
done

shift $(($OPTIND-1))
BM_NAME=$1
LINK_NAME=$BM_NAME
[[ $BM_NAME ]] || usage

[[ ! $force_mode ]] && ls /lkp/benchmarks/$BM_NAME/* &>/dev/null && {
	echo "$BM_NAME is already installed into /lkp/benchmarks/$BM_NAME, skip this packing"
	exit 0
}

[[ $arch ]] || arch=$(get_system_arch)

# for packages with same dependencies, force the update instead of generating link
# due to schedule stage will auto detect symbolic link, so no need generate new link
check_shared_pack()
{
	[[ -L $LKP_SRC/pack/$BM_NAME ]] || return
	[[ -z "$sync_dest" ]] && exit

	local target
	target=$(readlink $LKP_SRC/pack/$BM_NAME)
	target=$(basename $target)

	BM_NAME="$target"
}

[[ "$LKP_LOCAL_RUN" != "1" ]] && check_shared_pack

BM_ROOT=/lkp/benchmarks/$BM_NAME

source $LKP_SRC/lib/install.sh
source $LKP_SRC/pack/$BM_NAME || exit

if [[ "$BM_NAME" != "lkp-src" ]]; then
	rm -fr "$BM_ROOT"
	mkdir -p $BM_ROOT || exit
fi

# If there exists a patch for the benchmark, delete the tmp file of the
# benchmark. Then redownload the source code of the benchmark to restore the
# version of the source code to avoid error in patching.
BUILD_DIR=$(get_build_dir $BM_NAME)
[[ -f $LKP_SRC/pack/${BM_NAME}.patch ]] && [[ -d $BUILD_DIR ]] && \
	rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
cd $BUILD_DIR
download || exit
cd "$source_dir" || exit
[[ -n "$sync_dest" ]] && commit=$(git rev-parse --short HEAD 2>/dev/null)
build || exit
install || exit

pack

[[ -n $sync_dest ]] && {
	date=$(date +"%Y%m%d")
	cgz_name="${BM_NAME}-${arch}-${commit}_${date}.cgz"
	rsync -av "/lkp/benchmarks/${BM_NAME}-${arch}.cgz" "$sync_dest/$cgz_name" || exit
	[[ "$BM_NAME" != "$LINK_NAME" ]] && {
	    ln -sf "$cgz_name" "$sync_dest/${LINK_NAME}-${arch}.cgz" || exit
	}
	ln -sf "$cgz_name" "$sync_dest/${BM_NAME}-${arch}.cgz" || exit
}

[[ $opt_clean ]] && {
    cleanup || exit
}

exit 0
