#!/bin/bash
#
# Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Read kernel parameter and set global variables.

#/usr/bin/kwin_no_scale &
#sleep 5
init() {
    sleep 5                        # 延时启动背景窗口，保证窗口不会因为kwin导致的闪屏
    /usr/bin/deepin-installer-init # 设置安装器的背景，适配高分辨率下plymouth出现歪屏的情况
}

# 获取活动屏幕输出个数
monitors_cnt() {
    local out=`xrandr --listactivemonitors | grep ^Monitors:`
    out=${out##Monitors:}
    echo ${out:-0}
}

# 获取主屏幕
monitors_primary() {
    ## 通过前后空格精准匹配
    local out=`xrandr | grep ' connected primary ' | awk '{print $1}'`
    echo -n $out
}

# 获取其他屏幕数据列表
monitors_othe_list() {
    local out1=`xrandr | grep ' connected ' | grep -v 'connected primary' | awk '{print $1}' | grep -v "DisplayPort-"`
    local out2=`xrandr | grep ' connected ' | awk '{print $1}' | grep "DisplayPort-" | awk 'END {print}' | grep -v $(monitors_primary)`
    [ -n "$out1" ] || out2=`xrandr | grep ' connected ' | awk '{print $1}' | grep "DisplayPort-" | grep -v $(monitors_primary)`
    echo -n $out1 $out2
}


# 获取屏幕数据列表
monitors_list() {
    local out=`xrandr | grep ' connected ' | awk '{print $1}'`
    echo -n $out
}

# 处理1k情况，安装器最高以1k显示
monitors_mode_1k() {
    local mode="1920x1080"
    local cnt=`xrandr -q | awk '{print $1}' | grep -E [0-9]+x[0-9]+ | grep -E "^$mode$" | wc -l`
    if [ $cnt -eq $(monitors_cnt) ]; then
        echo $mode
    else
        echo ""
    fi
}

# 获取所有屏幕共同支持的显示模式
monitors_mode() {
   local mode_1k=$(monitors_mode_1k)
   ## 如果支持1k，最以1k屏显示
   if [ -n "$mode_1k" ]; then
       echo $mode_1k
   else
       local list=`xrandr -q | awk '{print $1}' | grep -E [0-9]+x[0-9]+`
       for line in $list; do
           local cnt=`xrandr -q | awk '{print $1}' | grep -E [0-9]+x[0-9]+ | grep -E "^$line$" | wc -l`
           if [ $cnt -eq $(monitors_cnt) ]; then
               echo $line
               return;
           fi
       done

       # 最低的默认分辨率，防止如果所有分辨率都不满足的情况下使用800x600
       echo "800x600"
   fi
}

print_log() {
    xrandr
    monitors_list | xargs -d" " -I {} echo "xrandr --output {} --auto --primary"
    monitors_primary | xargs -d" " -I {} echo "xrandr --output {} --mode $(monitors_mode)"
    monitors_othe_list | xargs -d" " -I {} echo "xrandr --output $(monitors_primary) --same-as {} --mode $(monitors_mode)"
    monitors_othe_list | xargs -d" " -I {} echo "xrandr --output {} --mode $(monitors_mode)"
}

# 处理设置屏幕
setup_monitors() {
    # 设置主屏幕，兼容都是dpi输出且输出屏幕位置重合的情况,兼容无主屏输出的情况
    monitors_list | xargs -d" " -I {} xrandr --output {} --auto --primary || xrandr

    local cnt=$(monitors_cnt)
    if [ $cnt -gt 1 ]; then
        # 设置主屏分辨率
        monitors_primary | xargs -d" " -I {} xrandr --output {} --mode $(monitors_mode)
        # 克隆模式
        monitors_othe_list | xargs -d" " -I {} xrandr --output $(monitors_primary) --same-as {} --mode $(monitors_mode)
        # 设置多屏共同支持的最大分辨率
        monitors_othe_list | xargs -d" " -I {} xrandr --output {} --mode $(monitors_mode)

        ## 输出调试日志
        print_log
    else
        echo "Single output"
    fi
}

# 将安装器后配置的运行目录改为/usr/share/deepin-installer-exec
first_boot_move() {

    installerRunDir=/usr/share/deepin-installer-exec/
    mkdir -p ${installerRunDir}

    firstBoot="/usr/bin/deepin-installer-first-boot"
    hookPath="/usr/share/deepin-installer/hooks"

    if [ -f ${firstBoot} ]; then
        cp -vrf /usr/bin/deepin-installer-first-boot ${installerRunDir}
    fi

    if [ -d ${hookPath} ]; then
        cp -vrf /usr/share/deepin-installer/hooks/ ${installerRunDir}
    fi
}

## 处理后配置卸载自己后有小概率启动不了桌面问题，安装器后配置运行路径变为/usr/share/deepin-installer-exec
first_boot_move >> /var/log/deepin-installer-init.log 2>&1

## 处理多屏的情况，安装器以克隆模式运行多屏的情况
setup_monitors >> /var/log/deepin-installer-init.log 2>&1

# 启动窗管，解决部分机型上界面花屏问题, 窗管启动失败不影响安装器本身程序的启动
export HOME=/root
kwin_x11 -platform dde-kwin-xcb || true &

# 后台启动背景窗口的初始化，保证背景的启动过程不会降低安装器主窗口启动的时间
# init &
