#!/bin/bash
printf '\e[8;33;80t]'
#
# Custom build script
# Copyright © 2017, Nana Iyke Quame "iyke"
# <nanaquameallday@gmail.com>
#
#Android_Matrix_Development
#DroidPeepz™ Inc
#
# NOTE : This Is Meant For  The Good & Ease Of Kernel Development,
#  - Please You Are Free To Edit And Make It Better, But DO NOT STEAL !
#  -
#
#--------------------------
# Updated On : 2017/09/23 
# by : @artificerpi 
# reason : Change to use `relative path` 
# commit : https://github.com/iykequame/android_system_extraction_and_repack_tool/pull/3/commits/4c8691b17c407a6d5eae9d408ba69c473e0f06c6 
# commit-ID : 4c8691b17c407a6d5eae9d408ba69c473e0f06c6
#--------------------------
user=$(echo "$(whoami | gawk '{ print $1 }')")
PROJECT_DIR=`realpath "$0" | sed 's|\(.*\)/.*|\1|'`
flicx="$PROJECT_DIR/.flicx/flicx.sh";
permsda=chmod 755 $PROJECT_DIR/tools/sdat2img.py
permimg=chmod 755 $PROJECT_DIR/tools/img2simg.py
permext=chmod 755 $PROJECT_DIR/tools/make_ext4fs
permext=chmod 755 $PROJECT_DIR/tools/img2simg.py
#-------------------

#START - HOME
START(){
clear
echo -e "                        \033[0;34m "
cecho P"" "                     \e[4m*||_http://www.droidpeepz.xyz_||*"
echo
cecho P ""  "                     \e[7m================================="
cecho C ""  "            \e[7m===!! Android System Extraction/Repack Tool. !!==="
cecho G "" "                                     \e[7mFor"
cecho C "" "                     \e[7m*..MediaTek & Qualcomm Devices..*"
echo
echo "                     ========*****************========"
cecho G ""  "                          ---> DroidPeepz™ Inc    "
cecho P ""  "                          ---> by : Nana Iyke Quame   "
echo "                     ========*****************========"
echo "                               Credit Goes To:"
cecho Y "" "                      **ANDROID MATRIX DEVELOPMENT**"
cecho P "" "                  & @xpirt, @Mesaj, @matrix, @artificerpi "
echo
echo
CT="$(date +"%r")"
DT="$(date +"%b-%d-%Y")"
cecho B "" "    \e[7m\e[3mMenu" "                        \033[0;36m\e[3m|| Time : $CT"
cecho C "" "    \e[3mHost System : $HOSTNAME" "    \033[0;36m\e[3m|| Date : $DT" 
echo
cecho Y "    \033[0mf - " "Convert file_contexts "
echo
cecho C "    i - " "Unpack "system.new.dat" "
cecho R "" "        NOTE : This process will request for your "sudo" password! "
echo
cecho G "    y - " "Repack To "system.new.dat" "
echo
cecho C "    v - " "View Credits + info"
echo
cecho R "    x - " "Exit"
echo
}


 #MENU
EXT_MAIN(){
while :
do

  clear
  START
  read -p "           Enter option: " CHOICE
  case "$CHOICE" in
			Y|y ) RePck;;
			F|f ) FILCX;;
			I|i ) UnPck;;
			V|v) V_CREDIT;;
			X|x) EXIT;;
				*) cecho R "" "     Invalid option"; sleep 0.3; continue;;
	esac

done
}

PAGE(){
clear
echo -e "                        \033[0;34m "
cecho P"" "                     \e[4m*||_http://www.droidpeepz.xyz_||*"
echo
echo
cecho P ""  "                     \e[7m================================="
cecho C ""  "            \e[7m===!! Android System Extraction/Repack Tool. !!==="
cecho G "" "                                     \e[7mFor"
cecho C "" "                     \e[7m*..MediaTek & Qualcomm Devices..*"
echo
echo
echo
}

TIME(){
CT="$(date +"%r")"
DT="$(date +"%b-%d-%Y")"
cecho B "" "    \e[7m\e[3mMenu" "                        \033[0;36m\e[3m|| Time : $CT"
cecho C "" "    \e[3mHost System : $HOSTNAME" "    \033[0;36m\e[3m|| Date : $DT"
}

# File_context_zone
COTEXTDIR=./file_context_zone
CONFOLDER=./file_context_zone/convert
REVFOLDER=./file_context_zone/revert
BINNAME=file_contexts.bin
NEWBINFILE=new-$(date +%Y%m%d)-$BINNAME

