#!/bin/sh
#
# Copyright (C) 2011 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
# Copyright (C) 2011-2012 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

ARGS_COUNT=2
REPO="/home/replicant/tools/repo"
RELEASE_TOOLS_DIR="$REPLICANT_DIR/out/host/linux-x86/bin"
RELEASE_TOOLS="adb fastboot mkbootimg"
RELEASE_IMAGES="bootloader.img boot.img recovery.img system.img system.yaffs2.img system.ext4.img system.tar.bz2 userdata.img data.ext4.img boot.scr replicant_gta04_install.sh"
RELEASE_IMAGES_DIR="$REPLICANT_DIR/out/target/product"
RELEASE_DEVICES="crespo galaxys2 galaxysmtd maguro"

INFOS="$RELEASE_DIR/infos"
SCRIPTS="$RELEASE_DIR/scripts"
TOOLS="$RELEASE_DIR/tools"
IMAGES="$RELEASE_DIR/images"

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_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"
	cp "$SCRIPT" "$SCRIPTS/"
}

release_infos_copying () {
	echo "\
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"

	cd "$dir"
}

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]"
}

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

release_infos
release_scripts
release_tools
release_images
