#!/bin/bash
#
# Installs control panel .desktop file for all users after software installation"
#
# The top variable passed is the .desktop file name,
# if you wish to use this script somewhere else,
# change that variable (cpname) to something not passed to the script.
# the rest should remain the same if the control panel should appear in "Software"
#
# NOTE: if app is to be included in main installer, include .desktop file in /etc/skel
# and root control panels, and do not rely upon this script to install it for you
#
cpname="$1"
#
####################################################################################
rootdir="/root/.abscontrolcenter/Applications"
skeldir="/etc/skel/.abscontrolcenter/Applications"
rootfile="$rootdir/$cpname"
skelfile="$skeldir/$cpname"
if [ ! -e "$rootfile" ]; then
  echo "Your Control panel may not be properly installed"
  exit
fi

# Lets put the new control center .desktop file into /etc/skel for future users
# if the control center was taken out of /skel, this will silently fail...
# which is exactly what we would like.
#
cp "$rootfile" "$skelfile" 2>/dev/null

# Now we do the same for each user directory on the machine
# this means we have to find each user, check if control center is there
# then copy and set ownership properly
cd /home
for f in `ls -d */`
do 
   cpdir="/home/$f.abscontrolcenter/Applications"
   # pass on install for this user if "Software" directory does not 
   #appear in their private control panel
   if [ ! -d "$cpdir" ]; then
     continue
   fi
   uname=$(echo ${f%\/})
   destfile="$cpdir/$cpname"
   cp "$rootfile" "$destfile" 2>/dev/null
   chown "$uname":users "$destfile" 2>/dev/null
done