#ST_DEB
FILCX(){
clear
PAGE
echo
cecho P "" "                   \e[7m*||_File_contexts.bin Convert Zone_||*"
echo
echo "                     ========*****************========"
echo "                               Credit Goes To:"
cecho C "" "            	    cofface@cofface.com - source script  "
cecho B "" "             	     Pom Kritsada @MTK THAI Developers  "
echo "                     ========*****************========"
echo
TIME
echo
cecho Y "    \033[0mv - " "View Credits & info"
echo
cecho C "    c - " "Convert"
echo
cecho G "    r - " "Revert "
echo
cecho R "    h - " "go to home"
echo
echo
read -p "           Enter option: " x
case "$x" in
	V|v) V_CREDIT;;
	C|c) CONVERT_FILCX;;
	R|r) REVERT_FILCX;;
	H|h ) START;;
	*) cecho R "" "     Invalid option"; sleep 0.3; continue;;
esac
FILCX
}

CONVERT_FILCX(){
clear
PAGE
echo -e "                        \033[0;34m "
echo " Please wait"
mkdir -p $CONFOLDER
sleep 1.0;
if [ -f $CONFOLDER/file_contexts* ]
then
 echo -e "  \033[0;31m "
 echo "            File." "file_context already exist in folder; "
 echo "            continuing will be overwrite it !!! "
 echo
 read -p "             to continue, press Enter ... "
fi
./tools/lib64/sefcontext -o $CONFOLDER/file_contexts $COTEXTDIR/$BINNAME > /dev/null
if [ $? == 0 ]
then
 echo -e "                        \033[0;34m "
 echo " !! DONE !!"
 read -p                 "Press enter key to continue . . ."
 echo
else
 echo " !! Faild !!"
 read -p                 "Press enter key to continue . . ."
fi
}

REVERT_FILCX() {
clear
PAGE
echo -e "                        \033[0;34m "
echo " Please wait"
mkdir -p $REVFOLDER
sleep 1.0;
if [ -f $REVFOLDER/file_contexts.bin* ]
then
 echo -e "  \033[0;31m "
 echo "            File." "file_context.bin already exist in folder; "
 echo "            continuing will be overwrite it !!! "
 echo
 read -p "             to continue, press Enter ... "
fi
./tools/lib64/sefcontext_compile -o $REVFOLDER/$NEWBINFILE $CONFOLDER/file_contexts > /dev/null
if [ $? == 0 ]
then
 echo -e "                        \033[0;34m "
 echo " !! DONE !!"
 read -p                 "Press enter key to continue . . ."
 echo
else
 echo " !! Faild !!"
 read -p                 "Press enter key to continue . . ."
fi
}


#--------------------------
#converting to system image
#--------------------------
UnPck(){
sudo mkdir $PROJECT_DIR/convert-dat/out
printf '\e[8;33;80t]'
$permsda
clear
PAGE

echo "        -----> Unpacking Image."
echo
if [ -f $PROJECT_DIR/convert-dat/system.transfer.list ]
  then
  echo
  echo "      system.transfer.list [FOUND]"
fi
if [ -f $PROJECT_DIR/convert-dat/system.new.dat ]
  then
  echo
  echo "      system.new.dat [FOUND]"
  echo
  sleep 0.6;
  PAGE
echo "            Few seconds to go... :)"
( tools/./sdat2img.py convert-dat/system.transfer.list convert-dat/system.new.dat convert-dat/system.img ) >> Unpack.log
mkdir -p /$hm/$user/$dsk/$tul/convert-dat/out
mkdir -p /$hm/$user/$dsk/$tul/convert-dat/tmpsparse
mkdir -p /$hm/$user/$dsk/$tul/convert-dat/rom_system
mkdir -p Finish-new.dat
echo
echo "        -----> mounting system image..."
echo "                  Please Wait..."
echo
echo -e "  \033[0;31m "
sudo mount -t ext4 -o loop convert-dat/system.img convert-dat/out/
echo
echo -e "  \033[0;36m "
echo "        -----> setting permission for modifications..."
echo "                  Please Wait..."
sudo chown -R $user:$user $PROJECT_DIR/convert-dat/out
( sudo cp -avr  $PROJECT_DIR/convert-dat/out $PROJECT_DIR/convert-dat/rom_system ) >> x-log
sudo umount $PROJECT_DIR/convert-dat/out/
rm -rf $PROJECT_DIR/convert-dat/out
rm convert-dat/system.new.dat
rm convert-dat/system.transfer.list
echo
echo -e "        \033[0;36m "
echo "        -----> Please go to convert-dat/rom_system to edit your Rom "
echo
echo -e "                        \033[0;34m "
echo "============================="
echo "  !! PROCESS TIME !!      "
echo $[$SECONDS / 60]' minutes '$[$SECONDS % 60]' seconds'
echo "============================="
echo
echo
echo "                !! DONE !!"
echo
echo
else
    PAGE
    echo -e "  \033[0;31m "
    echo "WARNING!  WARNING!! WARNING!!!"
    echo "Please Check & Trace Where Errors."
    echo " There Is NO rom_system found"
    echo
    cecho R "" "system.new.dat -->> Missing !"
    echo
    cecho R "" "system.transfer.list -->> Missing !"
    echo
    echo
fi
read -p "Press enter key to continue . . ."
START " "
}


