#####################################################################
#                                                                   #
#                       Fancy waifu motd                            #
#                                                                   #
#####################################################################


##### User config
#####################################################################
RESOLUTION=128                                                      # resolution of waifu
CENTER=true                                                         # center motd?
LOLCAT=true                                                         # use lolcat? (start delay)
UNAMEARG="-sr"                                                      # uname arguments
UPTIMEARG="--pretty"                                                # uptime arguments
MINTERMWIDTH=100                                                    # minimal width of term, to display motd (change depending on resolution, 0 to disable)
MOTDCOLOR="\033[0m"                                                 # Bash color code for motd text (no lolcat)
UNAMECOLOR="\033[0m"                                                # Bash color code for uname (no lolcat)
UPTIMECOLOR="\033[0m"                                               # Bash color code for uptime (no lolcat)
#####################################################################


##### Script config
#####################################################################
CACHEFILE="$HOME/.cache/waifu.cache"                                # to cache waifu state
MOTDPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"    # path to motd folder
tput_cols=$(/usr/bin/tput cols)                                     # terminal columns
WAIFUS=$(($(ls $MOTDPATH/waifus/*.256.png | wc -w) - 1))            # count of waifus
lx="$MOTDPATH/lolcat/lolcat"                                        # lolcat
#####################################################################


function output() {
  val=0
  if [ $1 == "true" ]; then val=$(($val + 1)); fi
  if [ $2 == "true" ]; then val=$(($val + 2)); fi
  case "$val" in
    0)
      echo -en $UPTIMECOLOR
      /bin/cat $MOTDPATH/motd
      echo -en $UNAMECOLOR
      uname $UNAMEARG
      echo -en $UPTIMECOLOR
      uptime $UPTIMEARG
      ;;

    1)
      /bin/cat $MOTDPATH/motd | $lx
      uname $UNAMEARG | $lx
      uptime $UPTIMEARG | $lx
      ;;

    2)
      echo -en $MOTDCOLOR
      /bin/cat $MOTDPATH/motd | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }'
      echo -en $UNAMECOLOR
      uname $UNAMEARG | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }'
      echo -en $UPTIMECOLOR
      uptime $UPTIMEARG | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }'
      ;;
    3)
      /bin/cat $MOTDPATH/motd | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }' | $lx
      uname $UNAMEARG | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }' | $lx
      uptime $UPTIMEARG | awk '{ z = '$tput_cols' - length; y = int(z / 2); x = z - y; printf "%*s%s%*s\n", x, "", $0, y, ""; }' | $lx
      ;;
  esac

printf  '\033[0m' #reset colors
}

 if [ $tput_cols -gt $MINTERMWIDTH ]
 then
  if [ $TERM == "xterm-kitty" ]
  then
    if [ $((0 + RANDOM % 2)) -eq 0 ]
    then
      if [ -f "$CACHEFILE" ]
      then
        WAIFU=$(/bin/cat $CACHEFILE)
      else
        touch $CACHEFILE
        echo 0 > $CACHEFILE
        WAIFU=$((0 + RANDOM % $WAIFUS))
      fi
      if [ $WAIFU -gt $WAIFUS ]
      then
        echo 0 > $CACHEFILE
        WAIFU=$((0 + RANDOM % $WAIFUS))
      else
        echo $(($WAIFU + 1)) > $CACHEFILE
      fi
      if $CENTER; then kitty +kitten icat "$MOTDPATH/waifus/waifu-$WAIFU.$RESOLUTION.png"; else kitty +kitten icat --align left "$MOTDPATH/waifus/waifu-$WAIFU.$RESOLUTION.png"; fi
    else
      if $CENTER; then kitty +kitten icat "$MOTDPATH/waifus/waifu-$((0 + RANDOM % $WAIFUS)).$RESOLUTION.png"; else kitty +kitten icat --align left "$MOTDPATH/waifus/waifu-$((0 + RANDOM % $WAIFUS)).$RESOLUTION.png"; fi
    fi
  fi
  output $LOLCAT $CENTER
 fi
