# -*-mic2-options-*- -f loop --pack-to=@NAME@.tar.gz -*-mic2-options-*-

# 
# Do not Edit! Generated by:
# kickstarter.py
# 

lang en_US.UTF-8
keyboard us
timezone --utc Asia/Seoul
part / --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-rootfs
part /opt/ --size=2000 --ondisk=sda --fstype=ext4 --label=emulator-sysdata

rootpw tizen 
xconfig --startxonboot
bootloader  --timeout=3  --append="rw vga=current splash rootwait rootfstype=ext4 plymouth.enable=0"   --ptable=gpt --menus="install:Wipe and Install:systemd.unit=system-installer.service:test"

desktop --autologinuser=guest  
user --name guest  --groups audio,video --password 'tizen'


repo --name=wearable-emulator-circle --baseurl=http://download.tizen.org/snapshots/tizen/wearable/@BUILD_ID@/repos/emulator-circle/packages/ --ssl_verify=no
repo --name=base-emulator32 --baseurl=http://download.tizen.org/snapshots/tizen/base/latest/repos/emulator32/packages/ --ssl_verify=no

%packages

@Generic Base
@Wearable Base
@Wearable Console Tools
@Wearable Packaging
@Wearable Adaptation
@Wearable Wayland
@Wearable Middleware
@Wearable Applications
@Wearable Bluetooth
@Generic Multimedia
@Wearable Multimedia Emulator
@Wearable Crosswalk
@Wearable Dali
@Wearable EFL
@Wearable Enlightenment
@Wearable Input Framework
@Wearable Connectivity Framework Emulator
@Wearable Security
@Wearable Web Framework
@Wearable Telephony
@Wearable Adaptation Emulator Circle


%end



%post
#!/bin/sh
echo "#################### generic-base.post ####################"

