#!/sbin/sh
#
# Warning: if you want to run this script in cm-recovery change the above to #!/sbin/sh
#
# validate_recovery - tool for making sure that there is a working recovery option installed in the /system of Xperias
# nobodyAtall @ xda-developers
#
# v0.01 - Initial addition
VERSION="0.01"

# Defaults
DEBUG=0 # Debug off by default
LOGGING=1 # Logging on by default
VERBOSE=1 # Verbose on by default

# Messages
UID_MSG="Changing user ownership for:"
GID_MSG="Changing group ownership for:"
PERM_MSG="Changing permissions for:"

# Programs needed
AWK="busybox awk"
ECHO="busybox echo"
GREP="busybox grep"
EGREP="busybox egrep"
CAT="busybox cat"
CHOWN="busybox chown"
CHMOD="busybox chmod"
MD5="busybox md5sum"
MOUNT="busybox mount"
UMOUNT="busybox umount"
CUT="busybox cut"
FIND="busybox find"
HEAD="busybox head"
LS="busybox ls"
TAR="busybox tar"
TR="busybox tr"
TEE="busybox tee"
TEST="busybox test"
SED="busybox sed"
RM="busybox rm"
WC="busybox wc"
EXPR="busybox expr"
DATE="busybox date"

# Initialise vars
CODEPATH=""
UID=""
GID=""
PACKAGE=""
RESTORE=0
CREATE=0
SIMULATE=0
SYSREMOUNT=0
SYSMOUNT=0
vr_STARTTIME=$( $DATE +"%m-%d-%Y %H:%M:%S" )
vr_STARTEPOCH=$( $DATE +%s )

vr_parseargs()
{
   # Parse options
   while $TEST $# -ne 0; do
      case "$1" in
         -d)
            DEBUG=1
         ;;
         -r)
            RESTORE=1
         ;;
         -c)
            CREATE=1
         ;;
         -s)
            SIMULATE=1
         ;;
         -V)
            $ECHO "$0 $VERSION"
            exit 0
         ;;
         -*)
            $ECHO "$0: unknown option $1"
            exit 1
         ;;
      esac
      shift;
   done
}

vr_print()
{
   MSG=$@
   if $TEST $LOGGING -eq 1; then
      $ECHO $MSG | $TEE -a $LOG_FILE
   else
      $ECHO $MSG
   fi
}

vr_start()
{
   if $TEST $SIMULATE -eq 0 ; then
      if $TEST $( $GREP -c " /system " "/proc/mounts" ) -ne 0; then
         DEVICE=$( $GREP " /system " "/proc/mounts" | $CUT -d ' ' -f1 )
         if $TEST $DEBUG -eq 1; then
            vr_print "/system mounted on $DEVICE"
         fi
         if $TEST $( $GREP " /system " "/proc/mounts" | $GREP -c " ro " ) -ne 0; then
            $MOUNT -o remount,rw $DEVICE /system
            SYSREMOUNT=1
         fi
      else
         $MOUNT /system > /dev/null 2>&1
         SYSMOUNT=1
      fi
   fi
   if $TEST $( $MOUNT | $GREP -c /sdcard ) -eq 0; then
      LOG_FILE="/data/validate_recovery.log"
   else
      LOG_FILE="/sdcard/validate_recovery.log"
   fi
   if $TEST ! -e "$LOG_FILE"; then
      > $LOG_FILE
   fi
   
   vr_print "$0 $VERSION started at $vr_STARTTIME"
}

vr_chmod()
{
   vr_OLDPER=$1
   vr_OLDPER=$( $ECHO $vr_OLDPER | cut -c2-10 )
   vr_PERSTR=$2
   vr_PERNUM=$3
   vr_FILE=$4
   
   #if the permissions are not equal
   if $TEST "$vr_OLDPER" != "$vr_PERSTR"; then
      if $TEST $VERBOSE -ne 0; then
         vr_print "$PERM_MSG $vr_FILE from '$vr_OLDPER' to '$vr_PERSTR' ($vr_PERNUM)"
      fi
      #change the permissions
      if $TEST $SIMULATE -eq 0; then
         $CHMOD $vr_PERNUM "$vr_FILE"
      fi
   fi
}

date_diff()
{
   if $TEST $# -ne 2; then
      vr_DDM="E"
      vr_DDS="E"
      return
   fi
   vr_DDD=$( $EXPR $2 - $1 )
   vr_DDM=$( $EXPR $vr_DDD / 60 )
   vr_DDS=$( $EXPR $vr_DDD % 60 )
}

