#!/usr/bin/env bash
# Yamada Hayao
# Twitter: @Hayao0819
# Email  : hayao@fascode.net
#
# kokkiemouse
# Mastodon: @kokkiemouse@mstdn.jp 
# Email   : kokkiemouse@fascode.net
#
# (c) 2019-2021 Fascode Network.
#

set -e

alterlive=false
force=false
checklive=false

remove () {
    rm -rf "${@}"
}

_help() {
    echo "usage ${0} [options]"
    echo
    echo " General options:"
    echo "    -f | --force        Force overwriting."
    echo "    -h | --help         This help message and exit."
    echo "    -l | --live         Create files only in a live environment."
    echo "                        Whether it is a live environment or not is determined"
    echo "                        by the presence or absence of the installer."
    echo
    echo "         --alterlive    Remove the file for live session"
    echo "                        If you specify --alterlive, --live is also specified automatically."
}

# Argument analysis and processing
options="${@}"
opt_short="fhl"
opt_long="force,live,help,alterlive"
OPT="$(getopt -o ${opt_short} -l ${opt_long} -- "${@}")"
if (( "${?}" != 0 )); then
    exit 1
fi

eval set -- "${OPT}"
unset OPT
unset opt_short
unset opt_long

while true; do
    case "${1}" in
        -f | --force)
            force=true
            shift 1
            ;;
        -l | --live)
            checklive=true
            shift 1
            ;;
        -h | --help)
            _help
            shift 1
            exit 0
            ;;
        --alterlive)
            alterlive=true
            #checklive=true
            shift 1
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Invalid argument '${1}'" >&2
            _help
            exit 1
            ;;
    esac
done

if [[ "${alterlive}" = true ]]; then
    remove "${HOME}/.config/autostart/genicon.desktop"
fi

if [[ "${checklive}" = true ]]; then
    if ! pacman -Qq alterlinux-calamares 1> /dev/null 2> /dev/null; then
        exit 0
    fi
fi


# ~/Desktopに相当するディレクトリを返します
get_desktop_dir() {
    local _user_config_dir _desktop_dir
    if [[ -v XDG_CONFIG_HOME ]]; then
        _user_config_dir="${XDG_CONFIG_HOME}"
    else
        _user_config_dir="${HOME}/.config"
    fi
    if [[ -f "${_user_config_dir}/user-dirs.dirs" ]]; then
        source "${_user_config_dir}/user-dirs.dirs"
    fi
    if [[ -v XDG_DESKTOP_DIR ]]; then
        _desktop_dir="${XDG_DESKTOP_DIR}"
    else
        _desktop_dir="${HOME}/Desktop"
    fi
    echo -n "${_desktop_dir}"
}

#copy <コピー元> <コピー先>
copy() {
    if [[ ! -f "${1}" ]]; then
        echo "${1} was not found" >&2
        exit 1
    fi
    if [[ -f "${2}/$(basename "${1}")" ]] || [[ -f "${2}" ]] && [[ "${force}" = false ]]; then
        echo "File already exists" >&2
        exit 1
    fi

    cp "${1}" "${2}"
}

desktop_dir="$(get_desktop_dir)"

# set_permission <file>
set_permission(){
    chmod 755 "${1}"

    # Set permission for gnome desktop
    # https://gitlab.com/rastersoft/desktop-icons-ng/-/issues/111
    gio set "${1}" metadata::trusted true
}

# ライブ環境から実行されているかチェックします
check_livecd(){
    if [[ -d "/run/archiso" ]] || grep "^archisobasedir=" "/proc/cmdline" 2> /dev/null 1>&2; then
        return 0
    fi
    return 1
}

# calamaresのアイコン
if check_livecd; then
    source_file="/usr/share/alterlinux/desktop-file/calamares.desktop"
    desktop_icon="${desktop_dir}/$(basename "${source_file}")"
    copy "${source_file}" "${desktop_dir}"
    os_name="$(
        if [[ -f "/etc/os-release" ]]; then
            source "/etc/os-release"
            echo -n "${NAME}"
        else
            echo -n "Alter Linux"
        fi
    )"
    sed -i "s/%OS_NAME%/${os_name}/g" "${desktop_icon}"

    set_permission "${desktop_icon}"
fi


# welcome-page
source_file="/usr/share/alterlinux/desktop-file/welcome-to-alter.desktop"
desktop_icon="${desktop_dir}/$(basename "${source_file}")"
copy "${source_file}" "${desktop_dir}"
set_permission "${desktop_icon}"