RePck(){
printf '\e[8;33;80t]'
PAGE
echo
#--------------------------
echo "        -----> Repacking Image."
echo
echo "            Few seconds to go... :)"
if [ -f $PROJECT_DIR/convert-dat/rom_system/build.prop ]
then
 api=$(grep "ro.build.version.sdk" convert-dat/rom_system/build.prop | cut -d"=" -f2);
 echo -e " \033[0;34m "
 echo "      Android SDK = $api "
 echo
fi
if [ -f $PROJECT_DIR/convert-dat/file_contexts ]
 then
 echo "      file_contexts [FOUND]"
 echo
 echo -e "        \033[0;36m "
 tools/./make_ext4fs -s -T -1 -S convert-dat/file_contexts -L system -l 2692743168 -a system convert-dat/system_new.img convert-dat/rom_system/
echo
clear
PAGE
echo "        -----> Converting To Sparse Image."
if [[ $api = "21" ]];
then
 argv="1"
elif [[ $api = "22" ]];
then
 argv="2"
elif [[ $api = "23" ]];
then
 argv="3"
elif [[ $api = "24" ]];
then
 argv="4"
elif [[ $api = "25" ]];
then
 argv="4"
fi
echo "            Please Wait..."
./tools/img2sdat.py convert-dat/system_new.img Finish-new.dat $argv; 
echo
echo "            Few seconds to go... :)"
echo
echo "            Thank You !!! :0"
rm $PROJECT_DIR/convert-dat/tmpsparse/system.*
rm $PROJECT_DIR/convert-dat/system.img
rm $PROJECT_DIR/convert-dat/system_new.img
echo
echo
if [ -f $PROJECT_DIR/Finish-new.dat/system.new.dat ]
then
 PAGE
 echo -e "        \033[0;36m "
 echo " PLEASE CHECK Finish-new.dat -Folder to see the following new files"
 echo
 echo "  ************************************"
 echo
 ls Finish-new.dat
 echo  ""
 echo -e "                        \033[0;34m "
 echo "============================="
 echo "          !! PROCESS TIME !!      "
 echo $[$SECONDS / 60]' minutes '$[$SECONDS % 60]' seconds'
 echo "============================="
 echo
 echo
 echo "                !! DONE !!"
 echo
 echo
 echo
fi
else
    PAGE
    echo -e "  \033[0;31m "
    echo "WARNING!  WARNING!! WARNING!!!"
    echo "Please Check & Trace Where Errors."
    echo " There Is NO rom_system found"
    echo
    cecho R "" "file_contexts -->> Missing !"
    echo
    cecho R "" "Android SDK -->> not detected !"
    echo
    read -p "Press enter key to continue . . ."
    echo
    PAGE
    echo "WARNING!  WARNING!! WARNING!!!"
    echo "Please Check & Trace Where Errors."
    echo
    echo
    cecho R "" "Repack ERRORS!"
    echo
    cecho R "" "Repack ERRORS!"
    echo
    cecho R "" "Repack ERRORS!"
    echo
    echo
fi
read -p                 "Press enter key to continue . . ."
START " "
}
#--------------------------

###-----------------------------------------
# ./sdat2img.py system.transfer.list system.new.dat system.img
# sudo mount -t ext4 -o loop system.img system/
# sudo chown -R iyke:iyke /home/iyke/Desktop/xe/tools/system
# ./make_ext4fs -T 0 -S file_contexts -l 1073741824 -a system system_new.img system/


