#!/bin/sh
# Name: TencentMeeting installer
# Version: 1.0.2
# Description: install TencentMeeting to your openSUSE.
# Author: Hillwood Yang <hillwood@opensuse.org>
# License: WTFPL-2.0

# force root
if [ "`id -u`" != "0" ]; then
    echo "error: You must be root to use this program!"
    exit 1
fi

if [ "`uname -i `" == "x86_64" ]; then
    PACKAGE_ARCH=x86_64
elif [ "`uname -i `" == "aarch64" ]; then
    PACKAGE_ARCH=arm64
else
    echo "It does not support your arch."
fi
WEMEET_VER="3.15.0.402"
WEMEET_PACKAGE="TencentMeeting_0300000000_${WEMEET_VER}_${PACKAGE_ARCH}_default.publish.deb"
if [ "`uname -i `" == "x86_64" ]; then
    WEMEET_URL="https://updatecdn.meeting.qq.com/cos/22516e32c067130e73b91ea0793495ae/$WEMEET_PACKAGE"
elif [ "`uname -i `" == "aarch64" ]; then
    WEMEET_URL="https://updatecdn.meeting.qq.com/cos/9d3403df3db67092990ce8ad9438a53a/$WEMEET_PACKAGE"
fi
SYSTEM_TMP=/root/cache
TMP_DIR=/root/cache/wemeet-${WEMEET_VER}
mkdir -p ${TMP_DIR}
echo "Downloading deb package from Tencent ..."
wget ${WEMEET_URL} -O ${SYSTEM_TMP}/${WEMEET_PACKAGE}

# sha256sum
SHA256=`sha256sum ${SYSTEM_TMP}/${WEMEET_PACKAGE}`
if [ "`uname -i `" == "x86_64" ]; then
    CHECKSUM=`echo $SHA256 | grep 6c720f9c9e62a1817fc63aee9e96e35e88627c950cbbfad576defabf60da0a67`
elif [ "`uname -i `" == "aarch64" ]; then
    CHECKSUM=`echo $SHA256 | grep 9b208ddec660c9ad6dd086bea22851f8fe6dcf899c45297e100610f935a0652a`
else
    echo "It does not support your arch."
fi
if [ "$CHECKSUM" = "" ]; then
read -p "Check sha256sum faild. Press any key to exit."
echo "Installation failed, please check your network anad try again."
exit 1
else
echo "Successfully downloaded ${TMP_URL} to ${SYSTEM_TMP}/${WEMEET_PACKAGE}."
fi

echo "Unpacking ${WEMEET_PACKAGE} ... it'll take some time ..."
mkdir -p ${TMP_DIR}

pushd ${TMP_DIR} &>/dev/null
mv ../${WEMEET_PACKAGE} .
ar -x ${WEMEET_PACKAGE}
tar -xf data.tar.xz
echo "Successfully unpacked ${SYSTEM_TMP}/${WEMEET_PACKAGE} to ${TMP_DIR}/usr"

if [ -d /usr/lib64/wemeet ]; then
    echo "Remove the old libraries which can't be automatic remove."
    rm -rf /usr/lib64/wemeet
fi

# use hack file
rm -rf opt/wemeet/wemeetapp.sh

# install necessary libraries
mkdir -p /usr/lib64/wemeet
cp -rf opt/wemeet/lib/{libwemeet*,libxcast.so,libxnn*,libtquic.so,libQt5WebKitWidgets.so.5,libQt5WebKit.so.5,libicui18n.so.55,libicuuc.so.55,libicudata.so.55,libdesktop_common.so} /usr/lib64/wemeet

# install icons
cp opt/wemeet/wemeet.svg /usr/share/icons/hicolor/scalable/apps/
for res in {16,32,64,128,256}
do
    mkdir -p /usr/share/icons/hicolor/${res}x${res}/apps;
    mv opt/wemeet/icons/hicolor/${res}x${res}/mimetypes/wemeetapp.png \
    /usr/share/icons/hicolor/${res}x${res}/apps/wemeetapp.png;
done

pushd opt/wemeet/
rm -rf plugins icons lib wemeetapp.sh wemeet.svg
popd

cp opt/wemeet /opt/ -r
popd

popd &>/dev/null
rm -rf ${TMP_DIR}

# Hack qt plugins path to use system default Qt5 plugin
sed -i "s|Prefix = ..|Prefix = /usr/lib64/qt5|g" /opt/wemeet/bin/qt.conf

/sbin/ldconfig
/usr/bin/gtk-update-icon-cache --quiet --force /usr/share/icons/hicolor/
/usr/bin/update-desktop-database /usr/share/applications &>/dev/nul

echo "Congratulations! Installation succeed!"

