#!/bin/bash

# This is AnyDesk update script for Porteus
# Version 2024-12-14

# Copyright 2023-2030, Blaze, Dankov, Russia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# root check
if [ `whoami` != "root" ]; then
    echo -e "\nYou need to be root to run this script.\n"
    exit 1
fi

PRGNAM=${PRGNAM:-anydesk}
VERSION=$(lynx -source "https://download.anydesk.com/linux/" | grep "_x86_64.rpm</a><br>$" | sort -u | tail -1 | cut -d'_' -f2 | cut -d'-' -f1)
SOURCE=$(echo https://download.anydesk.com/linux/${PRGNAM}-${VERSION}-amd64.tar.gz)
BUILD=${BUILD:-1}
BOLD=${BOLD:-"\e[1m"}
CYAN=${CYAN:-"\e[96m"}
GREEN=${GREEN:-"\e[92m"}
RED=${RED:-"\e[31m"}
RESET=${RESET:-"\e[0m"}
CWD=$(pwd)
TMPDIR=/tmp/portch
PKG=$TMPDIR/package-$PRGNAM
PKGINFO=$PKG/var/lib/pkgtools/packages
OUTPUT=${OUTPUT:-/tmp}
ARCH=$( uname -m )

cleanup(){
[ -d $TMPDIR ] && rm -rf $TMPDIR
[ -d $PKG ] && rm -rf $PKG
exit	
}

# check if AnyDesk installed in the system
if [[ -f "/usr/bin/anydesk" ]]; then
  MYVER=${MYVER:-`anydesk --version`}
fi

if [ "$MYVER" == "$VERSION" ]; then
    echo -e "You have the latest ${GREEN}$MYVER${RESET} version of $PRGNAM"
    sleep 5
    exit
else
    read -p "$(echo -e Would you like to build $PRGNAM ${GREEN}$VERSION${RESET} xzm module? [y/n])" -n 1 -r -s && echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "We continue the execution of the $0 script" &>/dev/null
        else
        exit
    fi
fi

rm -rf $PKG
mkdir -p $TMPDIR $PKG
cd $TMPDIR

# download AnyDesk
echo -e "\nDownloading ${CYAN}${BOLD}${PRGNAM}-${VERSION}${RESET}"
wget -q --show-progress "$SOURCE"
tar xf ${PRGNAM}-${VERSION}-amd64.tar.gz
rm -f *.tar.gz

# install binary
install -Dm 755 "${TMPDIR}/anydesk-${VERSION}/anydesk" "${PKG}/usr/bin/anydesk"

# install desktop entry
install -Dm 644 "${TMPDIR}/anydesk-${VERSION}/anydesk.desktop" "${PKG}/usr/share/applications/anydesk.desktop"

# workaround to fix crashes
sed -i 's/^Exec=/Exec=env GDK_BACKEND=x11 /' "${PKG}/usr/share/applications/anydesk.desktop"

# install polkit action
install -Dm 644 "${TMPDIR}/anydesk-${VERSION}/polkit-1/com.anydesk.anydesk.policy" "${PKG}/usr/share/polkit-1/actions/com.anydesk.anydesk.policy"

# install icons
cp -r "${TMPDIR}/anydesk-${VERSION}/icons" "${PKG}/usr/share"

# create rc.service in Slackware BSD style
mkdir -p $PKG/etc/rc.d
wget -q -P $PKG/etc/rc.d https://slackbuilds.org/slackbuilds/15.0/network/anydesk/rc.anydesk

# add AnyDesk global settings
echo '#!/bin/bash

psu /usr/bin/anydesk --admin-settings' > $PKG/usr/bin/anydesk-global-settings
chmod 755 $PKG/usr/bin/anydesk-global-settings

### fake Slackware type package info: super dumb version
mkdir -p $PKGINFO
echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > $PKGINFO/$PRGNAM-$VERSION-$ARCH

cat >> $PKGINFO/$PRGNAM-$VERSION-$ARCH << EOM
PACKAGE DESCRIPTION:
anydesk: anydesk (Remote Desktop Software)
anydesk:
anydesk: Connect to a computer remotely, be it from the other end of the
anydesk: office or halfway around the world. AnyDesk ensures secure and
anydesk: reliable remote desktop connections for IT professionals
anydesk: and on-the-go individuals alike. AnyDesk works across all your
anydesk: devices and operating systems: Windows, Linux, macOS, iOS, Android,
anydesk: FreeBSD and even your Raspberry Pi!
anydesk:
anydesk: Homepage:  http://www.anydesk.com
anydesk:
FILE LIST:
EOM

find * | grep -v var >> $PKGINFO/$PRGNAM-$VERSION-$ARCH

dir2xzm $PKG $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD.xzm

# check on AnyDesk xzm file exists in /tmp
if [ -f "$OUTPUT/$PRGNAM-$VERSION-x86_64-${BUILD}.xzm" ]; then
    echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$VERSION-x86_64-${BUILD}.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
else
    echo -e "\n${RED}${BOLD}Failed. Your $PRGNAM module is not built.${RESET}\n"
fi
cleanup