#!/bin/bash
# Copyright (c) 2000-2015 Synology Inc. All rights reserved.

##
# parted only build for 2.6 series
##
case ${MakeClean} in
	[Yy][Ee][Ss])
		if [ -f Makefile ]; then
			make distclean
		fi
		;;
esac

case ${CleanOnly} in
	[Yy][Ee][Ss])
		return
		;;
esac

LINUXDIR="${SysRootLib}/modules/${SYNO_KERNEL_SOURCE_DIR}"
VERSION="`grep "^VERSION =" ${LINUXDIR}/Makefile | tr -d '=' | awk '{print $2}'`"
PATCHLEVEL="`grep "^PATCHLEVEL =" ${LINUXDIR}/Makefile | tr -d '=' | awk '{print $2}'`"
SUBLEVEL="`grep "^SUBLEVEL =" ${LINUXDIR}/Makefile | tr -d '=' | awk '{print $2}'`"
LINUX_VERSION=`expr ${VERSION} \* 65536 + 0${PATCHLEVEL} \* 256 + 0${SUBLEVEL}`
LINUX_3_6_X=`expr 3 \* 65536 + 6 \* 256`

## define: SYNO_MBR_GPT_ONLY
# Add MBR style set partition flag function pointer to libparted system
# Add read MBR style partition flag function pointer to libparted system
#
# We know this is not good for underground tools.
# Due to there are too many hardcoding code in SDK. We can only choose to do this in underground.

## define : SYNO_REMOVE_PATCH_1223B
# ( http://git.debian.org/?p=parted/parted.git;a=commitdiff;h=1223b9fc07859cb619c80dc057bd05458f9b5669#patch1 )
# don't remove BLKRRPART ioctl, some modle still need it ex. 5281 platform

## define : SYNO_PART_CHECK
#  check if have this partition, to prevent get wrong value
#
FEATURE_DEFINE="-DSYNO_MBR_GPT_ONLY -DSYNO_UCLIBC -DSYNO_MBR_STYLE_PARTITION_FLAGS -DSYNO_REMOVE_PATCH_1223B -DSYNO_ARGUMENT_ESCAPE -DSYNO_PART_CHECK -DSYNO_USB3_PREVENT_LINKFAIL -DSYNO_AUTOFIX -DSYNO_STRICT_FAT_CHECK -DSYNO_INIT_SEQUENCE_REORDER -DSYNO_FAST_DELETE_PARTITION -DSYNO_IGNORE_FS_TYPE -DSYNO_MBR_SUPPORT_4K -DSYNO_KERNEL_VERSION_CHECK"

##
# Kernel have to support BLKPG_RESIZE_PARTITION ioctl in block layer if we want to enable -DBLKPG_RESIZE_PARTITION.
# The kernel version should be equal or newer than linux-3.6.x.
#
if [ ${LINUX_VERSION} -ge ${LINUX_3_6_X} ]; then
		FEATURE_DEFINE="${FEATURE_DEFINE} -DBLKPG_RESIZE_PARTITION"
fi

if checkPlatformFunction SYNO_FEA_VDSM; then
	FEATURE_DEFINE="${FEATURE_DEFINE} -DSYNO_FEA_VDSM"
fi

##
# Switch to GLIBC Makefile.in
#
cp libparted/labels/Makefile.in.glibc libparted/labels/Makefile.in
cp libparted/labels/Makefile.am.glibc libparted/labels/Makefile.am

export TOOLCHAIN_LIB="${SysRootLib}"
./configure ${ConfigOpt} CC="${CC}" LD="${LD}" AR=${AR} STRIP=${STRIP} RANLIB=${RANLIB} NM=${NM} \
	CFLAGS="${CFLAGS} ${FEATURE_DEFINE} -DSYNO_SUPPORT_NON_ASCII_NAME" \
	ac_cv_skip_blkid=yes \
	--prefix=/sbin --without-readline \
	--disable-mtrace --disable-debug --disable-shared --disable-assert \
	--disable-device-mapper --disable-selinux --enable-pc98=no \
	--disable-dynamic-loading

make ${MAKE_FLAGS} V=1

echo "remove SYNO Makefile"
rm libparted/labels/Makefile.in
rm libparted/labels/Makefile.am
