#!/bin/bash

desktop=$(ps -e | grep kwin)
timestamp=$( date +%F-%T )

post_install() {

    # Create chrome-remote-desktop group
    [[ $(cat /etc/group) == *chrome-remote-desktop* ]] || groupadd chrome-remote-desktop

    if [[ -n ${desktop} ]]; then

        KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true kdialog --yesno "Set up CRD by issuing the command:<br>
        <b>crd --setup</b> as a normal user. After that, enable access to CRD in your chrome browser.<br>
        To use in servers start or enable the chrome-remote-desktop.service.<br><br>
        You can also start the chrome remote desktop server by issuing the command:<br>
        <b>crd</b>. This will prompt you to set up your CRD if it hasn't been set up already.<br><br>
        Go to https://support.google.com/chrome/answer/1649523 for more information.
        Do you want to save this output?"

        save=${?}

            if [[ ${save} == 1 ]]; then

                KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true kdialog --passivepopup "Output not saved" 10

            else

                echo "Set up CRD by issuing the command:<br>
                      <b>crd --setup</b> as a normal user. After that, enable access to CRD in your Chrome browser.<br>
                      To use in servers start or enable the chrome-remote-desktop.service.<br><br>
                      You can also start the chrome remote desktop server by issuing the command:<br>
                      <b>crd</b>. This will prompt you to set up your CRD if it hasn't been set up already.<br><br>
                      Go to https://support.google.com/chrome/answer/1649523 for more information." > /var/log/chrome-remote-desktop_install${timestamp}.txt

                KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true kdialog --passivepopup "Output is saved in /var/log/chrome-remote-desktop_install${timestamp}.txt" 10
                
            fi   

    else

        echo "Set up CRD by issuing the command:"
        echo "crd --setup"
        echo "as a normal user."
        echo ""
        echo "After that, enable access to CRD in your Chrome browser."
        echo ""
        echo "You can start the chrome remote desktop server by issuing the command:" 
        echo "crd" 
        echo "as a normal user. Or use the chrome-remote-desktop.service to start"
        echo "This will prompt you to set up your CRD if it hasn't been set up already."
        echo ""
        echo "Go to https://support.google.com/chrome/answer/1649523 for more information."

    fi
}
 
post_upgrade() {
  post_install
}
 
post_remove() {
  echo ""
  echo "Please remove ~/.config/chrome-remote-desktop folder manually"
  echo ""
}
