#!/bin/bash
if [[ $(id -u) -ne 0 ]]; then
  echo "Please run as root"
  echo "Exit"
  echo ""
  exit
fi

PLCROOT="/plc_root"
INITD="/etc/init.d"
RCDINITD="/etc/rc.d/init.d"

if [ -d "$PLCROOT" ]; then
  echo "${PLCROOT} already exists, check the if older version LinuxRTE is running!!"
  echo "Exit"
  echo ""
  exit 1
fi

echo "Copying LinuxRTE to ${PLCROOT}"
mkdir /plc_root
cp LinuxRTE rte.identity rte.key /plc_root
chmod 777 /plc_root/*

if [ -d "$INITD" ]; then
  echo "Copying autorte to ${INITD} and making symbolic links at rc3 & rc5"
  cp autorte /etc/init.d
  chmod 777 /etc/init.d/autorte
  ln -s /etc/init.d/autorte /etc/rc3.d/S80autorte
  ln -s /etc/init.d/autorte /etc/rc5.d/S80autorte
  echo "autorte installed successfully"
  echo "Please reboot now"
  echo ""

else
  if [ -d "$RCDINITD" ]; then
    echo "Copying autorte to ${RCDINITD} and making symbolic links at rc3 & rc5"
    cp autorte /etc/rc.d/init.d
    chmod 777 /etc/rc.d/init.d/autorte
    ln -s /etc/rc.d/init.d/autorte /etc/rc.d/rc3.d/S80autorte
    ln -s /etc/rc.d/init.d/autorte /etc/rc.d/rc5.d/S80autorte
    echo "autorte installed successfully"
    echo "Please reboot now"
    echo ""

  else
    echo "Neither ${INITD} nor ${RCDINITD} exists, no autorte to be launched"
    echo "Exit"
    echo ""
  fi

fi



