#!/data/data/com.termux/files/usr/bin/env bash
#
# Awesomeshot: A command-line screenshot tool written in bash 5.1.16+.
# https://github.com/mayTermux/awesomeshot
#
# Copyright (c) 2021 - 2022 xShin

version=1.1.0

XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
config_file="${XDG_CONFIG_HOME}/awesomeshot/awesomeshot.conf"

LC_ALL=C
LANG=C

# Color
COLOR_DEFAULT="\e[39m"
COLOR_DANGER="\e[91m"
COLOR_WARNING="\e[93m"
COLOR_SUCCESS="\e[92m"
COLOR_SKY="\e[34m"

# Awesomeshot default config
read -rd '' config <<'EOF'
# Make sure when your phone screenshot the result file image
# PATH is same with this variable. Default my phone result file
# image PATH on "/sdcard/DCIM/Screenshots". If not same,
# you can edit this variable value
screenshot_result_path="/sdcard/Pictures/ScreenMaster"
#screenshot_result_path="/sdcard/DCIM/Screenshots"

# This variable serves to set the convert image with file type non PNG, change this value
# to blank or whatever for disable function and "yes" for enable function.
# Convert to PNG if file not PNG
convert_to_png="yes"

# This variable serves to function to backup original photo
# (screenshot result), change this value to blank or whatever for disable 
# function and "yes" for enable function
backup="yes"

# This variable serves to set the original photo backup PATH
path_backup="/sdcard/DCIM/awesomeshot"

# Array Hex Color
hex_color=(
  "#F8F9FA" # white
  "#000000" # black
  "#282C34" # black-gray
  "#333333" # gray
  "#A8D7FE" # blue sky
)

# This variable serves to convert title bar when editing the image, change this value
# to blank or whatever for disable convert and "yes" for enable function
convert_titlebar="yes"

# This variable serves to set where the title bar place, if "yes" the title bar add on image,
# if blank or whatever the title bar will be added at out of image
add_on_img=""

# This variable serves to set the width and height size of title bar.
# NOTE (Bug Found):
# - Sometimes the title bar result is too big or too small
#
# width_img=$(magick ${file_name} - format "%w" info:)
# height_img=$(magick ${file_name} - format "%h" info:)
# if (( ${width_img} > ${height_img} )); then
#   height_img=${width_img}
# elif (( ${width_img} < ${height_img} )); then
#   width_img=${height_img}
# fi
width_img=500
height_img=1000

# This variable serves to set titlebar color, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
titlebar_color="${hex_color[2]}"

# This variable serves to convert the titlebar text, change this value
# to blank or whatever for disable function and "yes" for enable function.
convert_titlebar_text="yes"

# This variable serves to set titlebar text, if you want to use icon
# you can search on Nerd Fonts Website (https://www.nerdfonts.com/cheat-sheet)
# NOTE: if you use icon from Nerd Fonts, make sure "titlebar_text_font" using nerd fonts to!
titlebar_text="גּ awesomeshot"

# This variable serves to set text position of titlebar text, the value contain 8 wind direction:
# - NorthWest
# - North
# - West
# - Center
# - East
# - SouthWest
# - South
# - SouthEast
# NOTE: Default value is "North"
titlebar_text_position="north"

# This variable serves to set font size
titlebar_text_size="35"

# This variable serves to set the text color, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
titlebar_text_color="${hex_color[0]}"

# This variable serves to set titlebar text background color (background text), change this value
# to "none" for transparent background, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
titlebar_text_background="none"

# This variable serves to set the font used when converting, you can check the list font
# available to use for ImageMagick by command: 
# - magick convert -list font | grep -iE 'font:.*'
titlebar_text_font="JetBrains-Mono-Medium-Nerd-Font-Complete"

# This variable serves to set position of "X" and "Y" titlebar text
titlebar_text_xy="+0+5"

# This variable serves to convert rounded corner, change this value
# to blank or whatever for disable convert and "yes" for enable function
convert_rounded="yes"

# This variable serves to set how many rounded corners
border_radius=20

# This variable serves to convert tiny first border (background image) when editing the image,
# change this value to blank or whatever for disable convert and "yes" for enable function
convert_first_border=""

# This variable serves to set tiny first border color (background image), change this value
# to "none" for transparent background, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
first_border_color="${hex_color[0]}"

