#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2006-2012 Daniel Baumann <daniel@debian.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e
set +x

# Including common functions
( . "${LIVE_BUILD}/scripts/build.sh" > /dev/null 2>&1 || true ) || . /usr/lib/live/build.sh

# Setting static variables
DESCRIPTION="$(Echo 'build rootfs image')"
HELP=""
USAGE="${PROGRAM} [--force]"

Arguments "${@}"

# Reading configuration files
Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source config/ostree
Set_defaults

Echo_message "Begin building root filesystem image..."

# Requiring stage file
Require_stagefile .build/config .build/bootstrap .build/binary_chroot

# Checking stage file
Check_stagefile .build/ostree_commit

# Checking lock file
Check_lockfile .lock

# Creating lock file
Create_lockfile .lock

if [ ! -f chroot/chroot/etc/ostree-release ]; then 
	Echo_message "ostree-realese文件不存在，非ostree不可变系统"
	exit 0
fi

ostree_file_path="/usr/bin/ostree" # 将路径更改为要检查的文件路径
if [ -f "$ostree_file_path" ] && [ -x "$ostree_file_path" ]; then
    Echo_message "ostree 命令存在"
else
    Echo_message "ostree 命令不存在，退出执行"
    exit 1
fi

export info_version_string=
export mate_data_add_string=
export os_version_name=
export sign_position=

# 根据ostree 要求，将根分区下的etc目录拷贝至usr目录下
rm chroot/chroot/dev/console chroot/chroot/dev/full chroot/chroot/dev/null chroot/chroot/dev/ptmx chroot/chroot/dev/random chroot/chroot/dev/tty chroot/chroot/dev/urandom chroot/chroot/dev/zero
cp -a chroot/chroot/etc chroot/chroot/usr/

while read os_release_file_line
do
	if [ "$os_release_file_line" = "[info]" ]; then
		sign_position=0
		continue
	fi

	if [ "$os_release_file_line" = "[metadata]" ]; then
		sign_position=1
		continue
	fi

	if [ "$sign_position" = "0" ] && [ "$os_release_file_line" != "[info]" ]; then
		info_version_string=$os_release_file_line
		continue
	fi

	if [ "$os_release_file_line" != "[metadata]" ]; then
		mate_data_add_string="$mate_data_add_string --add-metadata-string='$os_release_file_line'"
	fi

done < "chroot/chroot/etc/ostree-release"

info_version_string=${info_version_string#*version=}
os_version_name=$(echo "$info_version_string" | awk -F ":" '{print $1}')
ostree_branch_name=$(echo "$info_version_string" | awk -F ":" '{print $2}')
Echo_message "os_version_name: $os_version_name"
Echo_message "info_version_string: $info_version_string"
Echo_message "mate_data_add_string: $mate_data_add_string"
Echo_message "ostree_branch_name: $ostree_branch_name"


# 删除etc目录
rm -r chroot/chroot/etc

# 复制内核文件
#kernel_vesion=`chroot ./chroot/chroot uname -r`
kernel_name=`readlink -f ./chroot/chroot/boot/vmlinuz`
kernel_version=$(echo $kernel_name | awk '{split($0, a, "vmlinuz-"); print a[2]}')
Echo_message "kernel_version: $kernel_version"
initrd_real_name=`readlink -f ./chroot/chroot/boot/initrd.img`
cp -a "chroot/chroot/boot/vmlinuz-$kernel_version" "chroot/chroot/usr/lib/modules/$kernel_version/vmlinuz"
cp -a "$initrd_real_name" "chroot/chroot/usr/lib/modules/$kernel_version/initramfs.img"

#echo "---------->>>>>>>>>000000 1"

# 修改ostree 对于opt、mnt、media、tmp目录的存储路径，全部放置到var目录下
mv chroot/chroot/opt chroot/chroot/var/
ln -s var/opt chroot/chroot/opt

mv chroot/chroot/mnt chroot/chroot/var/
ln -s var/mnt chroot/chroot/mnt

mv chroot/chroot/media chroot/chroot/run/
ln -s run/media chroot/chroot/media

mv chroot/chroot/tmp chroot/chroot/var/
ln -s var/tmp chroot/chroot/tmp

mv chroot/chroot/srv chroot/chroot/var/
ln -s var/srv chroot/chroot/srv

[ -d sysroot ] || mkdir sysroot

#echo "---------->>>>>>>>>000000 4"
ostree admin init-fs sysroot

#echo "---------->>>>>>>>>000000 5"
ln -s /sysroot/ostree chroot/chroot/ostree

#echo "---------->>>>>>>>>000000 6"
ostree admin os-init "$os_version_name" --sysroot=sysroot

#echo "---------->>>>>>>>>000000 7"

#重新初始化仓库，使用--mode=archive-z2模式生成ostree repo仓库
mkdir usr_etc_file

for etcfile in fstab passwd shadow sudoers group locale.gen adjtime localtime hostname hosts timezone subuid subgid
do
	chroot_etc_file="chroot/chroot/usr/etc/${etcfile}"
	if [ -e "$chroot_etc_file" ] || [ -L "$chroot_etc_file" ]; then
		mv "$chroot_etc_file" ./usr_etc_file/
	fi
done

[ -d chroot/chroot/data ] || mkdir chroot/chroot/data
[ -d chroot/chroot/backup ] || mkdir chroot/chroot/backup
[ -d chroot/chroot/sysroot ] || mkdir chroot/chroot/sysroot

ostree init --repo=./repo --mode=archive-z2

ostree commit --repo=./repo -b "$ostree_branch_name" -s "$info_version_string" $mate_data_add_string chroot/chroot/

ostree summary --repo=./repo -u

#echo "---------->>>>>>>>>000000 8"

ostree-push --repo=./repo $LB_OSTREE_USER_NAME@$LB_OSTREE_IP:$LB_OSTREE_WARE_ADDRESS/$os_version_name $ostree_branch_name

mv ./usr_etc_file/* chroot/chroot/usr/etc/
rm -r usr_etc_file
rm -r chroot/chroot/data chroot/chroot/backup chroot/chroot/sysroot

#提交完成后，将etc目录拷贝出来，外面则是一个完成的根文件系统
cp -a chroot/chroot/usr/etc chroot/chroot

cp -a sysroot chroot/chroot/sysroot
mknod chroot/chroot/dev/null c 1 3
chmod 666 chroot/chroot/dev/null

unset info_version_string
unset mate_data_add_string
unset os_version_name
unset sign_position

Create_stagefile .build/ostree_commit
