#!/bin/sh
# ubuntukylin-hook
# This hook used for generated sources.list and do customized work, such as bypass ttf-hanyi
#
set -x

#. /usr/share/debconf/confmodule

# It will passed below 4 parameters from parent.
CLIENT_TYPE=$1
LANGUAGE=$2
ENCODING=$3
TIMEZONE=$4

KYLIN_LIST_FILE="/etc/apt/sources.list.d/ubuntukylin.list"
PACKAGES_LIST="/usr/lib/ubiquity/plugins/region_packages.list"

# Remove the source list file if it's exist
if [ -f $KYLIN_LIST_FILE ]; then
	rm -rf $KYLIN_LIST_FILE
fi

# This hook just used for Simplified Chinese, to add ubuntukylin's source list or not.
if [ "$LANGUAGE" != "zh_CN" ]; then
	exit 0
fi

if [ "$CLIENT_TYPE" = "kylin" ]
then
	# Now the kylin's source list is also added by sogoupinyin package.
	# Another method is that we could remove the sogoupinyin's repo
	mkdir -p /etc/apt/sources.list.d
	echo "deb http://archive.ubuntukylin.com:10006/ubuntukylin trusty main" >> $KYLIN_LIST_FILE
	# The ubuntukylin's list is added by sogoupinyin and kuaipan depend on the md5sum.
	# So remove below line.
	#echo "deb-src http://archive.ubuntukylin.com:10006/ubuntukylin trusty main" >> $KYLIN_LIST_FILE

	# bypass the ttf-hanyi, for internal use for HP project.
	# We can't use db_get in script with confmodule, cause of lock in root permission .
	# It should be defect to debconf, we can use oem user to visit debconf with readonly permission.
	BYPASS=`sudo -u oem debconf-get locale-support-plugin/bypass-ttf-hanyi 2>&1`
	RESULT=$(echo "$BYPASS" | grep "true")
	if [ "$RESULT" != "" ]; then
	#if db_get locale-support-plugin/bypass-ttf-hanyi && \
	#	[ "$RET" = true ]; then
		[ -s $PACKAGES_LIST ] && sed -i "s/ttf-hanyi/ttf-hanyi-/g" $PACKAGES_LIST
	fi
fi