# This variable serves to set how many size of the first border
# NOTE: make sure this value is small, otherwise it will look weird
first_border_size=7

# This variable serves to convert tiny first border (background image) when editing the image,
# change this value to blank or whatever for disable convert and "yes" for enable function
convert_first_border=""

# This variable serves to set tiny first border color (background image), change this value
# to "none" for transparent background, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
first_border_color="${hex_color[0]}"

# This variable serves to set how many size of the first border
# NOTE: make sure this value is small, otherwise it will look weird
first_border_size=7

# This variable serves to convert border (background image) when editing the image,
# change this value to blank or whatever for disable convert and "yes" for enable function
convert_second_border="yes"

# This variable serves to set border color (background image), change this value
# to "none" for transparent background, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
# NOTE: if convert_second_border enable and second_border_color value not none
#       will be conflict
#second_border_color="${hex_color[0]}"
second_border_color="none"

# This variable serves to set how many size of the second border
second_border_size=50

# This variable serves to convert border with gradient color,
# change this value to blank or whatever for disable convert and "yes" for enable function
convert_second_border_gradient="yes"

# This variable serves to convert gradient color method
# More information: https://legacy.imagemagick.org/Usage/misc/
# List available method:
# - saddle
# - mesh
interpolate_method="saddle"

second_border_gradient_color=(
  "#C850C0"
  "#FFCC70"
  "#4158D0"
  "#FE7FAA"
)

saddle_color_top_left="${second_border_gradient_color[0]}"
saddle_color_top_right="${second_border_gradient_color[1]}"
saddle_color_bottom_left="${second_border_gradient_color[2]}"
saddle_color_bottom_right="${second_border_gradient_color[0]}"

mesh_color_top_left="${second_border_gradient_color[1]}"
mesh_color_top_right="${second_border_gradient_color[1]}"
mesh_color_bottom_left="${second_border_gradient_color[3]}"
mesh_color_bottom_right="${second_border_gradient_color[3]}"

# This variable serves to convert the shadow of image, change this value
# to blank or whatever for disable function and "yes" for enable function.
convert_shadow="yes"

# This variable serves to set shadow color, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
shadow_color="${hex_color[1]}"

# This variable serves to set shadow size, this variable has four values:
# shadow_size="75x30+0+30"
# that mean:
# - bottom shadow is x75
# - right shadow is +30
# - top shadow is +0
# - left shadow is +30
shadow_size="75x30+0+30"

# This variable serves to convert the footer text, change this value
# to blank or whatever for disable function and "yes" for enable function.
convert_footer=""

# This variable serves to set text of footer, if you want to use icon
# you can search on Nerd Fonts Website (https://www.nerdfonts.com/cheat-sheet)
# NOTE: if you use icon from Nerd Fonts, make sure "footer_font" using nerd fonts to!
footer_text=" Shot by Awesomeshot"

# This variable serves to set text position of footer, the value contain 8 wind direction:
# - NorthWest
# - North
# - West
# - Center
# - East
# - SouthWest
# - South
# - SouthEast
# NOTE: Default value is "South"
footer_position="South"

# This variable serves to set position of "X" and "Y"
footer_xy="+0+45"

# This variable serves to set the font used when converting, you can check the list font
# available to use for ImageMagick by command: 
# - magick convert -list font | grep -iE 'font:.*'
footer_font="JetBrains-Mono-Medium-Nerd-Font-Complete"

# This variable serves to set font size
footer_font_size=25

# This variable serves to set the text color, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
footer_foreground="${hex_color[1]}"

# This variable serves to set footer background color (background text), change this value
# to "none" for transparent background, if you want to custom the color, you can
# change this value with hex color or use the one in the array list.
footer_background="none"

# This variable serves to open the result of image (when editing finished), change this value
# to blank or whatever for disable function and "yes" for enable function.
open_image=""
EOF

# init
file_name=""
interpolate_file_name=""

function getUserConfig() {

  if [ -f "${config_file}" ]; then

    source "${config_file}"

  elif [ ! -f "${config_file}" ]; then

    mkdir -p "${XDG_CONFIG_HOME}/awesomeshot/"
    printf '%s\n' "$config" > "$config_file"
    source "${config_file}"

  fi

  checkConfig

}

