#!/bin/bash

STABLE="yes"
INSTALL="no"
MIGRATE="no"
REVERT="no"
CHANNEL="prod"
LOCK_FILE=/home/mopidy/.migrate_lock
RESULT_LOCK_FILE=/home/mopidy/.mopidy_migrated

POSITIONAL=()
while [[ $# -gt 0 ]]
do
    key=$1
    case $key in
        --unstable)
        STABLE="no"
        shift
        ;;
        --install)
        INSTALL="yes"
        shift
        ;;
        --migrate)
        MIGRATE="yes"
        shift
        ;;
        --revert)
        REVERT="yes"
        shift
        ;;
        --chan|-c)
        CHANNEL="$2"
        shift
        shift
        ;;
        *)
        POSITIONAL+=("$1")
        shift
        ;;
    esac
done
set -- "${POSITIONAL[@]}"

if [ -f $LOCK_FILE ]; then
  echo "Migration in progress, skipping"
  exit 0
fi

#if grep -q 200 $RESULT_LOCK_FILE; then
#  echo "Musicbox already migrated, skipping"
#  exit 0
#fi

touch $LOCK_FILE

DESKTOP_UUID_FILE=/home/mopidy/.muzlab_device_uuid

if [[ $INSTALL == "yes" ]]; then
  echo "Getting last stable version for channel: ${CHANNEL} stable: ${STABLE}";

  DESKTOP_VERSION=$(curl "https://my.muzlab.ru/api/v1/versioning/get_version?channel=${CHANNEL}&stable=${STABLE}&type=desktopbox" | tr -d '"');
  rm -rf /var/lib/apt;

  apt update || true;   #Обновляем репозиторий

  REQ_FULL_VERSION=$(apt-cache -qq policy muzlab-client-desktop \
                      | grep "Version table:" -A9999 | grep $DESKTOP_VERSION-$CHANNEL \
                      | egrep -o '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\-[[:lower:]]{1,8}\-[[:digit:]]{3}');

  if [ -z "$REQ_FULL_VERSION" ]; then
    echo "Unable to find required version ${DESKTOP_VERSION} in repo, exiting...";
    exit 1;
  fi

  echo "Installing correct desktop player version";
  apt install -y --allow-downgrades muzlab-client-desktop=${REQ_FULL_VERSION};

  echo "Waiting for desktop to start";
  until [ -f $DESKTOP_UUID_FILE ]; do sleep 5; done

  echo "Desktop started, ready to migrate..";
  rm -f $LOCK_FILE;
fi

MUSICBOX_ID=$(hostname -s | egrep -o '[[:digit:]]{1,5}' | head -n1)
DESKTOP_UUID=$(cat /home/mopidy/.muzlab_device_uuid)

if [[ $MIGRATE == "yes" ]]; then
  rm -f $RESULT_LOCK_FILE

  curl -X POST "https://my.muzlab.ru/api/v1/devices/migrate_musicbox?id=${MUSICBOX_ID}&uuid=${DESKTOP_UUID}" >> $RESULT_LOCK_FILE

  if grep -q 200 $RESULT_LOCK_FILE; then
    echo "Musicbox successfully migrated, stopping mopidy service"

    systemctl stop mopidy
    systemctl disable mopidy

    systemctl start desktop_player
    systemctl enable desktop_player

    echo "Mopidy service stopped, cleaning up.."
    rm -f $LOCK_FILE
  fi
fi

if [[ $REVERT == "yes" ]]; then
  rm -f $RESULT_LOCK_FILE

  curl -X POST "https://my.muzlab.ru/api/v1/devices/migrate_desktop?id=${MUSICBOX_ID}&uuid=${DESKTOP_UUID}" >> $RESULT_LOCK_FILE

  if grep -q 200 $RESULT_LOCK_FILE; then
    echo "Desktop successfully migrated, starting mopidy service, stopping desktop.."

    systemctl start mopidy
    systemctl enable mopidy

    systemctl stop desktop_player
    systemctl disable desktop_player

    echo "Desktop service stopped, cleaning up.."
    rm -f $LOCK_FILE
  fi
fi

#rm -f $LOCK_FILE
exit 0

