#!/bin/bash

set -ex

do_user_file() {
	old_file=$1
	new_file=$2
	
	user_name=
	user_names=
	user_names=`cat "/tmp/etc/passwd" | grep home | grep "/bin/bash"`
	for i in ${user_names}; do
		user_name=${i%%:*}
        	echo "${user_name}"
		cat $1 | grep "^${user_name}:" >> $2 || true
	done

}

set_swap() {
  while (true); do
    if ! echo ${array} | grep -q linux-swap; then
        break
    fi
    swaptmp=${array#*linux-swap:}
    swapdev=$(echo ${swaptmp%%|*})
    #mkswap ${swapdev}
    swapon ${swapdev}

    array=$(echo ${swaptmp} | cut -d'\' -f2-)
  done
}


automatic=$(get_value automatic-installation)
if [[ $automatic -eq 0 ]]; then

  partlists=$(get_value partitions | sed 's/\"//g')
  array_space=${partlists//;/ }
  array=${partlists//;/|}

  list=
  for i in ${array_space[@]}; do
    list="$list $(echo $i | cut -d: -f1)"
  done
  sort_list=$(echo $list | tr ' ' '\n' | sort)

  for mountpoint in $sort_list; do
    dev0=$(echo ${array#*$mountpoint:})
    dev=$(echo ${dev0%%|*})

    if [[ $mountpoint == "linux-swap" ]]; then
      continue
    fi

    if [[ $mountpoint == "kylin-data" ]]; then
      mkdir -p /target/data
      mount $dev /target/data
      chmod 1777 /target/data
      do_bind_data
      continue
    fi
    
    if [[ $mountpoint == "empty" ]]; then
      continue
    fi

    mkdir -p /target$mountpoint
    mount $dev /target$mountpoint
    
    if [[ $mountpoint == "/backup" ]]; then
      set_backup $dev
    fi

    if [[ $mountpoint == "/data" ]]; then
      chmod 1777 /target$mountpoint >/dev/null
    fi
  done

  nomount_list=$(echo ${array_space} | tr ' ' '\n' | sort | grep empty) || true
  if [[ -n ${nomount_list} ]]; then
  	for mountpoint in ${nomount_list}; do
      		dev=$(echo ${mountpoint#*:})
     		mkdir -p /target/nomountpoint
      		mount $dev /target/nomountpoint
      		chmod -R 1777 /target/nomountpoint
      		umount -l /target/nomountpoint
      		rmdir /target/nomountpoint
  	done
  fi

  set_swap
fi

deal_squashfs() {
  if [[ "${is_minimal}" == "true" ]]; then
    minimal_squashfs=$(find /cdrom/casper -name '*img' | head -1)
    # 挂载最小系统镜像
    if [[ $minimal_squashfs =~ 'img' ]]; then
      mount -r $minimal_squashfs /rofs
    fi
  elif [[ "${is_ghost}" == "true" ]]; then
    ghost_img=$(get_value ghost-path)
    # 挂载 ghost 镜像
    if [[ ${ghost_img} =~ 'img' ]]; then
      mount -r $ghost_img /rofs || true
    fi
  fi
}

msg "正在拷贝基础文件系统..."
# 判断使用最小系统镜像或 ghost
deal_squashfs
rsync -aHAX /rofs/ /target
msg "文件系统拷贝完成"
sync

# 加密
if [[ "${isluks_lvm}" == "true" ]]; then
  rsync /etc/crypttab /target/etc
  rm -f /target/etc/grub.d/05* || true
  rm -f /target/etc/grub.d/06* || true
fi

# 保留data
data_unformat=$(get_value data-unformat)
data_preserve=$(get_value data-preserve)
if [[ "${data_unformat}" == "true" ]]; then

	if [[ -f /tmp/etc/passwd ]]; then
		do_user_file /tmp/etc/passwd /target/etc/passwd
		
		if [[ -f /tmp/etc/passwd- ]]; then
    			do_user_file /tmp/etc/passwd- /target/etc/passwd-
		fi

		if [[ -f /tmp/etc/shadow ]]; then
			do_user_file /tmp/etc/shadow /target/etc/shadow
		fi
		if [[ -f /tmp/etc/shadow- ]]; then
			do_user_file /tmp/etc/shadow- /target/etc/shadow-
		fi

		if [[ -f /tmp/etc/group ]]; then
			do_user_file /tmp/etc/group /target/etc/group
			if [[ -f /usr/share/kylin-os-installer/user-groups.txt ]]; then
                        	cp -a  /usr/share/kylin-os-installer/user-groups.txt /target/usr/share/kylin-os-installer || true
                	fi
		fi
		if [[ -f /tmp/etc/group- ]]; then
			do_user_file /tmp/etc/group- /target/etc/group-
		fi

		if [[ -f /tmp/etc/gshadow ]]; then
			do_user_file /tmp/etc/shadow /target/etc/shadow
		fi
		if [[ -f /tmp/etc/gshadow- ]]; then
			do_user_file /tmp/etc/shadow- /target/etc/shadow-
		fi
		if [[ -f /tmp/etc/uid_list ]]; then
			do_user_file /tmp/etc/uid_list /target/etc/uid_list
		fi
	fi
        
	if [[ -f /tmp/etc/subgid ]]; then
                cp -a /tmp/etc/subgid* /target/etc
        fi
	if [[ -f /tmp/etc/subuid ]]; then
                cp -a /tmp/etc/subuid* /target/etc
        fi


	if [[ -f /tmp/etc/hostname ]]; then
     		cp -a /tmp/etc/hostname /target/etc
	fi
	if [[ -f /tmp/etc/hosts ]]; then
     		cp -a /tmp/etc/hosts /target/etc
	fi
        if [[ -f /tmp/etc/lightdm/lightdm.conf ]]; then
                cp -a /tmp/etc/lightdm/lightdm.conf /target/etc/lightdm
        fi
	
	# 保留激活文件
	if [[ -f /tmp/etc/.kyhwid ]]; then
                cp -a /tmp/etc/.kyhwid /target/etc/
        fi
        if [[ -f /tmp/etc/.kyactivation ]]; then
                cp -a /tmp/etc/.kyactivation /target/etc/
        fi
fi

if [[ -f /usr/share/kylin-os-installer/ky-installer.cfg ]]; then
        cp -a /usr/share/kylin-os-installer/ky-installer.cfg /target/usr/share/kylin-os-installer
fi

#拷贝可选安装包
if [[ -d /cdrom/apps-third ]]; then
#        cp -a /cdrom/apps-third /target/opt
	rsync -aHA /cdrom/apps-third/ /target/opt/apps-third/

	if [[ -f /opt/apps-third/install.list ]]; then
		cp -a /opt/apps-third/install.list /target/opt/apps-third/install.list
	fi
	
       if cat /proc/cmdline | grep -qi automatic ;then
               if [ "x$is_oem_mode" = "xfalse" ];then
                       if [ -f /target/opt/apps-third/install.list ];then
                               rm -f /target/opt/apps-third/install.list
                       fi
                       /usr/share/kylin-os-installer/scripts/preinstall-scan.py
      		else
	       		if cat /proc/cmdline | grep -qi auti-only ;then
                       	/usr/share/kylin-os-installer/scripts/preinstall-scan.py
			fi
		fi
       fi
fi

#显卡测试拷贝结果
if [ -f "/tmp/kwin-backend.config" ]; then
    chmod 644 /tmp/kwin-backend.config
    cp /tmp/kwin-backend.config /target/etc/xdg/
fi

