#!/usr/bin/env bash 

cc="gcc"

case "$1" in
  -s)
    strip -R .comment -R .note.gnu.property --strip-all "$2" &&
    sstrip "$2"  
    ;;
  -cc)
    if [[ -n "$3" ]]; then
            "$cc" -Oz -nostdinc -ffreestanding -nostdlib -fno-builtin -static -fno-asynchronous-unwind-tables -fno-stack-protector \
    -Wl,--build-id=none -Wl,-nmagic  -s "$2" -o "$3"
    else 
          "$cc" -Oz -nostdlib  -fno-builtin -static -fno-asynchronous-unwind-tables -fno-stack-protector \
    -Wl,--build-id=none -Wl,-nmagic  -s "$2" -o "$2.out"
    fi 
    ;;
  -f) 
    "$0" -cc "$2"
    "$0" -s "$2.out"
    if [[ -n "$3" ]]; then 
      mv "$2.out" "$3"
    fi 
    ;;
  -h)
    echo "tbt: tiny binary tool"
    echo "-h > shows this, for help"
    echo "-f > compiles and strips"
    echo "-cc > compiles"
    echo "-s > strips"
    ;;
  *)
    shift
    echo -e "tbt: \x1b[31m<error>\x1b[0m invalid syntax: \x1b[31m$*"
    ;;
esac
