install_if_not_exists() {
    [ -z $SUDO_USER ] && SUDO_USER=$(who|head -n1|cut -f1 -d" ")
    [ -z $SUDO_HOME ] && SUDO_HOME=$(eval echo ~$SUDO_USER)
    src_path=$1
    dest_path=$2
    if [ ! -e $dest_path ];then
      install -D -m 644 $src_path $dest_path
    fi
}

install_as_user_if_not_exists() {
    [ -z $SUDO_USER ] && SUDO_USER=$(who|head -n1|cut -f1 -d" ")
    [ -z $SUDO_HOME ] && SUDO_HOME=$(eval echo ~$SUDO_USER)
    src_path=$1
    dest_path=$2
    if [ ! -e $dest_path ];then
      SUDO_GROUP=$(id -gn $SUDO_USER)
      install -o $SUDO_USER -g $SUDO_GROUP -D -m 644 $src_path $dest_path
    fi
}

post_install() {
    [ -z $SUDO_USER ] && SUDO_USER=$(who|head -n1|cut -f1 -d" ")
    [ -z $SUDO_HOME ] && SUDO_HOME=$(eval echo ~$SUDO_USER)
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/profile /etc/skel/.config/fcitx5/profile 
    sudo -u $SUDO_USER mkdir -p $SUDO_HOME/.config/fcitx5/conf
    mkdir -p /etc/skel/.config/fcitx5/conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/profile $SUDO_HOME/.config/fcitx5/profile
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/notifications.conf /etc/skel/.config/fcitx5/conf/notifications.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/notifications.conf $SUDO_HOME/.config/fcitx5/conf/notifications.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/waylandim.conf /etc/skel/.config/fcitx5/conf/waylandim.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/waylandim.conf $SUDO_HOME/.config/fcitx5/conf/waylandim.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/xim.conf /etc/skel/.config/fcitx5/conf/xim.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/xim.conf $SUDO_HOME/.config/fcitx5/conf/xim.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/mozc.conf /etc/skel/.config/fcitx5/conf/mozc.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/mozc.conf $SUDO_HOME/.config/fcitx5/conf/mozc.conf
    if [ $(pgrep plasmashell) ]; then
        echo "Setting up fcitx5 for KDE"
        install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/classicui.conf /etc/skel/.config/fcitx5/conf/classicui.conf
        install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/classicui.conf $SUDO_HOME/.config/fcitx5/conf/classicui.conf
    fi
}

post_upgrade() {
    [ -z $SUDO_USER ] && SUDO_USER=$(who|head -n1|cut -f1 -d" ")
    [ -z $SUDO_HOME ] && SUDO_HOME=$(eval echo ~$SUDO_USER)
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/profile /etc/skel/.config/fcitx5/profile 
    sudo -u $SUDO_USER mkdir -p $SUDO_HOME/.config/fcitx5/conf
    mkdir -p /etc/skel/.config/fcitx5/conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/profile $SUDO_HOME/.config/fcitx5/profile
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/notifications.conf /etc/skel/.config/fcitx5/conf/notifications.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/notifications.conf $SUDO_HOME/.config/fcitx5/conf/notifications.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/waylandim.conf /etc/skel/.config/fcitx5/conf/waylandim.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/waylandim.conf $SUDO_HOME/.config/fcitx5/conf/waylandim.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/xim.conf /etc/skel/.config/fcitx5/conf/xim.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/xim.conf $SUDO_HOME/.config/fcitx5/conf/xim.conf
    install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/mozc.conf /etc/skel/.config/fcitx5/conf/mozc.conf
    install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/mozc.conf $SUDO_HOME/.config/fcitx5/conf/mozc.conf
    if [ $(pgrep plasmashell) ]; then
        echo "Setting up fcitx5 for KDE"
        install_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/classicui.conf /etc/skel/.config/fcitx5/conf/classicui.conf
        install_as_user_if_not_exists /usr/share/fcitx5-mozc/fcitx5/conf/classicui.conf $SUDO_HOME/.config/fcitx5/conf/classicui.conf
    fi
}