test ! -e /opt/var && mkdir -p /opt/var
test -d /var && cp -arf /var/* /opt/var/
rm -rf /var
ln -snf opt/var /var

test ! -e /opt/usr/home && mkdir -p /opt/usr/home
test -d /home && cp -arf /home/* /opt/usr/home/
rm -rf /home
ln -snf opt/usr/home /home

build_ts=$(date -u +%s)
build_date=$(date -u --date @$build_ts +%Y%m%d_%H%M%S)
build_time=$(date -u --date @$build_ts +%H:%M:%S)

sed -ri \
	-e 's|@BUILD_ID[@]|@BUILD_ID@|g' \
	-e "s|@BUILD_DATE[@]|$build_date|g" \
	-e "s|@BUILD_TIME[@]|$build_time|g" \
	-e "s|@BUILD_TS[@]|$build_ts|g" \
	/etc/tizen-build.conf

# setup systemd default target for user session
cat <<'EOF' >>/usr/lib/systemd/user/default.target
[Unit]
Description=User session default target
EOF
mkdir -p /usr/lib/systemd/user/default.target.wants

# sdx: fix smack labels on /var/log
chsmack -a '*' /var/log

# create appfw dirs inside homes
function generic_base_user_exists() {
	user=$1
	getent passwd | grep -q ^${user}:
}

function generic_base_user_home() {
	user=$1
	getent passwd | grep ^${user}: | cut -f6 -d':'
}

function generic_base_fix_user_homedir() {
	user=$1
	generic_base_user_exists $user || return 1

	homedir=$(generic_base_user_home $user)
	mkdir -p $homedir/apps_rw
	for appdir in desktop manifest dbspace; do
		mkdir -p $homedir/.applications/$appdir
	done
	find $homedir -type d -exec chsmack -a User {} \;
	chown -R $user:users $homedir
	return 0
}

# fix TC-320 for SDK
. /etc/tizen-build.conf
[ "${TZ_BUILD_WITH_EMULATOR}" == "1" ] && generic_base_fix_user_homedir developer

# Add info.ini for system-info CAPI (TC-2047)
/etc/make_info_file.sh

#!/bin/sh
echo "############### wearable-base.post ################"

######### multiuser mode: create additional users and fix their homedirs

if ! generic_base_user_exists owner; then
        gum-utils --offline --add-user --username=owner --usertype=admin --usecret=tizen
fi

#for user in owner alice bob carol guest; do
#	if ! generic_base_user_exists $user; then
#		gum-utils --offline --add-user --username="$user" --usertype=normal --usecret=tizen
#	fi
#done


#!/bin/sh
echo "#################### generic-console-tools.post ####################"

# customize bash prompt
cat >/etc/profile.d/bash_prompt_custom.sh <<'EOF'
if [ "$PS1" ]; then

	function proml {
		# set a fancy prompt (overwrite the one in /etc/profile)
		local default="\[\e[0m\]"
		local usercol='\[\e[1;34m\]' # blue
		local hostcol='\[\e[1;32m\]' # green
		local pathcol='\[\e[1;33m\]' # yellow
		local gitcol='\[\e[1;31m\]' # light red
		local termcmd=''
		local _p="$";

		if [ "`id -u`" -eq 0 ]; then
			usercol='\[\e[1;31m\]'
			_p="#"
		fi

		PS1="${usercol}\u${default}@${hostcol}\h${default}:${pathcol}\w${default}${gitcol}${default}${_p} ${termcmd}"
	}

	proml

	function rcd () {
      [ "${1:0:1}" == "/" ] && { cd $1; } || { cd $(pwd -P)/$1; }
   }

	alias ll="ls -lZ"
	alias lr="ls -ltrZ"
	alias la="ls -alZ"

	function get_manifest () {
		rpm -qa --queryformat="%{name} %{Version} %{Release} %{VCS}\n" | sort
	}
fi
EOF


#!/bin/sh
echo "############### wearable-packaging.post ################"

# generate repo files for zypper
function genrepo() {
	local url=$1
	local reponame=$2
	local filename=${3:-$2}
	local enabled=${4:-0}

	local prefix=${TZ_BUILD_VENDOR}-${TZ_BUILD_PROFILE}-${TZ_BUILD_REPO}

	# remove double slashes if any
	url=$(sed -e  's|/\+|/|g' -e 's|:/|://|' <<<$url)

	cat >> /etc/zypp/repos.d/$prefix-${filename}.repo << EOF
[$prefix-${reponame}]
name=$prefix-${reponame}
enabled=$enabled
autorefresh=0
baseurl=${url}?ssl_verify=no
type=rpm-md
gpgcheck=0
 
EOF
}

# source /etc/tizen-build.conf to get more infos about project, repos etc.
. /etc/tizen-build.conf 

# adjust build_id if this scripts executes before the replacement in /etc/tizen-build.conf
TZ_BUILD_ID=$(echo $TZ_BUILD_ID | sed 's|@BUILD_ID[@]|@BUILD_ID@|')

# snapshot repo
genrepo ${TZ_BUILD_SNAPSHOT_URL}/${TZ_BUILD_ID}/repos/${TZ_BUILD_REPO}/packages snapshot snapshot 1
genrepo ${TZ_BUILD_SNAPSHOT_URL}/${TZ_BUILD_ID}/repos/${TZ_BUILD_REPO}/debug snapshot-debug snapshot 1

# latest repo
genrepo ${TZ_BUILD_SNAPSHOT_URL}/latest/repos/${TZ_BUILD_REPO}/packages update update 0
genrepo ${TZ_BUILD_SNAPSHOT_URL}/latest/repos/${TZ_BUILD_REPO}/debug update-debug update 0

# daily repo
genrepo ${TZ_BUILD_DAILY_URL}/latest/repos/${TZ_BUILD_REPO}/packages daily daily 0
genrepo ${TZ_BUILD_DAILY_URL}/latest/repos/${TZ_BUILD_REPO}/debug daily-debug daily 0

# weekly repo
genrepo ${TZ_BUILD_WEEKLY_URL}/latest/repos/${TZ_BUILD_REPO}/packages weekly weekly 0
genrepo ${TZ_BUILD_WEEKLY_URL}/latest/repos/${TZ_BUILD_REPO}/debug weekly-debug weekly 0


#!/bin/sh
echo "############### wearable-adaptation.post ################"


#!/bin/sh
echo "############### wearable-middleware.post ################"


#!/bin/sh
echo "#################### generic-bluetooth.post ####################"


#!/bin/sh
echo "#################### generic-multimedia.post ####################"


#!/bin/sh
echo "#################### generic-desktop-applications.post ####################"

# depends on generic-base functions
function generic_desktop_applications_fix_userhome() {
	user=$1

	generic_base_user_exists $user || return 1
	homedir=$(generic_base_user_home $user)

	echo "Fix app_info.db of $user"
	chown -R $user:users $homedir/.applications/dbspace/
}

# fix TC-320 for SDK
. /etc/tizen-build.conf
[ "${TZ_BUILD_WITH_EMULATOR}" == "1" ] && generic_desktop_applications_fix_userhome developer


#!/bin/sh
echo "############### wearable-desktop-applications.post ################"

# call function defined in meta-generic
for user in alice bob carol guest developer; do
	generic_desktop_applications_fix_userhome $user
done



#!/bin/sh
echo "#################### generic-crosswalk.post ####################"


#!/bin/sh
echo "############### wearable-crosswalk.post ################"


#!/bin/sh
echo "############### wearable-license.post ################"

LICENSE_DIR=/usr/share/license
LICENSE_FILE=/usr/share/license.html
MD5_TEMP_FILE=/usr/share/temp_license_md5

if [[ -f $LICENSE_FILE ]]; then
        rm -f $LICENSE_FILE
fi

LICENSE_LIST=`ls $LICENSE_DIR`

if [[ -f $MD5_TEMP_FILE ]]; then
        rm -f $MD5_TEMP_FILE
fi

cd $LICENSE_DIR

for INPUT in $LICENSE_LIST; do
        if [[ -f $INPUT ]]; then
                md5sum $INPUT >> $MD5_TEMP_FILE
        fi
done

MD5_LIST=`cat $MD5_TEMP_FILE|awk '{print $1}'|sort -u`

echo "<html>" >> $LICENSE_FILE
echo "<head>" >> $LICENSE_FILE
echo "<meta name=\"viewport\" content=\"initial-scale=1.0\">" >> $LICENSE_FILE
echo "</head>" >> $LICENSE_FILE
echo "<body>" >> $LICENSE_FILE
echo "<xmp>" >> $LICENSE_FILE

for INPUT in $MD5_LIST; do
        PKG_LIST=`cat $MD5_TEMP_FILE|grep $INPUT|awk '{print $2}'`
        PKG_NAME=`echo $PKG_LIST|awk '{print $1}'`

        echo "$PKG_LIST :" >> $LICENSE_FILE
        cat $PKG_NAME >> $LICENSE_FILE
        echo  >> $LICENSE_FILE
        echo  >> $LICENSE_FILE
        echo  >> $LICENSE_FILE
done

echo "</xmp>" >> $LICENSE_FILE
echo "</body>" >> $LICENSE_FILE
echo "</html>" >> $LICENSE_FILE

rm -rf $LICENSE_DIR/* $MD5_TEMP_FILE

#!/bin/sh
echo "#################### generic-security.post ####################"

if [ -e /usr/share/security-config/set_capability ]; then
	echo 'Give capabilities to daemons via set_capability from security-config package'
	/usr/share/security-config/set_capability
fi


%end

%post --nochroot
# buildname.nochroot 
if [ -n "$IMG_NAME" ]; then
    echo "BUILD_ID=$IMG_NAME" >> $INSTALL_ROOT/etc/tizen-release
    echo "BUILD_ID=$IMG_NAME" >> $INSTALL_ROOT/etc/os-release
fi


%end
