# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=4

inherit eutils

VERSION_MUSL_CROSS="843d4b7e9187"
VERSION_BINUTILS="2.22"
VERSION_GCC="4.7.2"
VERSION_LINUX="3.2.32"
VERSION_MUSL="0.9.8"
VERSION_ZLIB="1.2.7"
VERSION_TERMCAP="1.3.1"
VERSION_READLINE="6.2"
VERSION_ICONV="1.14"
VERSION_SFTPSERVER="0.1"
VERSION_DROPBEAR="2012.55"
VERSION_BUSYBOX="1.20.2"

DESCRIPTION="Recovery Initramfs Overlay"
HOMEPAGE="http://sablink.disconnected-by-peer.at"

SRC_URI="https://bitbucket.org/GregorR/musl-cross/get/${VERSION_MUSL_CROSS}.zip
	mirror://gnu/binutils/binutils-${VERSION_BINUTILS}.tar.bz2
	mirror://gnu/gcc/gcc-4.7.2/gcc-${VERSION_GCC}.tar.bz2
	mirror://kernel/linux/kernel/v3.0/linux-${VERSION_LINUX}.tar.bz2
	http://www.musl-libc.org/releases/musl-${VERSION_MUSL}.tar.gz
	http://prdownloads.sourceforge.net/libpng/zlib-${VERSION_ZLIB}.tar.gz
	http://www.catb.org/~esr/terminfo/termtypes.tc.gz
	mirror://gnu/termcap/termcap-${VERSION_TERMCAP}.tar.gz
	mirror://gnu/libiconv/libiconv-${VERSION_ICONV}.tar.gz
	mirror://gnu/readline/readline-${VERSION_READLINE}.tar.gz
	http://www.greenend.org.uk/rjk/sftpserver/sftpserver-${VERSION_SFTPSERVER}.tar.gz
	https://matt.ucc.asn.au/dropbear/dropbear-${VERSION_DROPBEAR}.tar.gz
	http://busybox.net/downloads/busybox-${VERSION_BUSYBOX}.tar.bz2
	ftp://oss.sgi.com/projects/xfs/cmd_tars/xfsprogs-3.1.8.tar.gz
	http://jfs.sourceforge.net/project/pub/jfsutils-1.1.15.tar.gz
	mirror://gentoo/btrfs-progs-0.19.11.tar.bz2
	http://sourceforge.net/projects/e2fsprogs/files/e2fsprogs/v1.42.6/e2fsprogs-libs-1.42.6.tar.gz
	http://sourceforge.net/projects/e2fsprogs/files/e2fsprogs/v1.42.6/e2fsprogs-1.42.6.tar.gz
	http://ftp.kernel.org/pub/linux/kernel/people/jeffm/reiserfsprogs/v3.6.22/reiserfsprogs-3.6.22.tar.bz2
	http://sourceforge.net/projects/reiser4/files/reiser4-utils/libaal/libaal-1.0.5.tar.gz
	http://sourceforge.net/projects/reiser4/files/reiser4-utils/reiser4progs/reiser4progs-1.0.7.tar.gz
	"

LICENSE="GPL-2 BSD"
SLOT="0"
KEYWORDS=""
IUSE=""
RESTRICT=""
DEPEND=""
RDEPEND="${DEPEND}"

S=${WORKDIR}
DDIR=${WORKDIR}/${PN}
IDIR=/usr/share/${PN}

pkg_setup() {
	# This is just for initd so no need for the lib32 lib64 split
	# so overwrite the LIBDIR_${ARCH} to lib
	# Please keep this in future versions
	export LIBDIR_${ARCH}="lib"

	if [[ ${CHOST/-*/} = "armv5tel" ]]; then
		MUSL_ARCH="arm"
	elif [[ ${CHOST/-*/} = "armv6j" ]]; then
		MUSL_ARCH="arm"
	elif [[ ${CHOST/-*/} = "armv7a" ]]; then
		MUSL_ARCH="arm"
	elif [[ ${ARCH} = "x86" ]]; then
		MUSL_ARCH="i386"
	elif [[ ${ARCH} = "amd64" ]]; then
		MUSL_ARCH="${CHOST/-*/}"
	fi

}


