#!/sbin/ksh

if [[ $# < 4 ]]
then
echo "Usage:"
echo "$0 <encrypted root pass> <old crawl pass> <new-crawl pass> <new-crawl ppp pass>"
exit 1
fi

# Update the root password
rootline=`grep "root:" /etc/passwd`
endofline=`echo $rootline | cut -f3,4,5,6,7 -d:`
newline="root:$1:$endofline"
echo $newline > /tmp/pass1.tmp
grep -v "root:" /etc/passwd >> /tmp/pass1.tmp

# Update the crawl password
oldpass="$2"
newpass="$3"
grep -v "crawl Account" /tmp/pass1.tmp > /tmp/pass2.tmp
crawlline=`grep "crawl Account" /tmp/pass1.tmp`
crawlpass=`echo $crawlline | cut -f2 -d:`
if [[ $crawlpass = $oldpass ]]
then
echo "Updating crawl password!"
endofline=`echo $crawlline | cut -f3,4,5,6,7 -d:`
crawlline="crawl:$newpass:$endofline"
fi
echo $crawlline >> /tmp/pass2.tmp

# Update the ppp-crawl password
newppp=$4
grep -v "ppp-crawl:" /tmp/pass2.tmp > /tmp/pass3.tmp
crawlline=`grep "ppp-crawl:" /tmp/pass2.tmp`
endofline=`echo $crawlline | cut -f3,4,5,6,7 -d:`
newline="ppp-crawl:$newppp:$endofline"
echo $newline >> /tmp/pass3.tmp

# Clean-up!
rm -f /tmp/pass1.tmp
rm -f /tmp/pass2.tmp
mv /etc/passwd /etc/passwd.old
mv /tmp/pass3.tmp /etc/passwd
