#!/bin/sh

# gets the interpreter (python or lua)
#
# parameters:
#   $1: package path

get_interpreter() {   
  local python_app=$1/run.py
  local lua_app=$1/default.lua 
  if [ -e ${python_app} ]; then
    echo "python"
  elif [ -e ${lua_app} ]; then
    echo "lua"
  fi

}


# adds the shebang line
#
# parameters:
#   $1: filename
#   $2: shebang-line
#   $3: basedir

add_shebang() {
  WORKDIR=/mnt/data/package
  touch ${WORKDIR}/tmp_header
  touch ${WORKDIR}/tmp_file
  has_shebang=$( if head $1 | grep "^#\!" > /dev/null;then echo "true";else echo "false"; fi )
  
  if [ ${has_shebang} != "true" ]; then

      echo $2 > ${WORKDIR}/tmp_header
      cp $1 ${WORKDIR}/tmp_file
      cat ${WORKDIR}/tmp_header ${WORKDIR}/tmp_file > $1
      rm ${WORKDIR}/tmp_header ${WORKDIR}/tmp_file
  fi

}


# installs the web-ui 
#
# parameters:
#   $1: package-path
#   $2: barionet/ipam400
#   $3: target-path
install_webui() {
  logger "Installing the new webui..."
  echo "Installing the new webui..."
  if [ ! $2 = "barionet" ]; then
    if [ -d $1/web_ui ]; then
    
      # check for the current/app symbolic link
      app_link="/usr/local/www/current/app"
      app_web="$3/web_ui"
      rm "$app_link" 
      ln -s "$app_web" "$app_link"
      logger "installing the new webui...Done"
    else 
      logger "installing the new webui... no web ui found!"
    fi
  fi
}

# installs the flexa-package
#
# parameters:
#   $1: package-path
#   $2: target-path
#   $3: barionet/ipam400

do_install() {
  echo "install $1 to $2 on $3 device"
  local filename
  if [ -d $1/package ]; then
    cp -rf $1/package/* $2
    install_webui $1/package $3 $2
  else
    cp -rf $1/* $2
    install_webui $1 $3 $2
  fi

  
  rm -rf $1

  # use info from install.json if possible
  if [ -e $2/install.json ]; then
    echo "found installation json!"
    app_name=$( jq -r .scriptPath $2/install.json )
    filename=$2/${app_name}
    interpreter=$( jq -r '.run' $2/install.json | awk '{ print $1 }' )

    if [ $3 != "barionet" ]; then
      if [ ${interpreter} == "lua" ]; then
        shebang="#!/usr/bin/lua"
        
      else 
        shebang="#!/usr/bin/python"
      fi
    else
      if [ ${interpreter} == "lua" ]; then
        shebang="#!/bin/lua"
      else 
        shebang="#!/bin/python"
      fi
    fi

  # if no install.json is present we either have a run.py
  # or a default.lua in the package!
  else 
    package_name="FLEXA"
    interpreter=$( get_interpreter $2 )
    echo "found the following interpreter: "${interpreter}
    if [ $3 = "barionet" ]; then
      if [ ${interpreter} == "lua" ]; then
        app_name=default.lua
        shebang="#!/usr/bin/lua"
        filename=$2/${app_name}
        
      else 
        app_name=run.py
        shebang="#!/usr/bin/python"   
        filename=$2/${app_name}
      fi
     else
      if [ ${interpreter} == "lua" ]; then
        app_name=default.lua
        shebang="#!/usr/bin/lua5.1"
        filename=$2/${app_name}
      else 
        app_name=run.py
        shebang="#!/usr/bin/python"
        filename=$2/${app_name}
      fi
    fi
  fi
  


  add_shebang ${filename} ${shebang} $2
  uci set flexa_agent.service.appl_name=${app_name}
  chmod +x ${filename}
  #/etc/init.d/run-flexa start
}


