#!/usr/bin/env sh


## Quit if anything goes wrong
set -e


## Location to use: /usr/share/example (default to /)
location="${1-/}"
## Filename name: example.exe
filename="$2"
## Have we printed output
printed="no"


## Print header banner
print_head() {
  if [ "${printed}" = "yes" ]; then
    return
  fi
  printf '┏━(\033[1;31mMessage from Kali developers\033[00m)\n'
  printf '┃\n'
  printed="yes"
}



## Print footer
print_tail() {
  if [ "${printed}" = "no" ]; then
    return
  fi
  printf '┗━\n'
}


## Quick how to use this
[ -z "${filename}" ] \
  && echo "[-] ERROR: Missing filename. $0 <location> <filename>" >&2 \
  && exit


## Find the architecture
arch=$( dpkg --print-architecture )


## Check to see if 64-bit, or if wine32 is already installed
if [ "${arch}" = 'amd64' ] \
|| [ "${arch}" = 'arm64' ] \
&& [ $( dpkg -s wine32 2>/dev/null | grep -c "ok installed" ) -eq 0 ]; then

  ## Are we root? If not, use sudo
  if [ $( id -u ) -ne 0 ]; then
    cmd="$ sudo dpkg --add-architecture i386 && sudo apt update && sudo apt -y install wine32"
  else
    cmd="# dpkg --add-architecture i386 && apt update && apt -y install wine32"
  fi

  ## Give a heads up with what to-do
  print_head
  cat <<END
┃ You may need to install the wine32 package first:
┃  ${cmd}
┃
END
  print_tail
fi

## Move two down, due to arguments
shift
shift

## Move into folder and execute
cd "${location}/" \
  && wine "${filename}" $@
