#!/bin/bash

WEB_CLIENT_DIR="/home/iont.tech/admin/client"
UPDATER_DIR="/home/iont.tech/updater"

sudo mkdir -p "$WEB_CLIENT_DIR"

sudo echo "Updating admin client..." | sudo tee -a "$UPDATER_DIR/update.log"


sudo echo "Removing old admin client files" | sudo tee -a "$UPDATER_DIR/update.log"

sudo rm -rf "$WEB_CLIENT_DIR"/*

if [ $? -eq 0 ]; then
    sudo echo "Old admin client files removed" | sudo tee -a "$UPDATER_DIR/update.log"
else
    sudo echo "Failed to remove old admin client files" | sudo tee -a "$UPDATER_DIR/update.log"
fi


sudo echo "Copying new admin client files" | sudo tee -a "$UPDATER_DIR/update.log"

sudo cp -r /tmp/update/admin-client/* "$WEB_CLIENT_DIR"

if [ $? -eq 0 ]; then
    sudo echo "New admin client files copied" | sudo tee -a "$UPDATER_DIR/update.log"
else
    sudo echo "Failed to copy new admin client files" | sudo tee -a "$UPDATER_DIR/update.log"
fi


sudo chown -R iont.tech:iont.tech "$WEB_CLIENT_DIR"
sudo chmod +x -R "$WEB_CLIENT_DIR"
