#!/bin/bash

[ -n "$LKP_SRC" ] ||
	export LKP_SRC=$(dirname $(dirname $(readlink -e -v $0)))
PKG_DIR="$LKP_SRC/pkg"
DEPTEST=0
UPGRADE=0
SYNC=0
QUERY=0
REMOVE=0

: ${PACMAN:=$0}
: ${BUILDDIR:=$LKP_SRC/tmp-pkg}
: ${PKGEXT:=tar.gz}
: ${DBFILE:=$BUILDDIR/installed}
export PACMAN BUILDDIR PKGEXT DBFILE

have_function() {
	func_name=$1
	declare -f "$func_name" >/dev/null
}

query() {
	:
}

pack_deb() {
	local pkg_name=$1
	mkdir -p "$INSTALL_DIR/DEBIAN"
	[ -f "$SRC_DIR/.install/.INSTALL" ] && mv "$SRC_DIR/.install/.INSTALL" "$INSTALL_DIR/DEBIAN/INSTALL"
	cat > "$INSTALL_DIR/DEBIAN/control" <<-EOF
	Package: $pkg_name-LKP
	Version: $(date +%F)
	Architecture: all
	Maintainer: LKP
	Description: LKP dependent packages
	EOF
	if have_function post_install; then
		cat > "$INSTALL_DIR/DEBIAN/postinst" <<-EOF
		#!/bin/sh
		. /var/lib/dpkg/info/$pkg_name-lkp.INSTALL
		post_install
	EOF
	chmod +x $INSTALL_DIR/DEBIAN/postinst
	fi

	# set dir permission to be exact 0755 (drwxr-xr-x) to avoid error:
	# dpkg-deb: error: control directory has bad permissions
	# 2755 (must be >=0755 and <=0775)
	chmod 0755 "$INSTALL_DIR/DEBIAN"
	chmod a-s "$INSTALL_DIR/DEBIAN"
	dpkg-deb --build "$pkg_name-lkp"
}

pack_rpm() {
	local pkg_name=$1
	mkdir -p $RPM_BUILD_DIR/{BUILD,RPMS,S{OURCE,PEC,RPM}S}
	cat > "$RPM_BUILD_DIR/SPECS/$pkg_name.spec" <<-EOF
	Name: $pkg_name-LKP
	Version: 1
	License: None
	Packager: LKP
	Release: 1
	Summary: LKP dependent packages

	%description
	LKP benchmarks rpm package

	%install
	# create directories where the files will be located
	# put the files in the relevant directories
	cp -a $INSTALL_DIR/* \$RPM_BUILD_ROOT

	%files
	%defattr(-,root,root)
	/*

	EOF

	cat > "$RPM_BUILD_DIR/.rpmmacros" <<-EOF
	%_topdir $RPM_BUILD_DIR
	%_rpmfilename %%{NAME}.rpm
	EOF

	if have_function post_install; then
		cat >> "$RPM_BUILD_DIR/SPECS/$pkg_name.spec" <<-EOF
		%post
		. $SRC_DIR/.install/.INSTALL
		post_install

	EOF
	fi

	(
		export HOME=$RPM_BUILD_DIR
		rpmbuild -bb "$RPM_BUILD_DIR/SPECS/$pkg_name.spec"
	)
}

distro_pkg_format() {
	case $DISTRO in
		debian|ubuntu)
			echo deb;;
		fedora|centos)
			echo rpm;;
		*)
			echo other;;
	esac
}

upgrade() {
	local SRC_DIR
	SRC_DIR=$(pwd)
	local pkg_name=${SRC_DIR##*/}
	local INSTALL_DIR="$SRC_DIR/$pkg_name-lkp"
	[ -d "$INSTALL_DIR" ] || mkdir "$INSTALL_DIR"
	[ -d "$SRC_DIR/.install" ] || mkdir "$SRC_DIR/.install"
	tar -zxf "$@" -C "$INSTALL_DIR"
	mv $INSTALL_DIR/.[^.]* "$SRC_DIR/.install"
	(
		[ -f "$SRC_DIR/.install/.INSTALL" ] && source "$SRC_DIR/.install/.INSTALL"
		case $(distro_pkg_format) in
			deb)
				pack_deb "$pkg_name"
				dpkg -i "$pkg_name-lkp.deb" 2>/tmp/dpkg_error || {
					grep -v "dpkg: warning: files list file for package '.*' missing;" /tmp/dpkg_error
					return 1
				}
				;;
			rpm)
				RPM_BUILD_DIR="$SRC_DIR/rpm_build"
				pack_rpm "$pkg_name"
				rpm -ivh --replacepkgs --force  "$RPM_BUILD_DIR/RPMS/$pkg_name-LKP.rpm" || return 1 ;;
			*)
				cp -r "$INSTALL_DIR"/* / || return 1 ;;
		esac
		
		echo "$pkg_name" >> "$DBFILE"
		echo install succeed
	)
}

deptest() {
	pkg=$1
	if ! grep -q -s "\b$pkg\b" "$DBFILE"; then
		echo "$pkg"
		return 127
	else
		return 0
	fi
}

sync() {
	local pkg=$1
	if [ -d "$PKG_DIR/$pkg" ]; then
		(
			cd "${PKG_DIR}/$pkg" && \
			"$LKP_SRC/sbin/makepkg" --config "$LKP_SRC/etc/makepkg.conf" -s -i --skippgpcheck
		)
	fi
	echo "$pkg" >> "$DBFILE"
}

remove() {
	:
}

case "$1" in
# Pacman Options
	-Qq)		QUERY=1;;
	-U)			UPGRADE=1;;
	-T)			DEPTEST=1;;
	-S)			SYNC=1;;
	-Rn)		REMOVE=1;;
esac
shift

if [[ DEPTEST -eq 1 ]]; then
	ret=0
	if [ ! -f "$DBFILE" ]; then
		echo "$@"
		exit 127
	fi
	for dep in "$@"; do
		deptest "$dep"
		[[ $? -eq 127 ]] && ret=127
	done
	exit $ret
fi

if [[ SYNC -eq 1 ]]; then
	shift
	for dep in "$@"; do
		sync "$dep"
	done
fi

if [[ UPGRADE -eq 1 ]]; then
	for dep in "$@"; do
		upgrade "$dep" || exit 1
	done
fi
