#!/bin/bash

URL_BASE="http://tools.1up.no/PRPNet"
PRPNet_VERSION="2.4.7"
PRPNet_CLIENTS="linux macintel windows"

SRC_DIR="src"
BUILD_DIR="build"
DEST_DIR="dest"
LLR_DIR="llr"
if [ "$1" = "-llr" ]; then 
  if [ -d "${LLR_DIR}/$2" ]; then 
    echo "Updating llr client to version $2"
    LLR_VER=$2
  else
    echo "Tried to update llr client, but could not find version $2"
  fi
fi

if [ ! -d $SRC_DIR ]; then 
  mkdir -p $SRC_DIR
fi
if [ ! -d $BUILD_DIR ]; then
  mkdir -p $BUILD_DIR
fi
if [ ! -d $DEST_DIR ]; then 
  mkdir -p $DEST_DIR
fi

for CLIENT in $PRPNet_CLIENTS
do
  echo "Building $PRPNet_VERSION client for $CLIENT"
  SRC_FILE="prpclient-${PRPNet_VERSION}-${CLIENT}"
  SRC_URL="${URL_BASE}/${SRC_FILE}.zip"
  
  if [ ! -f "${SRC_DIR}/${SRC_FILE}.zip" ]; then
    echo "Downloading $SRC_URL"
    wget -q -O ${SRC_DIR}/${SRC_FILE}.zip $SRC_URL
  else 
    echo "Source already downloaded"
  fi

  if [ ! -d ${BUILD_DIR}/${SRC_FILE} ]; then
    echo "Unziping source and patching"
    unzip ${SRC_DIR}/${SRC_FILE}.zip -d $BUILD_DIR >/dev/null 2>&1
    cp master_prpclient.ini ${BUILD_DIR}/${SRC_FILE}/
    if [ "$1" = "-llr" ]; then 
      if [ -d "${LLR_DIR}/${LLR_VER}/${CLIENT}" ]; then 
        echo "Copying new llr client version ${LLR_VER}"
	cp ${LLR_DIR}/${LLR_VER}/${CLIENT}/llr* ${BUILD_DIR}/${SRC_FILE}/programs/ || echo "llr executable missing"
	cp ${LLR_DIR}/${LLR_VER}/${CLIENT}/Readme.txt  ${BUILD_DIR}/${SRC_FILE}/programs/readme_llr.txt || echo "readme file for llr missing"
      else
        echo "Tried to update llr client, but could not find version $2 for $CLIENT"
      fi
    fi
    pushd ${BUILD_DIR}/${SRC_FILE}/ >/dev/null 2>&1
    echo "Removing unwanted ~ backup files"
    find . -name '*~' | xargs rm -f
    FBAK=""
    for BAK in $(find . -iname '*.bak')
    do
      if [ "$FBAK" = "" ]; then 
        echo -n "Removing unwanted .bak backup files: "
	FBAK="done"
      fi
      echo -n "$BAK "
      rm $BAK 2>/dev/null || echo -n "[not removed :(] "
    done
    if [ "$FBAK" = "done" ]; then
      echo 
    fi
    if [ "$CLIENT" = "linux" -o "$CLIENT" = "macintel" ]; then
      echo "Converting to UNIX files"
      find . -type f -name '*.[it][nx][it]' -printf '"%p"\n' | xargs dos2unix
    fi
    if [ "$CLIENT" = "linux" ]; then
      if file -b programs/prpclient | grep x86-64 -q; then 
        echo -n "prpclient is 64-bit.. this is stupid.. "
	if [ -d  ../prpnet/source/ ]; then 
  	  if grep -q "PRPNET_VERSION \"$PRPNet_VERSION\"" ../prpnet/source/defs.h; then
  	    echo "renaming and adding 32-bit version"
	    rm programs/prpclient
	    cp ../prpnet/source/prpclient programs/prpclient
	  fi
	else 
	  echo "download and compile 32-bit in build/prpnet/ to replace the 64-bit version"
	fi
      else
        echo "prpclient is 32-bit and ok!"
      fi
      STRIP=""
      for PRG in $(file programs/* |awk -F: '/not stripped/{print $1}')
      do 
        if [ "$STRIP" = "" ]; then 
	  echo -n "Stripping executables: "
	  STRIP="done"
	fi
	echo -n "$PRG "
	strip $PRG
      done
      if [ "$STRIP" = "done" ]; then
        echo 
      fi
    fi
    if [ "$CLIENT" = "windows" ]; then
      echo "Setting .exe extensions in master_prpclient.ini"
      sed -i 's/^\([[:alpha:]]\{1,\}exe\)=.\/\([[:alpha:]]\{1,\}\)/\1=\2.exe/' master_prpclient.ini
      echo "Converting to DOS files"
      find . -type f -name '*.[it][nx][it]' -printf '"%p"\n' | xargs unix2dos
      if [ -f programs/cllr.exe ]; then
        echo "Found cllr.exe.. removing"
	rm programs/cllr.exe
      fi
    fi
    GENEFER=$(find . -iname '*genef*')
    if [ "$GENEFER" != "" ]; then
      echo -n "Deleting genefer files: "
      for GENEFR in $GENEFER; do
        echo -n "$GENEFR "
        rm $GENEFR
      done
      echo 
    fi
    popd >/dev/null 2>&1
  else
    echo "Unziped build directory exists.. ignoring"
  fi

  if [ "$LLR_VER" != "" ]; then  
    ZIP_FILE=${SRC_FILE}-llr-${LLR_VER}.zip
  else
     ZIP_FILE=${SRC_FILE}.zip
  fi
  if [ ! -f "${DEST_DIR}/${ZIP_FILE}" ]; then
    echo "Building new dest package $ZIP_FILE"
    pushd $BUILD_DIR >/dev/null 2>&1
    zip -r ../${DEST_DIR}/${ZIP_FILE} $SRC_FILE >/dev/null 2>&1
    popd >/dev/null 2>&1  
  else 
    echo "Build exists"
  fi
  echo -e "Finished building\n"
done
