#!/bin/bash

# Copy necessary tools for Cygwin/Windows version
# After, you must copy all files to /bin directory
mkdir -p ${HOME}/ks-tools/bin/
cd /usr/bin

# All executables

list_binary="bash cat clear cut cygwin-console-helper date echo expr ffmpeg ffprobe find grep head ls md5sum mkdir mv cp rm rsync scp sed sleep ssh-add ssh-agent ssh-keygen ssh-keyscan ssh sshpass tail tr wc pwd mintty file ldd touch nano chmod chown make"

echo "* Copying script bashbug"
cp -rfv /usr/bin/bashbug ${HOME}/ks-tools/bin/  
for binary in ${list_binary} ; do
  echo "* Copying binary ${binary}"
  cp -rfv /usr/bin/${binary}.exe ${HOME}/ks-tools/bin/
  echo "* Searching dependencies for ${binary}"
  ldd /usr/bin/${binary}.exe | grep "/bin/" | cut -d ">" -f 2 | cut -d "(" -f 1 | tr -s " " > /tmp/deps.txt
  file_size=$(cat /tmp/deps.txt | wc -l)
  file_scan=1
  echo "* Copying dependencies for ${binary}"
  while [ ${file_scan} -le ${file_size} ] ; do
    library=$(cat /tmp/deps.txt | head -${file_scan} | tail -1)
    cp -rfv ${library} ${HOME}/ks-tools/bin/
    file_scan=$(expr ${file_scan} + 1)
  done
done
strip --strip-all ${HOME}/ks-tools/bin/*
cp -rfv /usr/bin/cyggcc_s-*.dll ${HOME}/ks-tools/bin/
cp -rfv /usr/bin/cygwin1.dll ${HOME}/ks-tools/bin/
cp -rfv /usr/bin/cygz.dll ${HOME}/ks-tools/bin/

# Finally, copy ks-tools scripts here.