#credit & info
V_CREDIT(){
  clear
  echo
  PAGE
  echo -e "                        \033[0;34m "
  cecho P " ""    \e[4m\e[7m@Xpirt [XDA]"
  echo "    for : sdat2img 1.0 - img2sdat 1.2"
  echo "    check thread link below  >>"
  cecho P"" "    \033[0;33m\e[3m\e[4m*https://forum.xda-developers.com/android/software-hacking/how-to-conver-lollipop-dat-files-to-t2978952"
  echo
  echo
  echo
  cecho P " " "\e[3m "
  read -p "         Press enter key for next . . ."
  clear
  echo
  PAGE
  echo -e "                        \033[0;34m "
  cecho P " ""    \e[4m\e[7m@SuperR. [XDA]"
  echo "    for : Some binaries"
  echo "    check profile link below  >>"
  cecho P"" "    \033[0;33m\e[3m\e[4m*https://forum.xda-developers.com/member.php?u=5787964"
  echo
  echo
  echo
  cecho P " " "\e[3m "
  read -p "         Press enter key for next . . ."
  clear
  echo
  PAGE
  echo -e "                        \033[0;34m "
  cecho P " ""    \e[4m\e[7m@Pom Kritsada [MTK THAI Developers]"
  echo "    for : file_context.bin conversion"
  echo "    check video link below  >>"
  cecho P"" "    \033[0;33m\e[3m\e[4m*https://www.youtube.com/watch?v=Tw5f4iLUYhc"
  echo
  echo
  echo
  cecho P " " "\e[3m "
  read -p "         Press enter key to continue . . ."
  clear
  echo
  PAGE
  echo -e "                        \033[0;34m "
  cecho P " ""    \e[4m\e[7m#AMD [FACEBOOK]"
  echo "    Android Matrix Development"
  echo "    check group link below  >>"
  cecho P"" "    \033[0;33m\e[3m\e[4m*https://web.facebook.com/groups/1024872487548231/"
  echo
  echo
  echo
  cecho P " " "\e[3m "
  read -p "         Press enter key for next . . ."
    clear
  echo
  PAGE
  echo -e "                        \033[0;34m "
  cecho P " ""    \e[4m\e[7mNana Yaa [Jennie]"
  echo "    for her time & motivation"
  echo
  echo
  echo
  cecho P " " "\e[3m "
  read -p "         Press enter key for next . . ."
  echo
  clear
  echo
  PAGE
  echo -e "                        \033[0;33m "
  echo -e "             \e[3m[ THANKS FOR VIEWING ]"
  echo -e "             \e[3m[  OUR CREDIT !!! ]"
  echo
  sleep 2.0;
  START
}

#WAIT
RELAX(){
 PAGE
echo  ""
echo -e "                        \033[0;34m "
echo "============================="
echo "          !! PROCESS TIME !!      "
echo $[$SECONDS / 60]' minutes '$[$SECONDS % 60]' seconds'
echo "============================="
echo
echo
echo "                !! DONE !!"
echo
echo
read -p "Press enter key to continue . . ."
echo
HOME " "
}

#EXIT
EXIT(){
printf '\e[8;33;80t]'
clear
 cecho C "" "Talent Is Nothing WIthout Ethics!!!"
 sleep 1.0;
 clear
exit
}

#COLOR
#Credit goes to @Matrix for his color codes
#USAGE: cecho TYPE=R|G|Y|B|P|C|W "msg1" "color_msg2" "msg3"
cecho ()
   {
   #Case didn't work out for me in cygwin
   if [ "$1" == "R" ]
then
  echo -e "$2""\033[0;91m$3\033[0m""$4" # Red
elif  [ "$1" == "G" ]
then
  echo -e "$2""\033[0;92m$3\033[0m""$4" # Green
elif  [ "$1" == "Y" ]
then
  echo -e "$2""\033[0;93m$3\033[0m""$4" # Yellow
elif  [ "$1" == "B" ]
then
  echo -e "$2""\033[0;94m$3\033[0m""$4" # Blue
elif  [ "$1" == "P" ]
then
  echo -e "$2""\033[0;95m$3\033[0m""$4" # Purple
elif  [ "$1" == "C" ]
then
  echo -e "$2""\033[0;96m$3\033[0m""$4" # Cyan
elif  [ "$1" == "W" ]
then
  echo -e "$2""\033[0;97m$3\033[0m""$4" # White
 fi
}

#EXTRA_COLOR_OPTIONS
blue='\033[0;34m'
cyan='\033[0;36m'
yellow='\033[0;33m'
red='\033[0;31m'
nocol='\033[0m'


#DEPLOYING function
EXT_MAIN