function generateDefaultConfig() {
  if [ ! -f "${config_file}" ]; then
    if [ ! -d "~/.config/awesomeshot" ]; then
      mkdir ~/.config/awesomeshot 2> /dev/null
    fi
    printf '%s\n' "$config" > "$config_file"
    stat "SUCCESS" "Success" "Default config has been generated"
    exit
  fi

  if [ -f "${config_file}" ]; then
    read -p "  Config '~/.config/awesomeshot/awesomeshot.conf' already exists, do you want to generate default config [y/N] " ask
    case "${ask}" in
      y|Y )
        printf '%s\n' "$config" > "$config_file"
        stat "SUCCESS" "Success" "Default config has been re-generated"
      ;;
      n|N )
        exit
      ;;
    esac
  fi
}

function header() {
  printf "\n\n                      %-18s\n\n" "AWESOMESHOT v${version}"
}

function title() {
  printf "\n\b\r\033[2K***${1}\n" | sed 's/*/ /g'
}

function subtitle() {
  printf "%-45s" "*******${1}" | sed 's/ /./g' | sed 's/*/ /g'
}

function check() {
  #   
  #   
  if [[ $? -eq 0 && ${PIPESTATUS[0]} -eq 0 ]]; then

    echo -e " ${COLOR_SUCCESS}${COLOR_DEFAULT} "

  else

    echo -e " ${COLOR_DANGER}${COLOR_DEFAULT} "

  fi
}

function autoRun() {
  getUserConfig
  count=1
  header
  title "${COLOR_SKY}[?] WAITING TO TAKE SCREENSHOT${COLOR_DEFAULT}             [${COLOR_WARNING}${count}${COLOR_DEFAULT}]"

  inotifywait -m -e create ${screenshot_result_path} 2> /dev/null | \
    while read get_file_name_result; do
      get_file_name=$(echo -e "${get_file_name_result}" | awk '{print $3}')
      if [[ "${screenshot_result_path}/${get_file_name}" != "${file_name}" || -z ${file_name} ]]; then
        subtitle "[+]*Execute*Program*"

        # Fix bug screenshot filename ".pending" 
        if [ ${get_file_name%%-*} == ".pending" ]; then
          file_name="${screenshot_result_path}/${get_file_name##*-}"
        else
          file_name="${screenshot_result_path}/${get_file_name}"
        fi

        echo -e ""

        if [ "${convert_to_png}" == "yes" ]; then
          convertToPng
        fi

        if [ "${backup}" == "yes" ]; then
          title "${COLOR_SKY}[+] BACKUP IMAGE${COLOR_DEFAULT}"
          backupOriginalPhoto "${get_file_name}"
        fi

        title "${COLOR_SKY}[+] EDITING IMAGE${COLOR_DEFAULT}"
         
        if [ "${convert_titlebar}" == "yes" ]; then
          convertTitleBar
          if [ "${convert_titlebar_text}" == "yes" ]; then
            convertTitleBarText
          fi
        fi

        if [ "${convert_rounded}" == "yes" ]; then
          convertRounded
        fi

        if [ "${convert_first_border}" == "yes" ]; then
          convertFirstBorder
          convertRounded
        fi

        if [ "${convert_shadow}" == "yes" ]; then
          convertShadow
        fi

        if [ "${convert_second_border}" == "yes" ]; then
          convertSecondBorder
        fi

        if [ "${convert_second_border_gradient}" == "yes" ]; then
          convertSecondBorderGradient
        fi
      
        if [ "${convert_footer}" == "yes" ]; then
          convertFooter
        fi

        termux-media-scan "${file_name}" &> /dev/null
        termux-toast -b "${hex_color[0]}" -c "${hex_color[1]}" -g top "${file_name##*/} ✅"
        termux-notification --action "termux-open '${file_name}'" --icon "camera_enhance" \
        --image-path "${file_name}" --priority "high" --title "📸 Awesomeshot v${version}"


        if [ "${open_image}" == "yes" ]; then
          termux-open "${file_name}"
        fi

        count=$(( ${count} + 1 ))

        title "${COLOR_SKY}[?] WAITING TO TAKE SCREENSHOT${COLOR_DEFAULT}             [${COLOR_WARNING}${count}${COLOR_DEFAULT}]"
      fi
    done
}

function getPwd() {
  pwd=$(pwd)
}

