#!/bin/bash

#echo "Running postchange"

#echo $1 $2

#remove .dpkg-new extension
if [ `echo $2 | grep .dpkg-new` ]
then
	edited=`echo "$2" |  cut -d '.'  -f1,2`
#	echo $edited
else
	edited="$2"
fi

#create variable for trigger file name
trigger=`echo ${edited##*/}`
#echo "trigger name is $trigger"

if [ "$1" = 'CREATE' ]
then
#echo $1

# the loop below pipes the output of the file into a variable ($line) line by line
ls /home | sed '/Music/ d
/lost+found/ d' | cat | while read line
do

#define output location
#loc="/home/$line/.mint-fm2/triggers/$trigger"
#echo "output location is $loc"

#echo $line #placeholder command to make sure everything is groovy
echo "$1 $edited" > /home/$line/.mint-fm2/triggers/"$trigger"
chown $line:$line /home/$line/.mint-fm2/triggers/"$trigger"
done
fi

if [ "$1" = 'DELETE' ]
then
#echo $1
# the loop below pipes the output of the file into a variable ($line) line by line
ls /home | sed '/Music/ d
/lost+found/ d' | cat | while read line
do
#echo $line #placeholder command to make sure everything is groovy
echo "$1 $2" > /home/$line/.mint-fm2/triggers/"$trigger"
chown $line:$line /home/$line/.mint-fm2/triggers/"$trigger"
done
fi

