#!/bin/sh
#
# Copyright (C) 2011 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
# Copyright (C) 2011-2014 Paul Kocialkowski <contact@paulk.fr>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

# Variables
SCRIPT=$0
REPLICANT_DIR=$1
RELEASE_DIR=$2
RELEASE_TYPE=$3

ARGS_COUNT=2
REPO="/espace/replicant/tools/repo"
RELEASE_TOOLS_DIR="$REPLICANT_DIR/out/host/linux-x86/bin"
RELEASE_TOOLS="adb fastboot heimdall mkbootimg"
RELEASE_SDK="replicant-sdk_linux-x86.zip"
RELEASE_SDK_DIR="$REPLICANT_DIR/out/host/linux-x86/sdk"
RELEASE_IMAGES="recovery.img replicant-4.2-crespo.zip replicant-4.2-galaxysmtd.zip replicant-4.2-i9100.zip replicant-4.2-n7000.zip replicant-4.2-maguro.zip replicant-4.2-p3100.zip replicant-4.2-p3110.zip replicant-4.2-p5100.zip replicant-4.2-p5110.zip replicant-4.2-i9300.zip replicant-4.2-n7100.zip"
RELEASE_IMAGES_DIR="$REPLICANT_DIR/out/target/product"
RELEASE_DEVICES="crespo galaxysmtd i9100 n7000 maguro p3100 p3110 p5100 p5110 i9300 n7100"
RELEASE_SCRIPTS="release.sh releasediff.sh"
RELEASE_SECURITY_DIR="$REPLICANT_DIR/vendor/replicant-security"
RELEASE_SECURITY="media.x509.pem platform.x509.pem shared.x509.pem system.x509.pem"
RELEASE_KEY="4A80EB23"

INFOS="$RELEASE_DIR/infos"
SECURITY="$RELEASE_DIR/security"
SCRIPTS="$RELEASE_DIR/scripts"
TOOLS="$RELEASE_DIR/tools"
IMAGES="$RELEASE_DIR/images"
SDK="$RELEASE_DIR/sdk"

release_signatures_tools () {
	if ! [ -d "$TOOLS" ]
	then
		return
	fi

	dir=$( pwd )

	cd "$TOOLS"
	for tool in $RELEASE_TOOLS
	do
		gpg --default-key "$RELEASE_KEY" --armor --output "$tool.sig" --detach-sign "$tool"
	done
	cd "$dir"
}

release_signatures_sdk () {
	if ! [ -d "$SDK" ]
	then
		return
	fi

	dir=$( pwd )

	cd "$SDK"
	for sdk in $RELEASE_SDK
	do
		gpg --default-key "$RELEASE_KEY" --armor --output "$sdk.sig" --detach-sign "$sdk"
	done
	cd "$dir"
}

release_signatures_images () {
	if ! [ -d "$IMAGES" ]
	then
		return
	fi

	cd "$IMAGES"
	for device in $RELEASE_DEVICES
	do
		if ! [ -d "$IMAGES/$device" ]
		then
			continue
		fi

		for image in $RELEASE_IMAGES
		do
			if [ -f "$IMAGES/$device/$image" ]
			then
				gpg --default-key "$RELEASE_KEY" --armor --output "$IMAGES/$device/$image.sig" --detach-sign "$IMAGES/$device/$image"
			fi
		done
	done
}

release_signatures () {
	echo "Releasing signatures"

	release_signatures_tools
	release_signatures_sdk
	release_signatures_images

	mkdir -p "$SECURITY"
	gpg --armor --output "$SECURITY/$RELEASE_KEY.gpg" --export "$RELEASE_KEY"

	dir=$( pwd )

	cd "$SECURITY"
	rm "security.md5"
	md5sum * > "security.md5"
	cd "$dir"
}