function manualRun() {
  getUserConfig
  header
  title "${COLOR_SKY}[?] CHECKING FILE${COLOR_DEFAULT}"
  subtitle "[+]*Set*Status*File*"
  if [ -f ${file_name} ]; then
    check

    if [ "${convert_to_png}" == "yes" ]; then
      convertToPng
    fi

    if [ "${backup}" == "yes" ]; then
      title "${COLOR_SKY}[+] BACKUP IMAGE${COLOR_DEFAULT}"
      backupOriginalPhoto "${file_name}"
    fi

    title "${COLOR_SKY}[+] EDITING IMAGE${COLOR_DEFAULT}"
      
    if [ "${convert_titlebar}" == "yes" ]; then
      convertTitleBar
      if [ "${convert_titlebar_text}" == "yes" ]; then
        convertTitleBarText
      fi
    fi

    if [ "${convert_rounded}" == "yes" ]; then
      convertRounded
    fi

    if [ "${convert_first_border}" == "yes" ]; then
      convertFirstBorder
      convertRounded
    fi

    if [ "${convert_shadow}" == "yes" ]; then
      convertShadow
    fi

    if [ "${convert_second_border}" == "yes" ]; then
      convertSecondBorder
    fi

    if [ "${convert_footer}" == "yes" ]; then
      convertFooter
    fi

    termux-media-scan "${file_name}" &> /dev/null
    termux-toast -b "${hex_color[0]}" -c "${hex_color[1]}" -g top "${file_name} ✅"
    termux-notification --action "termux-open '${file_name}'" --icon "camera_enhance" \
    --image-path "${file_name}" --priority "high" --title "📸 Awesomeshot v${version}"

    setterm -cursor on

    if [ "${open_image}" == "yes" ]; then
      termux-open "${file_name}"
    fi
  else
    echo -e "\n"
    stat "ERROR" "Danger" "${COLOR_DANGER}${file_name}${COLOR_DEFAULT}: file not found"
  fi
}

