#!/bin/sh

if [ "$2" = "" ]; then
  echo "[USAGE]"
  echo "$0 directory_name version"
  echo ""
  echo "Note: Use this script under Cygwin terminal."
  exit
fi

SRC_DIR=$1
VER=$2

PKG_WORK_DIR=_pkg_work_dir_

PKG_NAME="interstellar-console"

if [ -d $SRC_DIR ]; then

  if [ 1 ]; then
    PKG_WIN_ZIP=${PKG_NAME}_${VER}.zip

    rm -rf $PKG_WORK_DIR
    cp -dpuR $SRC_DIR $PKG_WORK_DIR

    cd $PKG_WORK_DIR
    echo $VER > VERSION.txt
    echo en > interstellar-console/isc-lang.txt
    RMLIST=`find . | grep -e '[.]DS_Store' -e 'Thumbs.db'`
    if [ "$RMLIST" != "" ]; then
      rm $RMLIST
    fi
    if [ -d interstellar-console/src ]; then
      cd interstellar-console/src
      make clean
      cp -p ../../../mk_pkg.sh .
      cd ../..
    fi
    rm -r interstellar-console/milkyway/old
    rm -r interstellar-console/milkyway/work
    rm -r interstellar-console/*/setup/old
    cd interstellar-console
    for i in ../../$SRC_DIR/interstellar-console/*.tcl ; do
      OUT=`basename $i`
      echo $i '->' $OUT
      #../../$SRC_DIR/interstellar-console/nkfwin/nkf.exe -O -S -w $i $OUT
      ../../$SRC_DIR/interstellar-console/nkfwin/nkf.exe --ic=CP932 --oc=UTF-8 -O $i $OUT
    done
    cd ..
    #
    rm ../$PKG_WIN_ZIP
    zip -r ../$PKG_WIN_ZIP *
    cd ..
  fi

  PKG_MAC=${PKG_NAME}_remote_macos_${VER}
  PKG_MAC_ZIP=${PKG_MAC}.zip
  rm -rf $PKG_WORK_DIR
  rm -f $PKG_MAC_ZIP
  mkdir $PKG_WORK_DIR
  cd $PKG_WORK_DIR
  mkdir interstellar-console
  echo en > interstellar-console/isc-lang.txt
  cd interstellar-console
  for i in ../../$SRC_DIR/interstellar-console/*.tcl ; do
    OUT=`basename $i`
    echo $i '->' $OUT
    #../../$SRC_DIR/interstellar-console/nkfwin/nkf.exe -O -S -w $i $OUT
    ../../$SRC_DIR/interstellar-console/nkfwin/nkf.exe --ic=CP932 --oc=UTF-8 -O $i $OUT
  done
  cp -p ../../$SRC_DIR/interstellar-console/*.txt .
  cp -p ../../$SRC_DIR/interstellar-console/localConfig.ssc .
  cp -p ../../$SRC_DIR/interstellar-console/isc_remote*.sh .
  cp -p ../../$SRC_DIR/interstellar-console/*.gif .
  cd ..
  cp -p ../$SRC_DIR/LICENCE.interstellar-console.txt .
  zip -r ../$PKG_MAC_ZIP *
  cd ..

fi

