#!/bin/bash
set -e
trap 'clear' SIGINT
cat > /tmp/.dialogrc << EOL
# Item color
tag_color = (BLACK,WHITE,ON)
EOL
set +ex

# Define menu items and their numbers
MENU_ITEMS=(
  "Add all && Commit && Push"
  "Add all && Commit"
  "Reset soft"
  "Reset hard"
  "Status"
  "Clone submodules"
)

if [ -z "$1" ]; then
  options=""
  for ((i=0; i<${#MENU_ITEMS[@]}; i++)); do
    options+=" \"${MENU_ITEMS[$i]}\" $i"
  done	
    action=$(eval $(echo DIALOGRC=/tmp/.dialogrc dialog --stdout --erase-on-exit --menu \'Choose the action for git\' 10 40 0 $options))
else
    action=${MENU_ITEMS[$1]}
fi

if [[ $action == "Add all && Commit && Push" ]]; then 
  commit_message=$(DIALOGRC=/tmp/.dialogrc dialog --stdout --erase-on-exit --title "$action" --inputbox "Enter the commit message:" 8 40)
  git add .
  git commit -"m $commit_message"
  git push
elif [[ $action == "Add all && Commit" ]]; then 
  commit_message=$(DIALOGRC=/tmp/.dialogrc dialog --stdout --erase-on-exit --title "$action" --inputbox "Enter the commit message:" 8 40)
  git add . 
  git commit -"m $commit_message"
elif [[ $action == "Reset soft" ]]; then
  if DIALOGRC=/tmp/.dialogrc dialog --stdout --erase-on-exit --yesno "Do you wanna soft reset git?" 10 40; then
    git reset
  fi
elif [[ $action == "Reset hard" ]]; then 
  if DIALOGRC=/tmp/.dialogrc dialog --stdout --erase-on-exit --yesno "Do you wanna hard reset git?" 10 40; then
    git reset --hard
  fi
elif [[ $action == "Status" ]]; then 
  git status
elif [[ $action == "Clone submodules" ]]; then 
  git submodule update --init --recursive
fi