function backupOriginalPhoto() {
  change_filename_backup=$(echo $1 | sed 's/.png/_backup.png/g')
  get_filename_suffix=${change_filename_backup##*/}

  if [ ! -d ${path_backup} ]; then
    mkdir -p "${path_backup}"
  fi

  cp "${file_name}" "${path_backup}/${get_filename_suffix}"

  if [ -f ${path_backup}/${get_filename_suffix} ]; then

    subtitle "[+]*Backup*File*"
    check

  else

    subtitle "[+]*Backup*File*"
    check

  fi
  termux-media-scan -r "${path_backup}" &> /dev/null
  termux-media-scan "${path_backup}/${change_filename_backup}" &> /dev/null
}

function convertToPng() {
  extension="${file_name##*.}"
  if [ "${extension}" != "png" ]; then
    title "${COLOR_SKY}[+] CONVERTING EXTENSION IMAGE${COLOR_DEFAULT}"
    subtitle "[+]*Convert*.${extension}*To*.png*"

    convert "${file_name}" "${file_name%.*}.png"
    rm "${file_name}"
    termux-media-scan "${file_name}" &> /dev/null
    file_name=${file_name%.*}.png
    check
  fi
}

function convertTitleBar(){
  subtitle "[+]*Set*Title*Bar*"

  g="#27C93F" #green
  y="#FFBD2E" #yellow
  r="#FF5F56" #red
  b="#282C34" #black

  rad=$( echo "0.0025 * ${width_img} * ${height_img} / 100" | bc )
  br=$( echo "${rad} * 5" | bc )
  x0=$( echo "${rad} * 3" | bc )
  y0=$( echo "${br} * 0.5" | bc )
  x1=$( echo "${x0} + ${rad}" | bc )

  declare -A arr=()
  for i in {0..2}; do
    arr[$i,0]=$x0
    arr[$i,1]=$y0
    arr[$i,2]=$x1
    arr[$i,3]=$y0
    x0=$( echo "${x0} + ${rad} * 3" | bc )
    x1=$( echo "${x0} + ${rad}" | bc)
  done

  #1520x720
  #760x360
  if [[ "${add_on_img}" == "yes" ]]; then  
    magick $file_name -fill $b \
      -background ${titlebar_color} \
      -gravity north \
      -chop 0x$br \
      -splice 0x$br \
      -draw "fill ${r}   circle ${arr[0,0]},${arr[0,1]} ${arr[0,2]},${arr[0,3]}
      fill ${y}   circle ${arr[1,0]},${arr[1,1]} ${arr[1,2]},${arr[1,3]} 
      fill ${g}   circle ${arr[2,0]},${arr[2,1]} ${arr[2,2]},${arr[2,3]}" \
      $file_name
  else
    magick $file_name -fill $b \
      -background ${titlebar_color} \
      -gravity north -splice 0x$br\
      -draw "fill ${r}   circle ${arr[0,0]},${arr[0,1]} ${arr[0,2]},${arr[0,3]}
      fill ${y}   circle ${arr[1,0]},${arr[1,1]} ${arr[1,2]},${arr[1,3]} 
      fill ${g}   circle ${arr[2,0]},${arr[2,1]} ${arr[2,2]},${arr[2,3]}" \
      $file_name
  fi
  check
}

function convertTitleBarText() {
  subtitle "[+]*Set*Title*Bar*Text*"

  echo -n "${titlebar_text}" |
  convert "${file_name}" \
    -gravity "${titlebar_text_position}" \
    -pointsize "${titlebar_text_size}" \
    -fill "${titlebar_text_color}" \
    -undercolor "${titlebar_text_background}" \
    -font "${titlebar_text_font}" \
    -annotate "${titlebar_text_xy}" \
    @- "${file_name}"
  check
}

function convertRounded() {
  subtitle "[+]*Set*Rounded*Corner*"

  convert "$file_name" \
    \( +clone -alpha extract -draw \
      "fill black polygon 0,0,0,${border_radius} ${border_radius},0 fill white circle ${border_radius},${border_radius},${border_radius},0" \
      \( +clone -flip \) -compose Multiply -composite \
      \( +clone -flop \) -compose Multiply -composite \
    \) \
    -alpha off -compose CopyOpacity -composite "$file_name"
  check
}

function convertFirstBorder() {
  subtitle "[+]*Set*Image*First*Border*Color*"

  convert "$file_name" -bordercolor "${first_border_color}" -border ${first_border_size} "$file_name"
  check
}

function convertShadow() {
  subtitle "[+]*Set*Image*Shadow*"

  convert "$file_name" \
    \( \
      +clone \
      -background "${shadow_color}" \
      -shadow "${shadow_size}" \
    \) \
    +swap \
    -background none \
    -layers merge \
    +repage "$file_name"
  check
}

function convertSecondBorder() {
  subtitle "[+]*Set*Image*Second*Border*Color*"

  convert "$file_name" -bordercolor "${second_border_color}" -border ${second_border_size} "$file_name"
  check
}

function interpolateSaddle() {
  interpolate_file_name="interpolate_saddle"
  convert \
    \( \
      xc:${saddle_color_top_left} xc:${saddle_color_top_right} +append \
    \) \
    \( \
      xc:${saddle_color_bottom_left} xc:${saddle_color_bottom_right} +append \
    \) -append -size $size_wh \
    xc: +swap -fx 'v.p{i/(w-1),j/(h-1)}' \
    ${XDG_CONFIG_HOME}/awesomeshot/backgrad/${interpolate_file_name}.png
}

function interpolateMesh() {
  interpolate_file_name="interpolate_mesh"
  convert \
    \( \
      xc:${mesh_color_top_left} xc:${mesh_color_top_right} +append \
    \) \
    \( \
      xc:${mesh_color_bottom_left} xc:${mesh_color_bottom_right} +append \
    \) -append -size ${size_wh} \
    xc: +swap -interpolate Mesh -fx 'v.p{i/(w-1),j/(h-1)}' \
    ${XDG_CONFIG_HOME}/awesomeshot/backgrad/${interpolate_file_name}.png
}

function convertSecondBorderGradient() {
  subtitle "[+]*Set*Image*Second*Border*Gradient*"
  size_wh=$(identify -format %wx%h $file_name)
  if [ ! -d "${XDG_CONFIG_HOME}/awesomeshot/backgrad" ]; then
    mkdir ${XDG_CONFIG_HOME}/awesomeshot/backgrad
  fi

  case ${interpolate_method} in
    "saddle"|"Saddle" )
      interpolateSaddle
    ;;
    "mesh"|"Mesh" )
      interpolateMesh
    ;;
  esac

  composite -gravity center "$file_name" ${XDG_CONFIG_HOME}/awesomeshot/backgrad/${interpolate_file_name}.png "$file_name"
  check
}

