#!/bin/bash
# How to create a specified Encoding
# "echo "$var" | iconv --from-code=utf-8 --to-code=utf-16le --output=file2.txt

fileDir="$(xdg-user-dir HOME)"

settingFileName=".bashrc"
fullFilePath="$fileDir/$settingFileName"

if test -f "$fullFilePath" ; then
  rm -f "$fullFilePath"
fi

echo "# Sample .bashrc for SUSE Linux" >> "$fullFilePath"
echo "# Copyright (c) SUSE Software Solutions Germany GmbH" >> "$fullFilePath"
echo >> "$fullFilePath"
echo "# There are 3 different types of shells in bash: the login shell, normal shell" >> "$fullFilePath"
echo "# and interactive shell. Login shells read ~/.profile and interactive shells" >> "$fullFilePath"
echo "# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all" >> "$fullFilePath"
echo "# settings made here will also take effect in a login shell." >> "$fullFilePath"
echo "#" >> "$fullFilePath"
echo "# NOTE: It is recommended to make language settings in ~/.profile rather than" >> "$fullFilePath"
echo "# here, since multilingual X sessions would not work properly if LANG is over-" >> "$fullFilePath"
echo "# ridden in every subshell." >> "$fullFilePath"
echo >> "$fullFilePath"
# Script to detect if dark mode is activated on the system.
# Currently only GNOME is supported.

set -eu

THEME=$(gsettings get org.gnome.desktop.interface gtk-theme)

THEME=${THEME%\'}  # Remove final quote returned by gsettings
SUBSTR=${THEME%-dark}

if [ "$SUBSTR" = "$THEME" ]
then
  echo "# Light mode shell" >> "$fullFilePath"
  echo "export PS1=\"\[\e[01;32m\]\u\[\e[01;32m\]@\[\e[01;32m\]\h\[\e[0;37m\]:\[\e[01;34m\]\w\[\e[0;37m\]> \[\e[0m\]\"" >> "$fullFilePath"
  echo >> "$fullFilePath"
  echo "# Dark mode shell" >> "$fullFilePath"
  echo "# export PS1=\"\[\e[01;32m\]\u\[\e[01;32m\]@\[\e[01;32m\]\h\[\e[0;37m\]:\[\e[01;33m\]\w\[\e[0;37m\]> \[\e[0m\]\"" >> "$fullFilePath"
  echo >> "$fullFilePath"
else
  echo "# Light mode shell" >> "$fullFilePath"
  echo "# export PS1=\"\[\e[01;32m\]\u\[\e[01;32m\]@\[\e[01;32m\]\h\[\e[0;37m\]:\[\e[01;34m\]\w\[\e[0;37m\]> \[\e[0m\]\"" >> "$fullFilePath"
  echo >> "$fullFilePath"
  echo "# Dark mode shell" >> "$fullFilePath"
  echo "export PS1=\"\[\e[01;32m\]\u\[\e[01;32m\]@\[\e[01;32m\]\h\[\e[0;37m\]:\[\e[01;33m\]\w\[\e[0;37m\]> \[\e[0m\]\"" >> "$fullFilePath"
  echo >> "$fullFilePath"
fi
echo "# Use nano instead of vi" >> "$fullFilePath"
echo "export VISUAL=nano" >> "$fullFilePath"
echo "export EDITOR=\"\$VISUAL\"" >> "$fullFilePath"
echo >> "$fullFilePath"
echo "test -s ~/.alias && . ~/.alias || true" >> "$fullFilePath"

if test -f "$fullFilePath" ; then
  echo "[OK] Settings were created in:"
  echo "    $fullFilePath"
else
  echo "[ERROR] Settings were not created."
fi