#!/bin/bash
####################################################
#                                                  #
#   Installer for Conky Scifi Theme                #
#                                                  #
#   Scripted by Koentje  (remon@cobrasoft.nl)      #
#                                                  #
#                                     version 1.0  #
####################################################
#
#
# INSTALL FOLDER
themefolder="$HOME/.conky/scifi-theme-v1"
#
#
#
####################################################################################################################
 ######################################   DO NOT EDIT BELOW !!!   #################################################
####################################################################################################################
# GET PATH TO WORKING DIRECTORY
  scrpath=$(readlink -f $0)
  workdir=$(dirname $scrpath)
  cd "$workdir"


# GET SYSTEM RESOLUTION
function system_res () {
  mons=0
  echo -e "\n The following monitor setups are found on your system:"
  while read -r ports
  do
     freq=$(xrandr | sed -n "/^$ports/,/^[^[:space:]]/p" | grep '*' | sed 's/*/^/g' | awk -F'^' '{print $1}' | awk '{print $NF}')
     reso=$(xrandr | sed -n "/^$ports/,/^[^[:space:]]/p" | awk 'sub("*"," "){print $1}') #'
     echo -e "  \e[32mMonitor $mons\e[m = \e[33m$reso \e[m\e[90;3m\t($ports)\e[m"
     mons=$((mons+1))
  done < <(xrandr | grep -w 'connected' | awk '{print $1}')
  echo -e "\n If you want to use the \e[33mxinerama_head\e[m option of conky to start\n the theme on a specific monitor, choose your monitor number.\n (press enter if you don't want to use xinerama)\n On which monitor do you want to install the theme?\n "
  printf " Make your choice: \e[32m"; read monitor
  if [ "$monitor" = "" ]; then
    echo -e "\n\e[m xinerama will not be used, conky theme starts on first monitor!"
  elif [[ $monitor -ge 0 ]] && [[ $monitor -le $mons ]]; then
    echo -e "\n\e[m xinerama will be set to monitor \e[32m$monitor\e[m !"
  else
    monitor=""
    echo -e "\n\e[31m ERROR:\e[m monitor \e[31m$monitor\e[m does not exist! Xinirama will not be used."
  fi
}


# SET XINERAMA
function xinerama () {
  if [ "$monitor" = "" ]; then
    echo -e "\n\e[33m Set xinerama off\e[m"
    sed 's/xinerama_head = @MON_NUM@,//'g "$workdir/$scififolder/01-scifi-background-loop.conky" > "$themefolder/01-scifi-background-loop.conky"
    sed 's/xinerama_head = @MON_NUM@,//'g "$workdir/$scififolder/02-scifi-cockpit.conky" > "$themefolder/02-scifi-cockpit.conky"
    sed 's/xinerama_head = @MON_NUM@,//'g "$workdir/$scififolder/03-scifi-overlay-text.conky" > "$themefolder/03-scifi-overlay-text.conky"
  else
    echo -e "\n\e[33m Set xinerama to monitor\e[32m $monitor\e[m"
    sed "s/@MON_NUM@/$monitor/"g "$workdir/$scififolder/01-scifi-background-loop.conky" > "$themefolder/01-scifi-background-loop.conky"
    sed "s/@MON_NUM@/$monitor/"g "$workdir/$scififolder/02-scifi-cockpit.conky" > "$themefolder/02-scifi-cockpit.conky"
    sed "s/@MON_NUM@/$monitor/"g "$workdir/$scififolder/03-scifi-overlay-text.conky" > "$themefolder/03-scifi-overlay-text.conky"
  fi
}


# SET NIC DEVICE
function set_nic () {
  nic=$(ip route get 8.8.8.8 | awk -- '{printf $5}')
  echo -e "\n\e[33;3m Searching for default network device!\n\e[m  Found \e[32m$nic\e[m. Editing conky scripts... \n"
  sed -i -e "s/@NIC_DEV@/$nic/"g "$themefolder/02-scifi-cockpit.conky"
  sed -i -e "s/@NIC_DEV@/$nic/"g "$themefolder/text.lua"
}


# CREATE DESTINATION (SUB)FOLDERS
function make_subfolders () {
  echo -e "\n\e[33;3m Creating theme folder and subfolders!\e[m"
  if [ ! -d "$HOME/.conky" ]; then
    echo -e "  Create $HOME/.conky"
    mkdir "$HOME/.conky"
  fi
  if [ ! -d "$themefolder" ]; then
    echo -e "  Create $themefolder"
    mkdir "$themefolder"
    echo -e "  Create $themefolder/images"
    mkdir "$themefolder/images"
    echo -e "  Create $themefolder/images/globe"
    mkdir "$themefolder/images/globe"
    echo -e "  Create $themefolder/images/space"
    mkdir "$themefolder/images/space"
    echo -e "  Create $themefolder/tempfiles"
    mkdir "$themefolder/tempfiles"
    echo -e "  Create $themefolder/scripts"
    mkdir "$themefolder/scripts"
  fi
  sleep 1
}