function convertFooter() {
  subtitle "[+]*Set*Image*Footer*"
  echo -n "${footer_text}" |
  convert "$file_name" \
    -gravity ${footer_position} \
    -pointsize ${footer_font_size} \
    -fill ${footer_foreground} \
    -undercolor ${footer_background} \
    -font ${footer_font} \
    -annotate ${footer_xy} \
    @- "$file_name"
  check
}

function handleInterruptByUser() {

  pkill com.termux

  echo -e "
    \n   [ ${COLOR_WARNING}ERROR${COLOR_DEFAULT} ] > ${COLOR_DANGER}${1}${COLOR_DEFAULT}\n"

  setterm -cursor on

  read -n 1 -s -r -p "   Press any key to exit "

  exit 1

}

function stat() {

  if [ "${2}" == "Success" ]; then

    echo -e "  [ ${COLOR_SUCCESS}${1}${COLOR_DEFAULT} ]\n  ${COLOR_SUCCESS}|\n  ╰─${COLOR_DEFAULT} ${3}"

  elif [ "${2}" == "Warning" ]; then

    echo -e "  [ ${COLOR_WARNING}${1}${COLOR_DEFAULT} ]\n  ${COLOR_WARNING}|\n  ╰─${COLOR_DEFAULT} ${3}"

  elif [ "${2}" == "Danger" ]; then

    echo -e "  [ ${COLOR_DANGER}${1}${COLOR_DEFAULT} ]\n  ${COLOR_DANGER}|\n  ╰─${COLOR_DEFAULT} ${3}"

  fi
  
}

function main() {
  trap "handleInterruptByUser 'Interrupt By User'" 2
  setterm -cursor off 
  ${1} ${2}
}