release_images () {
	echo "Releasing images"
	mkdir -p "$IMAGES"

	for device in $RELEASE_DEVICES
	do
		echo "Releasing images for $device"
		mkdir -p "$IMAGES/$device"

		for image in $RELEASE_IMAGES
		do
			if [ -f "$RELEASE_IMAGES_DIR/$device/$image" ]
			then
				cp "$RELEASE_IMAGES_DIR/$device/$image" "$IMAGES/$device/"
			fi
		done

		dir=$( pwd )

		cd "$IMAGES/$device"
		md5sum * > "$device.md5"
		cd "$dir"
	done
}

release_sdk () {
	echo "Releasing sdk"
	mkdir -p "$SDK"

	for sdk in $RELEASE_SDK
	do
		cp "$RELEASE_SDK_DIR/$sdk" "$SDK/"
	done

	dir=$( pwd )

	cd "$SDK"
	md5sum * > "sdk.md5"
	cd "$dir"
}

release_tools () {
	echo "Releasing tools"
	mkdir -p "$TOOLS"

	for tool in $RELEASE_TOOLS
	do
		cp "$RELEASE_TOOLS_DIR/$tool" "$TOOLS/"
	done

	dir=$( pwd )

	cd "$TOOLS"
	md5sum * > "tools.md5"
	cd "$dir"
}

release_scripts () {
	echo "Releasing scripts"
	mkdir -p "$SCRIPTS"

	for script in $RELEASE_SCRIPTS
	do
		cp "$script" "$SCRIPTS/"
	done
}

release_security () {
	echo "Releasing security"
	mkdir -p "$SECURITY"

	for security in $RELEASE_SECURITY
	do
		cp "$RELEASE_SECURITY_DIR/$security" "$SECURITY/"
	done

	dir=$( pwd )

	cd "$SECURITY"
	md5sum * > "security.md5"
	cd "$dir"
}

release_infos_copying () {
	echo -e "\
Replicant is a fully free Android distribution running on several devices.\n\
\n\
The software included in Replicant is free software that is owned by various\n\
copyright holders and released under various free software licenses.\n\
For more infos, please refer to the source code of each software component.\n\
\n\
Blog: http://replicant.us/\n\
Wiki/Tracker: http://redmine.replicant.us/\n\
Source code: https://gitorious.org/replicant \
" > "$INFOS/copying.txt"
}

release_infos_date () {
	date "+%Y-%m-%d" > "$INFOS/date.txt"
}

release_infos_changelog () {
	cat "$REPLICANT_DIR/vendor/replicant/CHANGELOG.mkdn" | awk '/### .*/{n++}n==1,/### .*/'{n++}n==2 > "$INFOS/changelog.txt"
}

release_infos_manifest_version () {
	dir=$( pwd )

	cd "$REPLICANT_DIR/.repo/manifests"
	git log | head -n 1 > "$INFOS/manifest_version.txt"
	cd "$dir"
}

release_infos_git_versions () {
	dir=$( pwd )
	cd "$REPLICANT_DIR"

	$REPO list | while read git_repo
	do
		cd $git_repo
		echo -n "$git_repo" | sed "s/ : .*$/ /g"
		git log | head -n 1
		cd "$REPLICANT_DIR"
	done > "$INFOS/git_versions.txt"

	$REPO manifest -r > "$INFOS/manifest.xml"

	cd "$dir"
}

release_infos_sdk() {
	echo "Releasing infos"
	mkdir -p "$INFOS"
	release_infos_git_versions
	release_infos_manifest_version
	release_infos_date
	release_infos_copying
}

release_infos () {
	echo "Releasing infos"
	mkdir -p "$INFOS"
	release_infos_git_versions
	release_infos_manifest_version
	release_infos_changelog
	release_infos_date
	release_infos_copying
}

print_help () {
	echo "$SCRIPT: [REPLICANT_DIR] [RELEASE_DIR] (RELEASE_TYPE)"
}

# Check for the correct number of args
if [ "$#"  -lt "$ARGS_COUNT" ]
then
	print_help
	exit 1
fi

case "$RELEASE_TYPE" in
	"signatures")
		release_signatures
		;;
	"sdk")
		release_infos_sdk
		release_scripts
		release_sdk
		;;
	*)
		release_infos
		release_security
		release_scripts
		release_tools
		release_images
		;;
esac
