#!/bin/bash
set -e

config_name=$1
src_dir=$2/buildsite
target_dir=$3/buildsite
image_prefix=$4

if [ -d $src_dir ]; then
  if [ ! -d $target_dir ]; then
    mkdir -p $target_dir
  fi

  filelist=`ls $src_dir`
  config=`cat $config_name`" image_prefix=$image_prefix"
  for file in $filelist
  do
    template=`cat $src_dir/$file`
    printf "$config\ncat << EOF\n$template\nEOF" | bash > $target_dir/$file
    echo "***Prepared $target_dir/$file"
  done
fi

dir=`pwd`
echo $dir
if [[ $src_dir =~ "ironman/buildsite" ]]; then
  echo "tmp build fe ironman images"
  if [[ -d "$dir/src/ironman-dist" ]]; then
     rm -rf $dir/src/ironman-dist
  fi
  cd /data/fe-web && rm -rf dist && git pull origin dev:dev && \
  npm config set registry http://mirrors.cloud.tencent.com/npm/ && \
  npm i && npm run build &&  cd $dir
  cp -r /data/fe-web/dist  $dir/src/ironman-dist
fi
