#! /bin/bash
# cp-fix home perms 

if ! [ -f "/usr/local/cpanel/cpanel" ]; then
       echo "NotCpanel"
       exit
fi

# Only once at a time please
exec 200<$0
flock -n 200 || exit 1

pscript=$(basename $0)
TODAY=$(date  +"%Y-%b-%d")
YESTERDAY=$(date -d yesterday  +"%Y-%b-%d")
PWDIR="/root/.preports/"$pscript""
OUTFILE="$PWDIR/fixed_homeperms"

Bla="\e[30m"
Red="\e[31m"
Gre="\e[32m"
Yel="\e[33m"
Blu="\e[34m"
Mag="\e[35m"
Cya="\e[36m"
EC="\e[0m"

USAGE=$(echo -e "\n${Red}=== Afrihost cpanel fix perms script ===${EC}
Usage: $pscript domain/username")


#setting environment
env_set() 
{
if [ ! -d "$PWDIR" ]; then
                mkdir -p "$PWDIR" ;
        else
        rm -f $PWDIR/*
fi
}
env_set

one_user_perm_fix () {
# fix perms
perm_fix_file="/backup/pfix_"$PCPUSER""
#for i in $(awk -F ": " '{print $2}' /etc/trueuserdomains | sort | uniq  ) ; do
        echo "chown -Rv "$PCPUSER":"$PCPUSER" /home/"$PCPUSER"" > $perm_fix_file
        echo "chown -v "$PCPUSER":nobody /home/"$PCPUSER"/public_html" >> $perm_fix_file
        echo "/scripts/mailperm --verbose "$PCPUSER"" >> $perm_fix_file
        echo "find /home/"$PCPUSER"/public_html/ -type f -name \*.php -exec chmod 600 {} \;"  >> $perm_fix_file
        echo "find /home/"$PCPUSER"/public_html/ -type f ! -name \*.php -exec chmod 644 {} \;" >> $perm_fix_file
        echo "find /home/"$PCPUSER"/public_html/ -type d -exec chmod 755 {} \;" >> $perm_fix_file
        echo "chmod 750 /home/"$PCPUSER"/public_html" >> $perm_fix_file
        cpl -dr | grep "$PCPUSER" | awk  '{print $NF}' | xargs -i echo "chgrp nobody {}" >> $perm_fix_file

echo -e "==== Going to the these:\n `cat  /backup/pfix`"
echo -e "==== You have 10 seconds to press ctrl+c to CANCEL ===="
sleep 10

screen -dmS perm_fix_"$PCPUSER" -- sh $perm_fix_file

#cd /backup
#split -l $(( $( wc -l < pfix  ) / 10 + 1 )) pfix  pfix.
#for i in $(ls -a1 pfix.a*) ; do
#        screen -dmS $i -- sh $i
#done

#fixperms_monitor () {
#while ps auwfx| grep SCREEN|grep pfix   ; do
#                        sleep 10
#                done
#                echo "::: permfix complete [ OK ] :::"
#}
#fixperms_monitor
}


whole_server () {
# fix perms
for i in $(awk -F ": " '{print $2}' /etc/trueuserdomains | sort | uniq  ) ; do 
        echo "chown -Rv $i:$i /home/$i"
        echo "chown -v $i:nobody /home/$i/public_html"
        echo "find /home/$i/public_html/ -type f -name \*.php -exec chmod 600 {} \;" 
        echo "find /home/$i/public_html/ -type f ! -name \*.php -exec chmod 644 {} \;"
        echo "find /home/$i/public_html/ -type d -exec chmod 755 {} \;"
        echo "chmod 750 /home/$i/public_html"
done  >  /backup/pfix
        echo "/scripts/mailperm --verbose $i" >>  /backup/pfix_mailperm


cd /backup
split -l $(( $( wc -l < pfix  ) / 10 + 1 )) pfix  pfix.
for i in $(ls -a1 pfix.a*) ; do 
        screen -dmS $i -- sh $i 
done

fixperms_monitor () {
while ps auwfx| grep SCREEN|grep pfix   ; do
                        sleep 10
                done
                sh  /backup/pfix_mailperm 
                echo "::: permfix complete [ OK ] :::"
}
fixperms_monitor
}

# Set the PCPUSER variable based on the supplied argument or the second match
if [ -n "$1" ]; then
  PPARG="$1"
  if [ -f "/etc/trueuserdomains" ]; then
    PCPUSER=$(awk -F ": " -v domain="$PPARG" '$2 == domain {print $2; exit}' /etc/trueuserdomains)
    if [ -n "$PCPUSER" ]; then
      echo "Using $PCPUSER based on the supplied domain: $PPARG"
      one_user_perm_fix       
    else
      PCPUSER=$(awk -F ": " -v domain="$PPARG" '$2 == domain {print $2; exit}' /etc/trueuserdomains)
      if [ -n "$PCPUSER" ]; then
        echo "Resetting Perms for: $PCPUSER"
        one_user_perm_fix
      else
        echo "No match found supplied domain or user in /etc/trueuserdomains. Exiting."
        exit 1
      fi
    fi
  else
    echo "No /etc/trueuserdomains file found. Exiting."
    exit 1
  fi
else
 echo "$USAGE"
fi