# COPY FILES TO DESTINATION
function copy_files () {
  echo -e "\n\e[33;3m Copying files!\e[m"
  echo -e "  Copy images to $themefolder/images"
  cp "$workdir/images/"* "$themefolder/images/" 2>/dev/null
  echo -e "  Copy images to $themefolder/images/globe"
  cp "$workdir/images/globe/"* "$themefolder/images/globe/" 2>/dev/null
  echo -e "  Copy bash scripts to $themefolder/scripts"
  cp "$workdir/$scififolder/scripts/"* "$themefolder/scripts/" 2>/dev/null
  echo -e "  Copy conky & lua scripts to $themefolder"
  cp "$workdir/$scififolder/"* "$themefolder/" 2>/dev/null
  sleep 1
}


# INSTALL FONTS
function install_fonts () {
  echo -e "\n\e[33;3m Installing fonts!\e[m"
  cd "$workdir/fonts"
  ./install-fonts
  sleep 1
}


# CREATE SPACE FRAME PNG FILES
function create_space_frames () {
  echo -e "\e[33;3m Converting space.mp4 into 4450 PNG files (from 40mb to about 3.2Gb)\e[m"
  cd "$workdir/images/space"
  if [ ! -f "./space.mp4" ]; then
    echo -e "\e[31mERROR:\e[m  No 'space.mp4' found! Exit script..."
    exit 1
  fi
  printf "  "; ffmpeg -loglevel error -stats -i "./space.mp4" -vf fps=60 "$HOME/.conky/scifi-theme-v1/images/space/out%d.png"
  sleep 1
}


# CHECK DEPENDENCIES
function dependencies () {
  clear
  echo -e "\n\e[33;3m Checking dependencies..\e[m"
  if [ "$(which sed)" = "" ]; then
    echo -e "\e[31m WARNING:\e[m sed not installed.. Exit script!\n"
    exit 1
  else
    echo -e "  sed    \t\e[32minstalled\e[m"
  fi
  if [ "$(which ffmpeg)" = "" ]; then
    echo -e "\e[31m WARNING:\e[m ffmpeg not installed.. Exit scipt!\n"
    exit 1
  else
    echo -e "  ffmpeg\t\e[32minstalled\e[m"
  fi
  if [ "$(which xrandr)" = "" ]; then
    echo -e "\e[31m WARNING:\e[m xrandr not installed.. Exit script!\n"
    exit 1
  else
    echo -e "  xrandr\t\e[32minstalled\e[m"
  fi
  sleep 2
}


# LAST WORDS
function last_words () {
 echo -e "* Theme can be found in '$themefolder'.\n* Use 'start' script to start the theme.\n* Don't forget the README.TXT !!\e[m\n"
}


# SHOW OPTIONS
function show_options () {
clear
  echo -e "\n\n\e[90m ##########################################################"
  echo -e " #                                                        #"
  echo -e " #-------[\e[m  Installer for Conky Scifi Theme v1.0  \e[90m]-------#"
  echo -e " #                                                        #"
  echo -e " ##########################################################\e[m"
  system_res
  echo -e "\n\n\e[m Choose the matching resolution of the monitor you want to run\n this conky theme on. Supported resolutions are:"
  echo -e "\e[32m  1.  \e[33m1920x1080\e[m"
  echo -e "\e[32m  2.  \e[33m2560x1440\e[m"
  echo -e "\e[32m  3.  \e[33m3440x1440\e[m"
  echo -e "\e[32m  4.  \e[33m3840x2160\e[m\n"
  echo -e "\e[31m  [enter = exit]\e[m\n"
  printf " Make your choice: \e[32m"; read scififolder

  if [ "$scififolder" = "1" ]; then
    scififolder="1920x1080"
  elif [ "$scififolder" = "2" ]; then
    scififolder="2560x1440"
  elif [ "$scififolder" = "3" ]; then
    scififolder="3440x1440"
  elif [ "$scififolder" = "4" ]; then
    scififolder="3840x2160"
  else
    echo -e "\n\e[31mNo valid choice! Exit script..\e[m\n"
    exit 1
  fi

  while true; do
      printf "\n\e[m Your choice was\e[33m $scififolder\e[m. Is this correct?  [y/n] :\e[32m "
      read yn
      case $yn in
          [Yy]* ) make_subfolders; copy_files; install_fonts; create_space_frames; xinerama; set_nic; echo -e "\n\e[32mInstallation finished!\n\e[m"; last_words; exit 0;;
          [Nn]* ) show_options;;
          * ) echo "Please answer yes or no.";;
      esac
  done
}

dependencies
show_options
