#!/bin/sh
# Name: linuxqq installer
# Version: 1.0.0
# Description: install netease-cloud-music 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

ARCH=`uname --m`

if [ "$ARCH" = "x86_64" ]; then
PACKAGE_ARCH=amd64
elif [ "$ARCH" = "aarch64" ]; then
PACKAGE_ARCH=arm64
fi

NCM_VER="2.0.0-b2-1076"
NCM_PACKAGE="linuxqq_${NCM_VER}_${PACKAGE_ARCH}.deb"
NCM_URL="https://down.qq.com/qqweb/LinuxQQ/%E5%AE%89%E8%A3%85%E5%8C%85/$NCM_PACKAGE"
SYSTEM_TMP=/tmp
TMP_DIR=/tmp/qq-${NCM_VER}
echo "Downloading deb package from netease ..."
wget ${NCM_URL} -O ${SYSTEM_TMP}/${NCM_PACKAGE}
echo "Successfully downloaded ${TMP_URL} to ${SYSTEM_TMP}/${NCM_PACKAGE}."

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

cp -rf usr/share/applications/qq.desktop /usr/share/applications/
cp -rf usr/local/bin/* /usr/local/bin/
cp -rf usr/local/share/* /usr/local/share/

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

