#! /bin/sh

# Author: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
#         Tollef Fog Heen <tfheen@canonical.com>
#         Marco Amadori <marco.amadori@gmail.com>
#

PATH=/usr/sbin:/usr/bin:/sbin:/bin
NAME=casper
SCRIPTNAME=/etc/init.d/${NAME}
DO_SNAPSHOT=/sbin/${NAME}-snapshot

# Exit if system was not booted by casper
[ -f /run/.casper-boot ] || exit 0

# Exit if the system was booted from an ISO image rather than a physical CD
grep -qs find_iso= /proc/cmdline && exit 0

# Read configuration variable file if it is present
[ -r /etc/$NAME.conf ] && . /etc/$NAME.conf

# Try to cache everything we're likely to need after ejecting.  This
# is fragile and simple-minded, but our options are limited.
cache_path() {
    path="$1"

    if [ -d "$path" ]; then
        for f in $(find "$path" -type f); do
            cache_path "$f"
        done
    elif [ -f "$path" ] && [ ! -L "$path" ]; then
        if [ -x "$path" ]; then
            if file -L "$path" | grep -q 'dynamically linked'; then
                for lib in $(ldd "$path" | awk '{ print $3 }'); do
                    cache_path "$lib"
                done
            fi
        fi
        echo -n >> "$path"
    fi
}

# 定义需要排除的关键词
EXCLUDE1="casper"
EXCLUDE2="reboot"
EXCLUDE3="journal"
EXCLUDE4="plymouth"
CONSOLE="/dev/ttyS0"

stop_service()
{
# 1. 处理运行中的服务：停止不含 casper 和 reboot 的服务
echo "===== start list service ====="
systemctl list-units --type=service --state=running --no-legend | awk '{print $1}' | while read -r service; do
    service_name=$(echo "$service" | sed 's/\.service//')
    
    # 跳过含 casper 的服务
    if echo "$service_name" | grep "$EXCLUDE1"; then
        echo "skip（$EXCLUDE1）: $service"
        continue
    fi
   
    # 跳过含 reboot 的服务
    if echo "$service_name" | grep "$EXCLUDE2"; then
        echo "skip（$EXCLUDE2）: $service"
        continue
    fi

    # 跳过含 journal 的服务
    if echo "$service_name" | grep "$EXCLUDE3"; then
        echo "skip（$EXCLUDE3）: $service"
        continue
    fi

    # 跳过含 plymouth 的服务
    if echo "$service_name" | grep "$EXCLUDE4"; then
        echo "skip（$EXCLUDE4）: $service"
        continue
    fi

    systemctl mask "$service"
    if [ $? -eq 0 ]; then
        echo "$service_name masked"
    else
        echo "$service_name masked fail" 
    fi

    # 停止符合条件的服务
    echo "stopping: $service"
    systemctl stop "$service"
    if [ $? -eq 0 ]; then
        echo "$service stoped"
    else
        echo "$service stop fail"
    fi

done

# 2. 屏蔽 /lib/systemd/system/reboot.target.wants 目录下不含 reboot 的服务
echo -e "\n===== start reboot.target.wants dir ====="
REBOOT_WANTS_DIR="/lib/systemd/system/reboot.target.wants"

# 检查目录是否存在
if [ -d "$REBOOT_WANTS_DIR" ]; then
    # 遍历目录下的所有服务（排除符号链接指向的目标含 reboot 的服务）
    find "$REBOOT_WANTS_DIR" -type l | while read -r service_link; do
        # 获取服务链接指向的实际服务名（去除路径和 .service 后缀）
        target_service=$(readlink "$service_link" | xargs basename | sed 's/\.service//')
        
        # 跳过含 reboot 的服务
        if echo "$target_service" | grep -q "$EXCLUDE2"; then
            echo "skip（$EXCLUDE2）: $service_link"
            continue
        fi

        # 跳过含 plymouth 的服务
        if echo "$target_service" | grep -q "$EXCLUDE3"; then
            echo "skip（$EXCLUDE3）: $service_link"
            continue
        fi
        
        # 屏蔽该服务（创建 /etc 下的屏蔽链接）
        service_name=$(basename "$service_link" .service)
        echo "masking: $service_name"
        systemctl mask "$service_name.service"
        if [ $? -eq 0 ]; then
            echo "$service_name masked"
        else
            echo "$service_name masked fail"
        fi
    done
else
    echo "no dro $REBOOT_WANTS_DIR "
fi

echo -e "\nstop and mask over"

sleep 20
}