vr_all()
{
   if $TEST $CREATE -eq 1; then
      vr_print "Recovery creation started..."
      # Create a valid working image of the recovery
      TARBALL="/sbin/cwm.tar"
      if [ -f "$TARBALL" ]; then
         vr_print "$TARBALL already exists"
         return
      fi
      TEMPFILE1=$( $AWK '/^$/ && ! textFound {next}{textFound=1; print}' /system/bin/chargemon | $HEAD -n 1 | $SED -e 's/^.*\! *//' | $AWK {'print $1'} )
      TEMPFILE2="/system/bin/chargemon"
      TEMPFILE3="/system/bin/recovery.tar"
      $TAR -cvf $TARBALL $TEMPFILE1 $TEMPFILE2 $TEMPFILE3
      ret=$?
      if $TEST $ret -eq 0; then
         vr_print "Recovery image successfully created!"
      else
         vr_print "Error while creating Recovery image!"
         exit 2
      fi
   fi

   if $TEST $RESTORE -eq 1; then
      vr_print "Recovery restore started..."
      # Restore a valid working image of the recovery
      TARBALL="/sbin/cwm.tar"
      if [ -f "$TARBALL" ]; then
         $TAR -xvf $TARBALL
         vr_print "Recovery successfully restored!"
      else
         vr_print "Fatal: $TARBALL not found!"
         exit 5
      fi
   fi
   
   TEMPFILE="/system/bin/chargemon"
   if [ -f "$TEMPFILE" ]; then
      OLD_UGD=$( $LS -ln "$TEMPFILE" )
      OLD_PER=$( $ECHO $OLD_UGD | $CUT -d ' ' -f1 )
      vr_chmod $OLD_PER "rwxr-xr-x" 755 "$TEMPFILE"
   else
      vr_print "Fatal: $TEMPFILE not found!"
      exit 1
   fi
   
   TEMPFILE="/system/bin/recovery.tar"
   if [ -f "$TEMPFILE" ]; then
      OLD_UGD=$( $LS -ln "$TEMPFILE" )
      OLD_PER=$( $ECHO $OLD_UGD | $CUT -d ' ' -f1 )
      vr_chmod $OLD_PER "rwxr-xr-x" 755 "$TEMPFILE"
   else
      TEMPFILE1="/system/bin/xrecovery.tar"
      if [ -f "$TEMPFILE1" ]; then
         OLD_UGD=$( $LS -ln "$TEMPFILE1" )
         OLD_PER=$( $ECHO $OLD_UGD | $CUT -d ' ' -f1 )
         vr_chmod $OLD_PER "rwxr-xr-x" 755 "$TEMPFILE1"
      else
         vr_print "Fatal: neither $TEMPFILE nor $TEMPFILE1 found!"
         exit 2
      fi
   fi
   
   # We also need the compatible busybox / sh
   TEMPFILE=$( $AWK '/^$/ && ! textFound {next}{textFound=1; print}' /system/bin/chargemon | $HEAD -n 1 | $SED -e 's/^.*\! *//' | $AWK {'print $1'} )
   VALIDMD5='a8e20a66939a26dfb524dc7844b1fde9'
   if [ -f "$TEMPFILE" ]; then
      CURRENTMD5=$( $MD5 "$TEMPFILE" | $AWK {'print $1'} )
      if [ "$CURRENTMD5" != "$VALIDMD5" ]; then
         vr_print "Fatal: $TEMPFILE mismatch!"
         exit 4
      fi
      OLD_UGD=$( $LS -ln "$TEMPFILE" )
      OLD_PER=$( $ECHO $OLD_UGD | $CUT -d ' ' -f1 )
      vr_chmod $OLD_PER "rwxr-xr-x" 755 "$TEMPFILE"
   else
      vr_print "Fatal: $TEMPFILE not found!"
      exit 3
   fi
}

vr_end()
{
   if $TEST $SYSREMOUNT -eq 1; then
      $MOUNT -o remount,ro $DEVICE /system > /dev/null 2>&1
   fi
   
   if $TEST $SYSMOUNT -eq 1; then
      $UMOUNT /system > /dev/null 2>&1
   fi
   
   vr_ENDTIME=$( $DATE +"%m-%d-%Y %H:%M:%S" )
   vr_ENDEPOCH=$( $DATE +%s )
   
   date_diff $vr_STARTEPOCH $vr_ENDEPOCH
   
   vr_print "$0 $VERSION ended at $vr_ENDTIME (Runtime:${vr_DDM}m${vr_DDS}s)"
}

#MAIN SCRIPT
vr_parseargs $@
vr_start
vr_all
vr_end
