#!/bin/bash

path='PATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/root\/bin'
cronfile='/var/spool/cron/asterisk2'
status=$(/sbin/e-smith/config getprop freepbx status || echo "disabled")

# add PATH to fist line if not present; else update it
sed -i "1!b;s/^PATH.*/$path/; t; 1i $path" $cronfile
# remove any other occurence of PATH
sed -i "1p;t; /^PATH.*/d" $cronfile

# check if freepbx enabled
if [[ $status == "enabled" ]]; then
 # if enabled uncomment
 sed -ri  's/^#(\@[a-zA-Z]+ .*|[0-9*]+ [0-9*]+ [0-9*]+ [0-9*]+ [0-9*]+ .*)/\1/' $cronfile
else
 # if disabled comment out
 sed -ri  's/^(\@[a-zA-Z]+ .*|[0-9*]+ [0-9*]+ [0-9*]+ [0-9*]+ [0-9*]+ .*)/#\1/' $cronfile
fi
#remove any duplicate entry
awk '!x[$0]++' ${cronfile} >${cronfile}-tmp && cat ${cronfile}-tmp > ${cronfile} && rm -f ${cronfile}-tmp