do_stop ()
{
    logger -t ${NAME} "resyncing snapshots and caching reboot files..."

    if [ ! -z "${ROOTSNAP}" ]; then
        $DO_SNAPSHOT --resync-string="${ROOTSNAP}"
    fi

    if [ ! -z "${HOMESNAP}" ]; then
        $DO_SNAPSHOT --resync-string="${HOMESNAP}"
    fi

    # check for netboot
    if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline ; then
        return 0
    fi

    # Don't prompt to eject the SD card on Babbage board, where we reuse it
    # as a quasi-boot-floppy. Technically this uses a bit of ubiquity
    # (archdetect), but since this is mostly only relevant for
    # installations, who cares ...
    if type archdetect >/dev/null 2>&1; then
        subarch="$(archdetect)"
        case $subarch in
            arm*/imx51)
                return 0
                ;;
        esac
    fi

    prompt=1
    if grep -qs noprompt /proc/cmdline || [ -e /run/casper-no-prompt ]; then
        prompt=
    fi

    for path in $(which halt) $(which reboot) /etc /lib/udev /bin/chvt /etc/rc?.d /etc/default $(which stty) /bin/plymouth /lib/plymouth /lib/*/plymouth /lib/systemd /etc/systemd /lib/*/libnss_files* /etc/nsswitch.conf /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf /usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf /usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf /etc/fonts/fonts.conf /etc/fonts/conf.d/60-latin.conf; do
        cache_path "$path"
    done

    device="$(grep " /cdrom " /proc/mounts | cut -d' ' -f1)" || device=
    # If /cdrom isn't mounted, don't try to eject it
    if [ -z "$device" ]; then
        return 0
    fi

    # If the device exists but can't be found in /sys/block, it's most likely a partition
    # so unmount it (lazy mode) and sync
    if [ -b "$device" ] && [ ! -f "/sys/block/$(basename $device)/removable" ]; then
        umount -l $device >/dev/null 2>&1
        sync
        # from now on operate on the partition's block device
        device=/dev/$(basename "$(readlink -f /sys/class/block/$(basename $device)/..)")
    fi

    # If we're still there, then we're probably a cdrom or other media
    # ship the eject if the kernel says the media isn't removable
    if [ "$(cat /sys/block/$(basename $device)/removable)" = "0" ]; then
	disk_remove=$(readlink /sys/block/$(basename $device) | grep dm)|| true
        if [ -z "${disk_remove}" ]; then
                return 0
        fi
    fi

    systemd-detect-virt
    if [ $? -eq 0 ]; then
        return 0
    fi

    # XXX - i18n
    #MSG="Please remove the installation medium, then press ENTER: "
    #MSG_FALLBACK="Please remove the installation medium, then reboot."
    ### add by kylin
    cat /etc/default/locale | grep "^LANG=" | grep -q "en_US"
    if [ $? -eq 0 ];then
        WAIT_MSG="Please wait, cleaning up temporary environment"
    else
        WAIT_MSG="请稍候，正在清理临时环境"
    fi

    if [ -f /tmp/ky-installer.cfg ]; then
        cat /tmp/ky-installer.cfg | grep "^language=" | grep -q "en_US"
        if [ $? -eq 0 ];then
        	WAIT_MSG="Please wait, cleaning up temporary environment"
        else
        	WAIT_MSG="请稍候，正在清理临时环境"
        fi
        cat /tmp/ky-installer.cfg | grep "^language=" | grep -q "bo_CN"
        if [ $? -eq 0 ];then
        	WAIT_MSG="སྒུག་དང་། གནས་སྐབས་ཀྱི་ཁོར་ཡུག་གཙང་སྦྲ་བྱེད་རོགས།"
        fi
    else
        if [ -f /var/log/installer/ky-installer.cfg ]; then
                cat /var/log/installer/ky-installer.cfg | grep "^language=" | grep -q "en_US"
                if [ $? -eq 0 ];then
        		WAIT_MSG="Please wait, cleaning up temporary environment"
                else
        		WAIT_MSG="请稍候，正在清理临时环境"
                fi
                cat /var/log/installer/ky-installer.cfg | grep "^language=" | grep -q "bo_CN"
        	if [ $? -eq 0 ];then
        		WAIT_MSG="སྒུག་དང་། གནས་སྐབས་ཀྱི་ཁོར་ཡུག་གཙང་སྦྲ་བྱེད་རོགས།"
        	fi
        fi
    fi

    if [ "$prompt" ]; then
        sync
        if [ -x /bin/plymouth ] && plymouth --ping; then
            # 不切换终端，解决直接安装引入的无法重启问题
            #chvt 63
            plymouth message --text="$WAIT_MSG"
        fi
    fi

    stop_service

    cat /etc/default/locale | grep "^LANG=" | grep -q "en_US"
    if [ $? -eq 0 ];then
        MSG="Please remove the installation medium, then press ENTER"
        MSG_FALLBACK="Please remove the installation medium, then reboot"
    else
        MSG="请取出安装介质，然后按 ENTER 键"
        MSG_FALLBACK="请取出安装介质，然后重启"
    fi
    cat /etc/default/locale | grep "^LANG=" | grep -q "bo_CN"
    if [ $? -eq 0 ];then
        MSG="ནང་འཇུག་བར་རྫས་ལེན་རོགས།་དེ་ནས་ENTERགནོན་རོགས།"
        MSG_FALLBACK="ཁྱེད་ཀྱིས་སྨྱན་སྦྱོར་ངོ་བོ་ལེན་རོགས། དེ་ནས་ཡང་བསྐྱར་སྒོ་ཕྱེ་རོགས།"
    fi
    
    if [ -f /tmp/ky-installer.cfg ]; then
        cat /tmp/ky-installer.cfg | grep "^language=" | grep -q "en_US"
        if [ $? -eq 0 ];then
                MSG="Please remove the installation medium, then press ENTER"
                MSG_FALLBACK="Please remove the installation medium, then reboot"
        else
                MSG="请取出安装介质，然后按 ENTER 键"
                MSG_FALLBACK="请取出安装介质，然后重启"
        fi
        cat /tmp/ky-installer.cfg | grep "^language=" | grep -q "bo_CN"
        if [ $? -eq 0 ];then
                MSG="ནང་འཇུག་བར་རྫས་ལེན་རོགས།་དེ་ནས་ENTERགནོན་རོགས།"
                MSG_FALLBACK="ཁྱེད་ཀྱིས་སྨྱན་སྦྱོར་ངོ་བོ་ལེན་རོགས། དེ་ནས་ཡང་བསྐྱར་སྒོ་ཕྱེ་རོགས།"
        fi
    else
        if [ -f /var/log/installer/ky-installer.cfg ]; then
                cat /var/log/installer/ky-installer.cfg | grep "^language=" | grep -q "en_US"
                if [ $? -eq 0 ];then
                        MSG="Please remove the installation medium, then press ENTER"
                        MSG_FALLBACK="Please remove the installation medium, then reboot"
                else
                        MSG="请取出安装介质，然后按 ENTER 键"
                        MSG_FALLBACK="请取出安装介质，然后重启"
                fi
                cat /var/log/installer/ky-installer.cfg | grep "^language=" | grep -q "bo_CN"
        	if [ $? -eq 0 ];then
               		MSG="ནང་འཇུག་བར་རྫས་ལེན་རོགས།་དེ་ནས་ENTERགནོན་རོགས།"
                	MSG_FALLBACK="ཁྱེད་ཀྱིས་སྨྱན་སྦྱོར་ངོ་བོ་ལེན་རོགས། དེ་ནས་ཡང་བསྐྱར་སྒོ་ཕྱེ་རོགས།"
        	fi
        fi
    fi

    if [ "$prompt" ]; then
    	sync
        if [ -x /bin/plymouth ] && plymouth --ping; then
            # 不切换终端，解决直接安装引入的无法重启问题
            #chvt 63
            plymouth message --text="$MSG"
            clear > /dev/tty1
            echo $MSG_FALLBACK > /dev/tty1
        else
            stty sane < /dev/console
            echo $MSG > /dev/console
        fi
    fi


    [ "$prompt" ] || return 0

    # PXE 安装时可配置直接重启
    if grep -q automatic /proc/cmdline; then
  	  if [ -f /cdrom/ky-installer.cfg ]; then
       		 if grep -q reboot=1 /cdrom/ky-installer.cfg; then
	        	return 0
		 fi
    	  fi
    fi

    if [ -x /bin/plymouth ] && plymouth --ping; then
        plymouth watch-keystroke > /dev/null
        plymouth message --text=""
    else
        read x < /dev/console
    fi
    
    eject -p -m $device >/dev/null 2>&1

    # 强制关机或重启
    sync
    if systemctl list-units | grep plymouth-poweroff >/dev/null; then
        poweroff --force
    elif systemctl list-units | grep plymouth-reboot >/dev/null; then
        reboot --force
    fi
}

do_stop

