#!/bin/bash

# shell script to resolve the Vision Docs relationships after install
# argument 1: DOCU_BASEDIR: Where the Docs reside, usually $VISION_SDK/Docs/Vision
# argument 2: TARGET_PLATFORM: what platform you're setting up for
# argument 3: TARGET_LANGUAGE: what language you're setting up for

DOCU_BASEDIR=$1
TARGET_PLATFORM=$2
TARGET_LANGUAGE=$3

###########################################################
################# function declarations ###################
###########################################################

function failWithMessage(){
  echo "\n$1\n"
  #uncomment this to keep the log on screen when run manually
  #read -p "Press any key to exit..."
  exit 1
}

function exitSuccess(){
  echo "\n$1\n"
  exit 0
}

function copyFile(){
  SOURCE_FILE=$1
  TARGET_FILE=$2
  SOURCE_PACKAGE=$3
  DOCU_BASEDIR=$4
  
  # make target dir if not exists
  TARGET_PATH=${TARGET_FILE%/*}
  if [ ! -f "$DOCU_BASEDIR/$TARGET_PATH" ]; then
    mkdir -p "$DOCU_BASEDIR/$TARGET_PATH"
  fi
  
  # create the link
  copyResult=`cp -f "$DOCU_BASEDIR/$SOURCE_FILE" "$DOCU_BASEDIR/$TARGET_FILE" 2>&1`
  
  CP_PID=$!
  CP_STATUS=$?
  
  # cp returns code 0 even when errors occurred on the mac, so we check
  # if something appeared on stderr (redirected to stdout "2>&1")
  if [[ ( $CP_STATUS -ne 0) || ( $copyResult != '' ) ]]; then
    echo "\n$copyResult"
    failWithMessage "Couldn't link or copy from $DOCU_BASEDIR/$SOURCE_FILE to $DOCU_BASEDIR/$TARGET_FILE - an error occurred. This script will now exit.\n\nThe documentation has NOT been installed successfully."
  else
    echo "Successfully copied from $SOURCE_FILE to $TARGET_FILE"
  fi
}

function linkFile(){
  SOURCE_FILE=$1
  TARGET_FILE=$2
  SOURCE_PACKAGE=$3
  DOCU_BASEDIR=$4
  
  # check if source file exists
  if [ ! -f "$DOCU_BASEDIR/$SOURCE_FILE" ]; then
    failWithMessage "Please install the $SOURCE_PACKAGE documentation package and run this script again to complete the setup!"
  fi
  
  # make target dir if not exists
  TARGET_PATH=${TARGET_FILE%/*}
  if [ ! -f "$DOCU_BASEDIR/$TARGET_PATH" ]; then
    mkdir -p "$DOCU_BASEDIR/$TARGET_PATH"
  fi
  
  # create the link
  linkResult=`ln -f "$DOCU_BASEDIR/$SOURCE_FILE" "$DOCU_BASEDIR/$TARGET_FILE" 2>&1`
  
  LN_PID=$!
  LN_STATUS=$?
  
  # ln returns code 0 even when errors occurred on the mac, so we check
  # if something appeared on stderr (redirected to stdout "2>&1")
  if [[ ( $LN_STATUS -ne 0 ) || ( $linkResult != '' ) ]]; then
    echo "\n$linkResult"
    echo "\nLinking the documentation file failed. Falling back to copying.\n"
    copyFile "$SOURCE_FILE" "$TARGET_FILE" "$SOURCE_PACKAGE" "$DOCU_BASEDIR"
  else
    echo "Successfully created link from $SOURCE_FILE to $TARGET_FILE" 
  fi
}

###########################################################
########################## main ###########################
###########################################################

# get base dir
if [ -z $DOCU_BASEDIR ]; then
  SCRIPT_PATH=$( cd $(dirname $0) ; pwd -P )
  DOCU_BASEDIR=`python -c "import os,sys; print os.path.realpath(sys.argv[1])" $SCRIPT_PATH/../Docs/Vision`
fi

if [ ! -d $DOCU_BASEDIR ]; then
  failWithMessage "Please put the setup script into the 'setup' subdirectory of the SDK root and run it from there, or pass the Vision documentation basedir as a parameter!"
fi

if [ -z $TARGET_PLATFORM ]; then
  TARGET_PLATFORM="android"
fi

if [ -z $TARGET_PLATFORM ]; then
  TARGET_LANGUAGE="english"
fi

# make lowercase
TARGET_PLATFORM=`echo $TARGET_PLATFORM | tr '[:upper:]' '[:lower:]'`
TARGET_LANGUAGE=`echo $TARGET_LANGUAGE | tr '[:upper:]' '[:lower:]'`

# remove trailing slash
DOCU_BASEDIR=${DOCU_BASEDIR%/}


linkFile "windows/VisionEngine_Introduction.chm" "android/VisionEngine_Introduction.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/VisionEngine_StartupGuide.chm" "android/VisionEngine_StartupGuide.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/vForgeAPI.chm" "android/vForgeAPI.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/VisionEngine_Common.chm" "android/VisionEngine_Common.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/VisionEngine_Tutorials.chm" "android/VisionEngine_Tutorials.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/Geom2API.chm" "android/Geom2API.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/ScriptingAPI.chm" "android/ScriptingAPI.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/HavokSdk_ProgrammersManual.chm" "android/HavokSdk_ProgrammersManual.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/VisionEngine_ReleaseNotes.chm" "android/VisionEngine_ReleaseNotes.chm" "Windows English" $DOCU_BASEDIR

linkFile "windows/VisionEngine_ArtistManual.chm" "android/VisionEngine_ArtistManual.chm" "Windows English" $DOCU_BASEDIR


exitSuccess "The setup was successful."
