#!/bin/sh
# Name: ZWCAD for linux installer
# Version: 1.0.1
# Description: install ZWCAD 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=amd64
elif [ "`uname -i `" == "aarch64" ]; then
    PACKAGE_ARCH=arm64
else
    echo "It does not support your arch."
fi
ZWCAD_VER="22.0.2.4"
ZWCAD_PACKAGE="com.zwsoft.zwcad_${ZWCAD_VER}_${PACKAGE_ARCH}.deb"
ZWCAD_URL="https://download.zwcad.com/zwcad/cad_linux/2022/$ZWCAD_PACKAGE"
SYSTEM_TMP=/root/.cache
TMP_DIR=/root/.cache/ZWCAD-${ZWCAD_VER}
mkdir -p ${TMP_DIR}
echo "Downloading deb package from netease ..."
wget ${ZWCAD_URL} -O ${SYSTEM_TMP}/${ZWCAD_PACKAGE}
echo "Successfully downloaded ${TMP_URL} to ${SYSTEM_TMP}/${ZWCAD_PACKAGE}."

echo "Unpacking ${ZWCAD_PACKAGE} ... it'll take some time ..."
pushd ${TMP_DIR} &>/dev/null
mv ../${ZWCAD_PACKAGE} .
ar -x ${ZWCAD_PACKAGE}
tar -xf data.tar.xz
echo "Successfully unpacked ${SYSTEM_TMP}/${ZWCAD_PACKAGE} to ${TMP_DIR}"

if [ -d "/opt/apps/zwcad" ]; then
    rm -rf /opt/zwcad
fi

install -d /opt/zwcad
cp -r opt/apps/com.zwsoft.zwcad2022/* /opt/zwcad/

chmod +x /opt/zwcad/files/{ZWCAD,ZwLmgr,ZwCrashReport}

# Remove invalid libraries
if [ "`uname -i `" == "aarch64" ]; then
    rm /opt/zwcad/files/{libmonosgen-2.0.so.1,libgmp.so.3,libZwSpaModeler.so}
fi

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

/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!"
