if [ -f ~/.bash_aliases_priv ]; then
    . ~/.bash_aliases_priv
fi
alias ssh-host-key='ssh-keyscan localhost | ssh-keygen -lf -'
alias weston-phone="weston --width=1920 --height=1056 -S /run/user/1000/wayland-1"
alias waydroid-start="waydroid session start"
alias waydroid-show="waydroid show-full-ui"
alias waydroid-stop="waydroid session stop"
alias waydroid-launch="weston-phone && waydroid-start & read && waydroid-stop"
alias show-waydroid="waydroid-show"
alias swd="waydroid-show"
alias launch-waydroid="waydroid-launch"
alias screen-off="sleep 0.1 && xset dpms force off"
alias boot-sign-verify="sudo sign-boot && sudo verify-boot"
alias rdp-session-desktop-cvd="xfreerdp3 ~/Downloads/SessionDesktop.rdpw /gateway:type:arm /sec:aad +clipboard /w:1920 /h:1000 +auto-reconnect"
alias pulse-restart="systemctl --user stop pulseaudio.socket pulseaudio.service && sleep 1 && systemctl --user start pulseaudio.socket pulseaudio.service"
function conda-activate() {
  eval "$(~/anaconda3/bin/conda shell.bash hook)";
}
function kernel-build-dep() {
  echo "[+] Kernel Build Dependancy Installation...";
  sudo apt install gawk git dh-make llvm libclang-dev sbsigntool linux-libc-dev libperl-dev libpython3-dev libunwind-dev liblzma-dev lzma-dev libzstd-dev libnuma-dev libelf-dev libcap-dev linux-libc-dev;
  sudo apt install zlib1g-dev libclang1 zlib1g libbpf0 libzstd1 libnuma1 libelf1;
  sudo apt install libpython3.10 libunwind8 liblzma5 libcap-ng0 libcap2;
  sudo apt build-dep linux-image-unsigned-$(uname -r);
}
function kernel-build() {
  echo "[+] Kernel Build Starting...";
  cwd=$(pwd);
  mkdir -p ~/kernel;
  cd ~/kernel;
  doNotContinueKernelBuild=0;
  if [ -f kernel-build-occuring ]; then
    echo "[-] Kernel Build Occuring Already!";
    doNotContinueKernelBuild=1;
  else
    touch kernel-build-occuring;
  fi;
  if [ $doNotContinueKernelBuild -eq 0 ]; then
    if [ -f last-build-failed ]; then
      echo "[*] Resuming last build; delete last-build-failed in the kernel directory before starting a kernel build to re-obtain sources.";
      rm -f last-build-failed;
      for _dir in *"linux"*; do
        [ -d "${_dir}" ] && linuxdir="${_dir}" && break;
      done;
      cd "$linuxdir";
    else
      for _dir in *"linux"*; do
        [ -d "${_dir}" ] && linuxdir="${_dir}" && break;
      done;
      echo "[-] Removing Sources...";
      rm -rf "$linuxdir";
      sudo rm -rf deb-contents;
      rm -f *.gz;
      rm -f *.dsc;
      echo "[*] Archiving old packages...";
      mkdir -p old-debs;
      touch dummy.deb;
      mv *.deb old-debs/;
      echo "[+] Obtaining Sources...";
      apt-get source linux-image-unsigned-$(uname -r);
      if [ $? -eq 0 ]; then
        for _dir in *"linux"*; do
          [ -d "${_dir}" ] && linuxdir="${_dir}" && break;
        done;
        cd "$linuxdir";
        echo "[*] Patching source and configuration...";
        sed -i "s/.*CONFIG_MODULE_SIG_FORCE.*/CONFIG_MODULE_SIG_FORCE                         policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'ppc64el': 'y', 's390x': 'y'}>/" debian.master/config/annotations;
        if [ -f certs/system_certificates.S ]; then
          sed -i 's@.*\.incbin "certs/signing_key\.x509".*@	\.incbin "certs/signing_key\.der"@' certs/system_certificates.S;
        fi;
        git apply --verbose ~/Downloads/patches/hibernate/0001-Hibernate-Patch.patch;
        echo "[*] Cleaning kernel build...";
        chmod a+x debian/rules;
        chmod a+x debian/scripts/*;
        chmod a+x debian/scripts/misc/*;
        fakeroot debian/rules clean;
        mkdir -p debian/build/build-generic/certs;
        sudo cp /cert/mok/signing_key.pem ../ -f;
        sudo chmod u=rw,g=rw,o=rw ../signing_key.pem;
        sudo cp /var/lib/shim-signed/mok/MOK.der ../signing_key.x509 -f;
        sudo chmod u=rw,g=rw,o=rw ../signing_key.x509;
        cp ../signing_key.x509 debian/build/build-generic/certs/signing_key.der -f;
        (((until [ -f debian/build/build-generic/certs/signing_key.pem ]; do sleep 0.1; done) && while lsof debian/build/build-generic/certs/signing_key.pem; do sleep 0.1; done) && mv -f ../signing_key.pem debian/build/build-generic/certs/signing_key.pem) &
        (((until [ -f debian/build/build-generic/certs/signing_key.x509 ]; do sleep 0.1; done) && while lsof debian/build/build-generic/certs/signing_key.x509; do sleep 0.1; done) && mv -f ../signing_key.x509 debian/build/build-generic/certs/signing_key.x509) &
      else
        echo "[-] Failed to obtain sources!";
        doNotContinueKernelBuild=1;
        rm -f kernel-build-occuring;
      fi;
    fi;
  fi;
  if [ $doNotContinueKernelBuild -eq 0 ]; then
    echo "[*] Building kernel...";
    fakeroot debian/rules binary;
    if [ $? -eq 0 ]; then
      echo "[*] Build Complete!";
      rm -f debian/build/build-generic/certs/signing_key.pem;
      rm -f ../signing_key.pem;
      rm -f ../signing_key.x509;
      echo "[+] Extracting kernel package...";
      cd ~/kernel;
      mkdir -p deb-contents;
      dpkg-deb -R $(ls linux-image-unsigned-* | head -1)  deb-contents;
      cd "$linuxdir";
      echo "[*] Making kernel signed...";
      pkgarch=$(dpkg-architecture -qDEB_HOST_ARCH);
      pkgver=$(dpkg-parsechangelog --show-field Version);
      cd ~/kernel/deb-contents;
      pkgunom=$(head -1 DEBIAN/control | sed -e 's/Package: //');
      find ./ -type f -exec sed -i -e 's/-unsigned//g' {} \;
      find ./ -type f -exec sed -i -e 's/ unsigned//g' {} \;
      pkgnom=$(head -1 DEBIAN/control | sed -e 's/Package: //');
      sudo mv usr/share/doc/$pkgunom usr/share/doc/$pkgnom;
      tlinuz=$(ls boot/vmlinuz-* | head -1);
      sudo sbsign --key /cert/BMOK.priv --cert /cert/BMOK.pem $tlinuz --output $tlinuz;
      sed -i "s/.*Conflicts: .*/Conflicts: $pkgunom/" DEBIAN/control;
      pkgisz=$(du -ks * | grep -v DEBIAN | cut -f1 | xargs | sed -e 's/\ /+/g' | bc);
      find ./ -path '*/DEBIAN' -prune -o -type f -exec md5sum {} \; | awk '{ print $1 "  " substr($2, 10) }' > DEBIAN/md5sums;
      sed -i "s/.*Installed-Size: .*/Installed-Size: $pkgisz/" DEBIAN/control;
      echo "[+] Packing signed kernel...";
      cd ~/kernel;
      dpkg-deb -b deb-contents ${pkgnom}_${pkgver}_${pkgarch}.deb;
      sudo rm -rf deb-contents;
      echo "[+] Deploying Packages Locally...";
      sudo cp ${pkgnom}_${pkgver}_${pkgarch}.deb /usr/local/mydebs/;
      sudo cp linux-headers-* /usr/local/mydebs/;
      sudo cp linux-libc-dev* /usr/local/mydebs/;
      sudo cp linux-modules* /usr/local/mydebs/;
      sudo cp linux-lib-rust* /usr/local/mydebs/;
      sudo update-mydebs;
      rm -f kernel-build-occuring;
      cd "$cwd";
      echo "[+] Kernel Build Finished!";
    else
      echo "[*] Build Failed!";
      cd ~/kernel;
      touch last-build-failed;
      rm -f kernel-build-occuring;
      cd "$cwd";
    fi;
  fi;
}
function install-tar() {
  if [ $# -gt 0 ]; then
    if [ $# -eq 1 ]; then
      fln=$(basename -- "$1");
      flp=$1;
    else
      fln=$(basename -- "$2");
      flp=$2;
    fi
    fln="${fln%%.*}";
    echo "Installing: /opt/$fln";
    sudo mkdir -p "/opt/$fln";
    sudo tar -xvf "$flp" -C /opt/$fln;
    if [ $# -gt 1 ]; then
      sudo chown -R "$1" /opt/$fln;
    fi
  else
    echo "Usage:";
    echo "install-tar <archive> | <owner> <archive>";
  fi
}
function install-tar-contents() {
  if [ $# -gt 0 ]; then
    if [ $# -eq 1 ]; then
      flp=$1;
    else
      flp=$2;
    fi
    echo "Installing: /opt";
    sudo tar -xvf "$flp" -C /opt;
    if [ $# -gt 1 ]; then
      sudo chown -R "$1" /opt;
    fi
  else
    echo "Usage:";
    echo "install-tar-contents <archive> | <owner> <archive>";
  fi
}
function edit-dot-desktop() {
  sudo nano "/usr/share/applications/$(basename -- "$1").desktop";
}
function edit-dot-desktop-local() {
  nano ~"/.local/share/applications/$(basename -- "$1").desktop";
}
function tpm2-contents() {
  sudo tpm2_selftest
  sudo tpm2_gettestresult
  sudo tpm2_getcap -l
  sudo tmp2_pcrread
  sudo tpm2_pcrread
}
alias list-xinput-devices="xinput list"
function disable-xinput-device() {
  if [ -z $1 ]; then
    echo "Provide a device name fragment.";
  else
    cdid=$(xinput list | grep "$1" | head -1 | grep -Po 'id=\K[^\t]*' | head -1);
    echo "Disabling: $cdid";
    xinput disable $cdid;
  fi;
}
function enable-xinput-device() {
  if [ -z $1 ]; then
    echo "Provide a device name fragment.";
  else
    cdid=$(xinput list | grep "$1" | head -1 | grep -Po 'id=\K[^\t]*' | head -1);
    echo "Enabling: $cdid";
    xinput enable $cdid;
  fi;
}
function record-screen-x11() {
  cwd=$(pwd);
  cd ~/Videos;
  mkdir -p ScreenRecordings;
  cd ScreenRecordings;
  srecfname="recording_"$(date '+%Y-%m-%d_%H-%M-%S')".mkv";
  if [ $# -gt 0 ]; then
    srecintv="$1";
    if [ $# -gt 1 ]; then
      srecfname="$2";
    fi
  else
    srecintv='1';
  fi
  echo "Recording x11 screen to ${srecfname} every ${srecintv}s at 30 fps.";
  ffmpeg -framerate "$srecintv" -f x11grab -s 1920,1080 -i :0.0+0,0 -vf settb=\(1/30\),setpts=N/TB/30 -r 30 -vcodec libx264 -crf 0 -preset ultrafast -threads 0 "$srecfname";
  cd "$cwd";
}
function search-in-files() {
  grep -Rn "$1" -e "$2";
}
function search-word-in-files() {
  grep -Rnw "$1" -e "$2";
}
function apt-search() {
  apt list | grep "$1";
}
function apt-search-installed() {
  apt list --installed | grep "$1";
}
function attach-raw-img() {
  sudo udisksctl loop-setup -f "$1";
}
function attach-raw-img-ro() {
  sudo udisksctl loop-setup -f "$1" -r;
}
function dettach-raw-img() {
  sudo udisksctl loop-delete -b "$1";
}