#https://github.com/Neo-Oli/termux-ubuntu References # EMPIRESSERVERHEADER
echo PARAM DEBUG ${currdir} ${currentexecfile}
if [ -z ${currdir} ] ;then
export currdir=$(pwd)
fi
#This prevent new instances to overwrite current directory causes multiple problems
if [ -z ${currentexecfile} ] ;then
export currentexecfile=${0}
fi


distroDetection(){
# DISTRO MANAGER DETECTION IS ALSO USED TO DETERMINE WHERE DOES THE SCRIPT RUN
export ptracecompat='1' #flag ptrace compatibility based on the distro or kernel that its running
export linktosymlinkActivated=0
export foreignenvironment="0" # flag foreign environment such as freebsd darwin or other nix system
export arch=$(uname -m)
if [ ! -z $(which apk) ]; then
  echo "Alpine Distro"
  export packmanager=apk
  export installParameter="add"
  export ptracecompat='1'
  export distro="alpine"
fi

if [ ! -z $(which apt) ]; then
export packmanager="apt"
export installParameter="install -y"
export distro="debianGNU"
fi
    if [ ! -z $(which pkg) ]; then
    export packmanager=pkg
    export installParameter="install -y"
    export linktosymlinkActivated=1
    echo Termux Detected
    export distro="termux"

    fi


    if [ ! -z $(which yum) ]; then
    export packmanager="yum"
    export installParameter="install -y"
    export distro="centOSGNU"
    fi

    if [[ $(grep Microsoft /proc/version) ]]; then
      export ptracecompat='0'
      export WSLenv="1"
    fi

    if [[ $( uname -a | grep darwin ) ]]; then
      echo "Bash is running on Darwin Kernel"
      echo "Running Experimental mode"
      yellow
      echo "${pbadge} Running environment setup" > /dev/tty
      if [ -z $(which brew) ]; then
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
      fi
      export distro="darwinkrnl"
      export packmanager="brew"
      export foreignenvironment="1"
      export installParameter="install -y"
      export ptracecompat='0'
      export WSLenv="0"
    fi

    if [[ $(grep iSH /proc/version) ]]; then
      echo "Bash is running on iSH"
      echo "PTRACE ON iSH IS NOT SUPPORTED"
      export ptracecompat='0'
      export WSLenv="0"
    fi

    if [ ! -z $(which pacman) ]; then
    export packmanager="pacman"
    export installParameter="-Sy --noconfirm"
    export distro="archlinux"
    echo Redownloading Databases
    pacman -Syy
    fi

    if [ ! -z $(which easy_install) ]; then
    export pyPacman="easy_install"
    export pyInstallParam=""
    fi

    if [ ! -z $(which add-apt-repository) ]; then
    export packmanager="apt-get"
    export installParameter="install -y"
    fi

    if [ ! -z $(which pip) ]; then
    export pyPacman="pip"
    export pyInstallParam="install"
    fi

# Sourced from unifyServer technology
}

pkginstall(){
distroDetection
export installerprefix="[ModularInstaller]"
for a in ${1}; do
  echo "${installerprefix} Checking ${a}"
  su -c "${packmanager} ${installParameter} ${a}" >> packageManager.log  < /dev/null 2>&1
  su -c "${pyPacman} ${pyInstallParam} ${a}" >> packageManager.log  < /dev/null 2>&1
done

}
if [ $(whoami) != "root" ]; then echo Please run this as root; exit; fi
##############################
# Install dependencies
pkginstall "python python-setuptools wget proot busybox dialog which tar sed python3 python3.7 python3.8 subversion python3-setuptools pip"; ln -s /usr/bin/busybox /usr/bin/which
##############################
which(){
busybox which $1
}
export installTarget=/usr/bin
empiresServer="${installTarget}/empires-server"
cat > "${empiresServer}" <<- EOM
#!/bin/sh
bash ${currdir}/${0}
EOM
chmod 777 "${empiresServer}"




########################################################## Install Logic ############################################33

#if [ $(whoami) != root ]; then
#echo Please re run this compile script with root
#echo Thank you
#exit
#fi
if [ ! -d empiresservercontainer ] || [ $(cat empiresservercontainer/buildnumber) -lt 6969696 ] ; then
lineno=190 #This line is the position of the md5hash stored #Please Change this Number to successfully compiles 21 from last line if the last line is 188 then put it 190
initorigindir=$(pwd)
integrity=$(sed -n "${lineno}p" "$0") #https://stackoverflow.com/questions/21651310/reading-a-particular-line-from-a-file-in-bash-script-using-variable-as-line-numb
echo "${integrity}" > hash
echo Preparing package empires-server
echo Pulling Package from the script
if [ -z $(which busybox) ]; then echo no busybox detected toybox unsupported ; exit ;fi
echo 'Preparing For installation'
tail -n +191 $0 > empires-RNT #Please Change this Number to successfully compiles +3 from last line 188 then put it 191
if md5sum -c hash ; then
echo Archive Integrity Verified
rm hash
else
echo It is altered or ruined thus installation is aborted
rm hash
exit
fi
tar -xf empires-RNT
rm -rf empires-RNT ; echo Installation Successfull


fi
#####################################################################################################################


#FILESHRINKING LOGIC#########################################

echo "#!/bin/sh" > ${0}
echo "cd ${currdir}/empiresservercontainer" >> ${0}
echo "bash empires-server-routines.sh" >> ${0}


#############################################################


########## Handing over control ##################

echo Starting EALServer
echo Entering Directory and handing over control

cd ${currdir}/empiresservercontainer
bash empires-server-routines.sh

##################################################

#FILESHRINKING LOGIC CLEANING#########################################
rm -rf ${currdir}/*_minimized
#delete temporary file
##################################################################
exit
echo "The program has Reached out of the boundary of execution" #starting boundaries
