#!/bin/sh

BOLD='\033[1m'
ITALIC='\033[3m'
NC='\033[0m'
BBCYAN='\033[1;96m'

PROGRAM="${BBCYAN}SysD Manager${NC}"
PROXY_BIN="sysd-manager-proxy"

if [ $# -eq 0 ]; then
    TARGET="release"
    BUILD_ARG="--release"
fi

if [[ "$1" == "debug" ]]; then
    echo Debug installation
    TARGET="debug"
fi

REL_PATH=".."

echo -e Compiling $PROGRAM program
echo ""
cargo build $BUILD_ARG || exit 1
echo ""
echo Generating files
echo ""
cargo run -p transtools -- packfiles
echo ""
echo Installing files
echo ""
sudo install -vDm755 "${REL_PATH}/target/${TARGET}/sysd-manager" -t "/usr/bin"
sudo install -vDm644 "${REL_PATH}/target/loc/io.github.plrigaux.sysd-manager.desktop" -t "/usr/share/applications"
sudo install -vDm644 "${REL_PATH}/target/loc/io.github.plrigaux.sysd-manager.metainfo.xml" -t "/app/share/metainfo"
sudo install -vDm644 "${REL_PATH}/data/icons/hicolor/scalable/apps/io.github.plrigaux.sysd-manager.svg" -t "/usr/share/icons/hicolor/scalable/apps/"
sudo install -vDm644 "${REL_PATH}/data/schemas/io.github.plrigaux.sysd-manager.gschema.xml" -t "/usr/share/glib-2.0/schemas"   
sudo cp -r -v "${REL_PATH}/target/locale"  "/usr/share/" 
#sudo install -Dm644 "data/applications/org.freedesktop.policykit.sysd-manager.policy" -t "/usr/share/polkit-1/actions/"

echo Installing $PROGRAM Proxy  
cargo build $BUILD_ARG --manifest-path ${REL_PATH}/${PROXY_BIN}/Cargo.toml
sudo install -vDm755 "${REL_PATH}/target/${TARGET}/${PROXY_BIN}" -t "/usr/bin"
#sudo install -vDm644 "./sysd-manager-proxy/data/io.github.plrigaux.SysDManager.conf" -T  "/usr/share/dbus-1/system.d/io.github.plrigaux.SysDManager.conf"
#sudo sed -i -e s/{BUS_NAME}/io.github.plrigaux.SysDManager/ -e s/{DESTINATION}/io.github.plrigaux.SysDManager/ -e s/{ENVIRONMENT}// -e s/{INTERFACE}/io.github.plrigaux.SysDManager/ "/usr/share/dbus-1/system.d/io.github.plrigaux.SysDManager.conf"
#sudo install -vDm644 "./sysd-manager-proxy/data/io.github.plrigaux.SysDManager.policy" -t "/usr/share/polkit-1/actions"
#sudo install -vDm644 "./sysd-manager-proxy/data/50-io.github.plrigaux.SysDManager.rules" -t "/usr/share/polkit-1/rules.d"
#sudo install -vDm644 "./sysd-manager-proxy/data/sysd-manager-proxy.service" -T "/usr/lib/systemd/system/sysd-manager-proxy.service"
#sudo sed -i -e s/{BUS_NAME}/io.github.plrigaux.SysDManager/ -e s/{DESTINATION}/io.github.plrigaux.SysDManager/ -e s/{ENVIRONMENT}// -e s/{EXECUTABLE}/\\/usr\\/bin\\/sysd-manager-proxy/ -e s/{INTERFACE}/io.github.plrigaux.SysDManager/ -e s/{SERVICE_ID}/sysd-manager-proxy/ "/usr/lib/systemd/system/sysd-manager-proxy.service"

echo ""
echo Executing Install script
/usr/bin/sysd-manager-proxy install

echo -e Compiling ${BOLD}Schemas${NC} 
sudo glib-compile-schemas "/usr/share/glib-2.0/schemas"
echo ""

echo -e Installation of $PROGRAM completed, enjoy.

COMPILE_SIZE=$(du -sh "${REL_PATH}/target")
COMPILE_SIZE=$(echo $COMPILE_SIZE "Hello World" | cut -d ' ' -f 1) 

# COMPILE_SIZE_ARR=$($COMPILE_SIZE)

echo ""
echo -e "${ITALIC}${BOLD}Hint:${NC} ${ITALIC}run the command line ${BOLD}cargo clean${NC} ${ITALIC}to remove compiled files and save ${ITALIC}${BOLD}${COMPILE_SIZE}${NC} ${ITALIC}of disk space."