src_prepare() {
	# prepare croos toolchain
	pushd ${WORKDIR}/GregorR-musl-cross-${VERSION_MUSL_CROSS}
	cd tarballs
	ln -sf ${DISTDIR}/binutils-${VERSION_BINUTILS}.tar.bz2 .
	ln -sf ${DISTDIR}/gcc-${VERSION_GCC}.tar.bz2 .
	ln -sf ${DISTDIR}/linux-${VERSION_LINUX}.tar.bz2 .
	ln -sf ${DISTDIR}/musl-${VERSION_MUSL}.tar.gz .
	popd

	# nothing to prepare in zlib
	#pushd ${WORKDIR}/zlib-${VERSION_ZLIB}
	#popd

	# prepare in termcap
	pushd ${WORKDIR}/termcap-${VERSION_TERMCAP}
	EPATCH_SOURCE="${FILESDIR}/termcap/${VERSION_TERMCAP}"
	EPATCH_SUFFIX="patch"
	epatch "${EPATCH_SOURCE}"
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub .
	popd
	mv termtypes.tc termcap || die
	epatch "${EPATCH_SOURCE}"/tc.file

	# add redline patches
	pushd ${WORKDIR}/readline-${VERSION_READLINE}
	EPATCH_SOURCE="${FILESDIR}/readline/${VERSION_READLINE}"
	EPATCH_SUFFIX="patch"
	epatch "${EPATCH_SOURCE}"
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub support/
	popd

	# prepare in iconv
	pushd ${WORKDIR}/libiconv-${VERSION_ICONV}
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub build-aux/
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub libcharset/build-aux/
	popd

	# prepare in sftpserver
	pushd ${WORKDIR}/sftpserver-${VERSION_SFTPSERVER}
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub config.aux/
	popd

	# prepare in dropbear
	pushd ${WORKDIR}/dropbear-${VERSION_DROPBEAR}
	EPATCH_SOURCE="${FILESDIR}/dropbear/${VERSION_DROPBEAR}"
	EPATCH_SUFFIX="patch"
	epatch "${EPATCH_SOURCE}"
	cp ../GregorR-musl-cross-${VERSION_MUSL_CROSS}/extra/config.sub .
	popd

#	pushd ${WORKDIR}/
#	popd
}