function help() {
  echo -e "
  $(version)
  A command-line screenshot tool written in bash 5.1.16+

  Usage:
    awesomeshot [args] [file]

  META OPTIONS:
    -h, --help                Show list of command-line options
    -v, --version             Show version of awesomeshot

  RUN OPTIONS:
    -a, --auto                Run awesomeshot with automatic while take screenshot
    -m, --manual              Run awesomeshot with manual (this option require filename)
    -c, --config              Generate default config awesomeshot.conf

  CONFIG OPTIONS:
    screenshot_result_path    Make sure when your phone screenshot the result file image
                              PATH is same with this variable. Default my phone result file
                              image PATH on '/sdcard/DCIM/Screenshots'. If not same,
                              you can edit this variable value

    convert_to_png            This variable serves to set the convert image with file type non PNG, change this value
                              to blank or whatever for disable function and 'yes' for enable function.
                              Convert to PNG if file not PNG

    backup                    This variable serves to function to backup original photo
                              (screenshot result), change this value to blank or whatever for disable 
                              function and 'yes' for enable function

    path_backup               This variable serves to set the original photo backup PATH

    hex_color                 Array Hex Color

    convert_titlebar          This variable serves to convert title bar when editing the image, change this value
                              to blank or whatever for disable convert and 'yes' for enable function

    add_on_img                This variable serves to set where the title bar place, if 'yes' the title bar add on image,
                              if blank or whatever the title bar will be added at out of image

    width_img & height_img    This variable serves to set the width and height size of title bar.
                              NOTE (Bug Found):
                              - Sometimes the title bar result is too big or too small
                              width_img=magick file_name - format '%w' info:
                              height_img=magick file_name - format '%h' info:
                              if (( width_img > height_img )); then
                                height_img=width_img
                              elif (( width_img < height_img )); then
                                width_img=height_img
                              fi

    titlebar_color            This variable serves to set titlebar color, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    convert_titlebar_text     This variable serves to convert the titlebar text, change this value
                              to blank or whatever for disable function and 'yes' for enable function.

    titlebar_text             This variable serves to set titlebar text, if you want to use icon
                              you can search on Nerd Fonts Website (https://www.nerdfonts.com/cheat-sheet)
                              NOTE: if you use icon from Nerd Fonts, make sure 'titlebar_text_font' using nerd fonts to!

    titlebar_text_position    This variable serves to set text position of titlebar text, the value contain 8 wind direction:
                              - NorthWest
                              - North
                              - West
                              - Center
                              - East
                              - SouthWest
                              - South
                              - SouthEast
                              NOTE: Default value is 'North'

    titlebar_text_size        This variable serves to set font size

    titlebar_text_color       This variable serves to set the text color, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    titlebar_text_background  This variable serves to set titlebar text background color (background text), change this value
                              to 'none' for transparent background, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    titlebar_text_font        This variable serves to set the font used when converting, you can check the list font
                              available to use for ImageMagick by command: 
                              magick convert -list font | grep -iE 'font:.*'

    titlebar_text_xy          This variable serves to set position of "X" and "Y" titlebar text

    convert_rounded           This variable serves to convert rounded corner, change this value
                              to blank or whatever for disable convert and 'yes' for enable function

    border_radius             This variable serves to set how many rounded corners

    convert_first_border      This variable serves to convert tiny first border (background image) when editing the image,
                              change this value to blank or whatever for disable convert and 'yes' for enable function

    first_border_color        This variable serves to set tiny first border color (background image), change this value
                              to 'none' for transparent background, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    first_border_size         This variable serves to set how many size of the first border
                              NOTE: make sure this value is small, otherwise it will look weird

    convert_second_border     This variable serves to convert border (background image) when editing the image,
                              change this value to blank or whatever for disable convert and 'yes' for enable function

    second_border_color       This variable serves to set border color (background image), change this value
                              to 'none' for transparent background, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    second_border_size        This variable serves to set how many size of the second border

    convert_second_border_gradient This variable severs to convert border with gradient color,
                                   change this value to blank or whatever for disable convert and 'yes' for enable function

    interpolate_method        This variable serves to convert gradient color method
                              More information: https://legacy.imagemagick.org/Usage/misc/
                              List available method:
                              - saddle
                              - mesh

    convert_shadow            This variable serves to convert the shadow of image, change this value
                              to blank or whatever for disable function and 'yes' for enable function.

    shadow_color              This variable serves to set shadow color, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    shadow_size               This variable serves to set shadow size, this variable has four values:
                              shadow_size='75x30+0+30'
                              that mean:
                              - bottom shadow is x75
                              - right shadow is +30
                              - top shadow is +0
                              - left shadow is +30

    convert_footer            This variable serves to convert the footer text, change this value
                              to blank or whatever for disable function and 'yes' for enable function.

    footer_text               This variable serves to set text of footer, if you want to use icon
                              you can search on Nerd Fonts Website (https://www.nerdfonts.com/cheat-sheet)
                              NOTE: if you use icon from Nerd Fonts, make sure 'footer_font' using nerd fonts to!

    footer_position           This variable serves to set text position of footer, the value contain 8 wind direction:
                              - NorthWest
                              - North
                              - West
                              - Center
                              - East
                              - SouthWest
                              - South
                              - SouthEast
                              NOTE: Default value is 'South'

    footer_xy                 This variable serves to set position of 'X' and 'Y'

    footer_font               This variable serves to set the font used when converting, you can check the list font
                              available to use for ImageMagick by command: 
                              magick convert -list font | grep -iE 'font:.*'

    footer_font_size          This variable serves to set font size

    footer_foreground         This variable serves to set the text color, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    footer_background         This variable serves to set footer background color (background text), change this value
                              to 'none' for transparent background, if you want to custom the color, you can
                              change this value with hex color or use the one in the array list.

    open_image                This variable serves to open the result of image (when editing finished), change this value
                              to blank or whatever for disable function and 'yes' for enable function.
  "
}

function version() {
  echo -e "awesomeshot v.${version}"
}

function checkConfig() {
  if [[ $convert_second_border == "yes" && $convert_second_border_gradient == "yes" ]]; then
    if [ $second_border_color != "none" ]; then
      stat "ERROR" "Danger" "Conflict detected on config '${COLOR_WARNING}convert_second_border${COLOR_DEFAULT}' and '${COLOR_WARNING}convert_second_border_gradient${COLOR_DEFAULT}' enable at the same time"
      echo -e "
      both variable can be enable if '${COLOR_WARNING}second_border_color${COLOR_DEFAULT}' value is none
      "
      exit 1
    fi
  fi
}

case "${1}" in
  -a|--auto )
    main autoRun
  ;;
  -c|--config )
    generateDefaultConfig
  ;;
  -m|--manual )
    if [ ${2} ]; then
      getPwd
      file_name="${pwd}/${2}"
      main manualRun
    else
      echo ""
      stat "ERROR" "Danger" "This option require filename"
      help
      exit 1
    fi
  ;;
  -h|--help )
    help
  ;;
  -v|--version )
    version
  ;;
  *)
    help
  ;;
esac
