#!/bin/bash

# This is assistant update script for Porteus
# Version 2026-01-08

# Copyright 2026-2035, 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:-assistant}
BUILD=${BUILD:-1}
ARCH=$( uname -m )
VERSION=$(lynx --source https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=assistant | grep "^pkgver" | cut -d'=' -f2)
SOURCE=$(lynx --source https://aur.archlinux.org/packages/assistant | grep -A15 'Sources' | grep 'assistant-' | cut -d'"' -f2)
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}

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

# check if assistant installed in the system
if [[ -f "/opt/assistant/bin/assistant" ]]; then
    MYVER=${MYVER:-`basename /var/lib/pkgtools/packages/assistant* | cut -d'-' -f2`}
fi

if [ "$MYVER" == "$VERSION" ]; then
    echo -e "You have the latest ${GREEN}$MYVER${RESET} version of $PRGNAM messenger."
    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/etc/rc.d $PKG/usr/bin $PKG/usr/share/{applications,pixmaps}
cd $PKG

# download and extract assistant
echo -e "\nDownloading ${CYAN}${BOLD}${PRGNAM}-${VERSION}${RESET}"
wget -q --show-progress -O ${PRGNAM}-${VERSION}.deb "$SOURCE"
ar p ${PRGNAM}-${VERSION}.deb data.tar.gz | tar xvz
rm -rf ${PRGNAM}-${VERSION}.deb $PKG/opt/assistant/license

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# create a symlink at assistant to usr/bin
ln -sf /opt/${PRGNAM}/bin/${PRGNAM} $PKG/usr/bin

# create assistant.desktop
touch $PKG/usr/share/applications/$PRGNAM.desktop
cat >> $PKG/usr/share/applications/$PRGNAM.desktop << EOM
[Desktop Entry]
Name=Assistant
Name[ru]=Ассистент
GenericName=Remote Desktop
Comment=Remote Desktop
Comment[ru]=Удаленный рабочий стол
Icon=assistant
Exec=assistant -- %u
Terminal=false
Type=Application
Categories=Network;
Keywords=remote;rdp;remote-desktop;vnc;
EOM

# copy assistant icon
cp -a $PKG/opt/assistant/share/icons/$PRGNAM.png $PKG/usr/share/pixmaps

# copy rc.assistant daemon
cp -a $PKG/opt/$PRGNAM/scripts/runasts.sh $PKG/etc/rc.d/rc.$PRGNAM
chmod 644 $PKG/etc/rc.d/rc.$PRGNAM

# downloading dependencies: gtk+2
echo -e "\nDownloading dependencies for ${BOLD}$PRGNAM-$VERSION-$ARCH${RESET}"
rm -f ${OUTPUT}/*.txz
XZM=no getpkg -q gtk+2
tar -xf ${OUTPUT}/gtk+2-*.txz --wildcards 'usr/lib64/libgdk*' 'usr/lib64/libgtk*'
LIBGDK=$(cd $PKG/usr/lib64 ; ls -1 | grep libgdk)
LIBGTK=$(cd $PKG/usr/lib64 ; ls -1 | grep libgtk)
( cd usr/lib64 ; ln -sf $LIBGDK libgdk-x11-2.0.so ; ln -sf $LIBGDK libgdk-x11-2.0.so.0 )
( cd usr/lib64 ; ln -sf $LIBGTK libgtk-x11-2.0.so ; ln -sf $LIBGTK libgtk-x11-2.0.so.0 )
rm -f ${OUTPUT}/*.txz

### 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:
assistant: assistant (remote desktop app)
assistant:
assistant: Secure remote access to computers
assistant: over a local network or over the Internet.
assistant:
assistant: https://xn--80akicokc0aablc.xn--p1ai/
assistant:
FILE LIST:
EOM

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

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

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