#!/bin/bash

# DISCLAIMER   :   It is recomended to test this script on a test machine.
#				           ManageEngine will not be responsible for any
#                  damage/loss to the data/setup based on the behavior of the script.
#
# DESCRIPTION  :   Script to change wallpaper in linux machines.
#
# ARGUMENT(S):
#
#  1) To change wallpaper with user restriction
#
#       ARGUMENT FORMAT: -w <wallpaper_image>
#       EXAMPLE        : -w sample.jpeg
#
#  2) To disable user restriction
#       ARGUMENT       : -f no
#
#           RETURN VALUE                  MEANING
#
#                0             Wallpaper set successfully.
#                1             Error while setting wallpaper
#                2             Invalid arguments
#                3             Invalid picture type
#
# IMPORTANT NOTE:
#    This script requires the dconf package installed in the machine.
#    If dconf package is not installed , Kindly install dconf and proceed with the script.
#    To see the script output, Kindly enable the option Enable logging in Troubleshooting while deploying configuration.

errorFunc() {
  exit $errorCode
}

for i in 1; do

  errorCode=2

  euid=$(id -u)
  #check root access
  if [ $euid -ne 0 ]; then
    echo "This script must be run as root"
    return $errorCode
  fi

  pictureName=""
  force="yes"

  while getopts "w:f:" option; do
    case $option in
    w)
      pictureName=$OPTARG

      ;;
    f)
      force=$OPTARG

      ;;
    \?)
      echo "Invalid option"
      errorFunc
      ;;
    esac
  done

  errorCode=0
  policy_file="/etc/dconf/db/local.d/99_dcpolicy"
  user_file="/etc/dconf/profile/user"
  locks_file="/etc/dconf/db/local.d/locks/99_dcpolicy"

  if [ ! -z $pictureName ]; then

    if [ ! -e $pictureName ]; then
      echo "file does not exist"
      errorCode=4
      break
    fi

    #Check the format of the picture
    IsFormat=$(echo $pictureName | grep -o '\..*..$')
    if [ \( "$IsFormat" != ".jpg" \) -a \( "$IsFormat" != ".jpeg" \) -a \( "$IsFormat" != ".png" \) ]; then
      echo "Unsupported format : "
      echo "Supported formats : \".jpg\" \".jpeg\" \"\".png\"\""
      errorCode=3
      break
    fi

    IsFolder=$(find /usr/share/ -name mywallpapers | grep "mywallpapers" | wc -l)

    if [ $IsFolder -eq 0 ]; then
      mkdir /usr/share/mywallpapers
      cp -f $pictureName /usr/share/mywallpapers/
    else
      cp -f $pictureName /usr/share/mywallpapers/
    fi
    picturePath=/usr/share/mywallpapers/$pictureName

    rule=""
    errorCode=0

    if [ ! -e /etc/dconf/profile ]; then
      mkdir -p /etc/dconf/profile
    fi
    if [ ! -e $user_file ]; then
      touch $user_file
    fi

    if [ ! -e /etc/dconf/db/local.d ]; then
      mkdir -p /etc/dconf/db/local.d
    fi
    if [ ! -e $policy_file ]; then
      touch $policy_file
    fi

    if [ ! -e /etc/dconf/db/local.d/locks ]; then
      mkdir -p /etc/dconf/db/local.d/locks
    fi
    if [ ! -e $locks_file ]; then
      touch $locks_file
    fi
    
    cat $policy_file | grep -v '^\s*#' | grep org/gnome/desktop/background >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "[org/gnome/desktop/background]
      " >>$policy_file
    fi

    cat $policy_file | grep -v '^\s*#' | grep picture-uri >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      rule="picture-uri='file://$picturePath'"
      line=$(grep -nv '^\s*#' $policy_file | grep org/gnome/desktop/background | head -n 1)
      num=$(echo $line | sed 's/:.*//')
      n=$(($num + 1))
      echo "Line:$n"
      sed -i "$n i $rule" "$policy_file"
      if [ $? -eq 0 ]; then
        echo "Background wallpaper rule added to db file"
      else
        echo "could not add rule to file."
        errorCode=1
        break
      fi
    else
      rule="picture-uri='file:\/\/\/usr\/share\/mywallpapers\/$pictureName'"
      line=$(grep -nv '^\s*#' $policy_file | grep picture-uri | head -n 1)
      num=$(echo $line | sed 's/:.*//')

      echo "Line:$num"
      sed -i "$num s/picture-uri.*/$rule/" "$policy_file"
      if [ $? -eq 0 ]; then
        echo "Background wallpaper rule added to db file"
      else
        echo "could not add rule to file."
        errorCode=1
        break
      fi
    fi

    cat $locks_file | grep -v '^\s*#' | grep /org/gnome/desktop/background/picture-uri >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "/org/gnome/desktop/background/picture-uri" >>$locks_file
    fi

    cat $user_file | grep "user-db:user" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "user-db:user" >>$user_file
    fi
    cat $user_file | grep "system-db:local" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "system-db:local" >>$user_file
    fi

  elif [[ $force = "yes" ]]; then
    echo "Empty option."
    echo "Usage : bash ChangeWallpaperProfile.bash -w <ImageFile>"
    break
  fi

  if [[ $force = "no" ]]; then
    cat $locks_file | grep -v '^\s*#' | grep /org/gnome/desktop/background/picture-uri >/dev/null 2>&1
    if [ $? -eq 0 ]; then

      sed -i "s/\/org\/gnome\/desktop\/background\/picture-uri//" "$locks_file"
      if [ $? -eq 0 ]; then
        echo "successfully disabled the user restriction"
      else
        echo "Failed to update rules"
        errorCode=1
        break
      fi
    fi
  fi

  dconf update
  if [ $? -eq 0 ]; then
    echo "successfully updated Desktop wallpaper rules"
  else
    echo "Failed to update rules"
    errorCode=1
    break
  fi
done

errorFunc