src_compile() {
	# configure compile install musl for later use
	pushd ${WORKDIR}/GregorR-musl-cross-${VERSION_MUSL_CROSS}
	echo "CC_BASE_PREFIX=${DDIR}" > config.sh
	echo "MAKEFLAGS=${MAKEFLAGS}" >> config.sh
	echo "ARCH=${CHOST/-*/}" >> config.sh
	# echo "GCC_BUILTIN_PREREQS=yes" >> config.sh
	./build.sh
	# export all the overwrites for the followd compiles
	export PATH="${DDIR}/bin:${DDIR}/${CHOST/-*/}-linux-musl/bin:$PATH"
	export CC=${CHOST/-*/}-linux-musl-gcc
	export CXX=${CHOST/-*/}-linux-musl-g++
	export LD_LIBRARY_PATH="${DDIR}/lib:${DDIR}/${CHOST/-*/}-linux-musl/${CHOST/-*/}-linux-musl/lib64:${DDIR}/${CHOST/-*/}-linux-musl/${CHOST/-*/}-linux-musl/lib"
	export LDFLAGS="-Wl,--as-needed -L${DDIR}/lib"
	export CFLAGS="-Os -I${DDIR}/include"
	export CPPFLAGS="-Os -I${DDIR}/include"
	popd

	# configure compile install musl libc for later use
	pushd ${WORKDIR}/musl-${VERSION_MUSL}
	./configure --prefix=${DDIR} --syslibdir=${DDIR}/lib --host=${CHOST/-*/}-linux-musl --build=${CHOST/-*/}-linux-musl
	emake
	emake install
	popd

	# configure compile install zlib for later use
	pushd ${WORKDIR}/zlib-${VERSION_ZLIB}
	CC=${CHOST/-*/}-linux-musl-gcc ./configure --prefix=${DDIR} --shared
	emake
	emake install
	popd

	# configure compile install termcap for later use
	# ncurses would be to much for the initramfs so just termcap
	pushd ${WORKDIR}/termcap-${VERSION_TERMCAP}
	./configure --prefix=${DDIR} --host="${CHOST/-*/}-linux-musl" --build="${CHOST/-*/}-linux-musl"
	emake
	emake install
	popd

	# configure compile install readline for later use
	pushd ${WORKDIR}/readline-${VERSION_READLINE}
	LIBS="-ltermcap" ./configure --prefix="${DDIR}" --host="${CHOST/-*/}-linux-musl" --build="${CHOST/-*/}-linux-musl"
	emake
	emake install
	popd

	# configure compile install libiconv for later use
	pushd ${WORKDIR}/libiconv-${VERSION_ICONV}
	./configure --prefix="${DDIR}" --host="${CHOST/-*/}-linux-musl" --build="${CHOST/-*/}-linux-musl"
	emake
	emake install
	popd

	# configure compile install sftpserver
	pushd ${WORKDIR}/sftpserver-${VERSION_SFTPSERVER}
	# great autodetect not working so overwrite libs to link
	LIBS="-lreadline -liconv -ltermcap -lz" ./configure --prefix="${DDIR}" \
		--enable-reversed-symlink --host="${CHOST/-*/}-linux-musl" --build="${CHOST/-*/}-linux-musl"
	emake
	emake install
	# is not installed automatically
	cp sftpclient "${DDIR}"/bin
	popd

	# configure compile install dropbear wit all supported programms
	pushd ${WORKDIR}/dropbear-${VERSION_DROPBEAR}
	./configure --prefix="${DDIR}" --with-zlib="${DDIR}" --host="${CHOST/-*/}-linux-musl" --build="${CHOST/-*/}-linux-musl"
	emake PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp"
	emake PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" install
	popd
}

src_install() {
	into "${IDIR}"
	# install libc and dependencies
	dolib.so "${DDIR}"/lib/libc.so
	dolib.so "${DDIR}"/${CHOST/-*/}-linux-musl/${CHOST/-*/}-linux-musl/lib*/*.so*
	ln -sf libc.so "${IDIR}"/lib/ld-musl-${MUSL_ARCH}.so.1
	echo "/lib" > "${IDIR}"/etc/ld-musl-${MUSL_ARCH}.path

	# install zlib
	dolib.so "${DDIR}"/lib/libz.so*

	# install termcap
	dolib.so "${DDIR}"/lib/libtermcap.so*
	insinto "${IDIR}"/etc
	doins "${WORKDIR}"/termcap || die

	# install readline
	dolib.so "${DDIR}"/lib/libcharset.so*
	dolib.so "${DDIR}"/lib/libhistory.so*
	dolib.so "${DDIR}"/lib/libreadline.so*
	insinto "${IDIR}"/lib
	doins "${DDIR}"/lib/charset.alias

	# install iconv
	dolib.so "${DDIR}"/lib/libiconv.so*
	dobin "${DDIR}"/bin/iconv

	# install sftpserver and client
	dobin "${DDIR}"/bin/sftpclient
	exeinto "${IDIR}"/libexec
	# dropbear expects sftp to be specialy named
	newexe "${DDIR}"/libexec/gesftpserver sftp-server

	# insatll dropbear
	dobin "${DDIR}"/bin/{dbclient,dropbearconvert,dropbearkey,scp}
	dosbin "${DDIR}"/sbin/dropbear
}
