#!/bin/bash

# VARIABLES
VAR_OS=`uname`;
VAR_TMP="none";
VAR_PATH="/var/tmp";
VAR_NODE_TYPE=$(</etc/abbeoo/abo.system.nodetype.cfg);

VAR_FILE_HEADER="# ABBEOO MODIFIED AT `date '+ %Y-%m-%d %H:%M:%S'`\n#";

VAR_PATH_ABO="/etc/abbeoo";
VAR_PATH_SOUL="http://soul.abbeoo.net";
VAR_PATH_INSTALL="/pub/install/linux/debian/buster";
VAR_PATH_UPDATE="/pub/update/linux/debian/buster";
VAR_PATH_SOURCE="/pub/source/linux";
VAR_PATH_BIN="/pub/bin/linux/debian/buster";
VAR_PATH_DIR_STA="/usr/src";
VAR_PATH_DIR_TMP="/var/tmp";
VAR_PATH_DIR_LOG="/var/log";

VAR_FILE_LOG=$VAR_PATH_DIR_LOG"/abo.install.log";

CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN);



function abo_mkabobasics {
  if [ ! -d "$1" ]; then
    VAR_NODE_TYPE=$1;
  fi
  abo_mkdir "$VAR_PATH_ABO";

  abo_mkfile.empty "$VAR_PATH_ABO/abo.system.nodetype.cfg";
  echo "$VAR_NODE_TYPE">>$VAR_PATH;

  abo_mkfile.empty "$VAR_PATH_ABO/abo.system.updateseq.cfg";
  echo "0">>$VAR_PATH;

  abo_mkfile.bash "$VAR_PATH_ABO/abo.system.update.sh";
  echo "abo_update.system;">>$VAR_PATH;
  echo "">>$VAR_PATH;
  echo "exit 0;">>$VAR_PATH;
  rm -f /usr/bin/abo.system.update.sh;
  ln -s $VAR_PATH /usr/bin/abo.system.update.sh;
}

function abo_mkabosoulfile {
  if [ ! -d "$VAR_PATH_ABO" ]; then
    mkdir  $VAR_PATH_ABO;
    chmod -fR 755 $VAR_PATH_ABO;
  fi
  if [ -f "$VAR_PATH_ABO/abo.global.parameters.sh" ]; then
    rm -f "$VAR_PATH_ABO/abo.global.parameters.sh";
  fi
  wget --continue $VAR_PATH_SOUL$VAR_PATH_INSTALL/abo.global.parameters.sh -O $VAR_PATH_ABO/abo.global.parameters.sh;
  chmod +x $VAR_PATH_ABO/abo.global.parameters.sh;
}

function abo_install.sw {
  if [ -n "$VAR_PATH_DIR_TMP/$1" ]; then
    rm -f $VAR_PATH_DIR_TMP/$1;
  fi
  wget --continue $VAR_PATH_SOUL$VAR_PATH_INSTALL/$1 -O $VAR_PATH_DIR_TMP/$1;
  chmod +x $VAR_PATH_DIR_TMP/$1;
  $VAR_PATH_DIR_TMP/$1;
  rm -f $VAR_PATH_DIR_TMP/$1;
  abo_writelog "File ["$1"] downloaded and executed sucessfully.";
}

function abo_update.system {
  abo_download.file.bash.execute $VAR_PATH_SOUL$VAR_PATH_UPDATE/"abo.update.$VAR_NODE_TYPE.sh" "$VAR_PATH_DIR_TMP";
  abo_writelog "System has sucessfully updated.";
}

function abo_install.kernel {
  if [ -n "$VAR_PATH_DIR_TMP/$1" ]; then
    rm -f $VAR_PATH_DIR_TMP/$1;
  fi
  wget --continue $VAR_PATH_SOUL$VAR_PATH_BIN/kernel/$1 -O $VAR_PATH_DIR_TMP/$1;
  dpkg -i $VAR_PATH_DIR_TMP/$1;
  rm -f $VAR_PATH_DIR_TMP/$1;
  abo_writelog "Kernel ["$1"] downloaded and installed sucessfully.";
}

function abo_uninstall.kernel {
  abo_system.aptitude.purge $1;
  update-grub2;
  abo_writelog "Kernel ["$1"] uninstalled sucessfully.";
}

function abo_system.aptitude {
  DEBIAN_FRONTEND=noninteractive aptitude -y update;
  DEBIAN_FRONTEND=noninteractive aptitude -y upgrade;
  if [ -n "$1" ]; then
    DEBIAN_FRONTEND=noninteractive aptitude -y install $1
  fi
  if [ -n "$2" ]; then
    DEBIAN_FRONTEND=noninteractive aptitude -y purge $2
  fi
  DEBIAN_FRONTEND=noninteractive aptitude -y clean;
  DEBIAN_FRONTEND=noninteractive apt-get clean;
  abo_writelog "System aptitude done: Installed: $1; Purged: $2";
}

function abo_system.aptitude.purge {
  DEBIAN_FRONTEND=noninteractive aptitude -y update;
  DEBIAN_FRONTEND=noninteractive aptitude -y upgrade;
  if [ -n "$1" ]; then
    DEBIAN_FRONTEND=noninteractive aptitude -y purge $1
  fi
  DEBIAN_FRONTEND=noninteractive aptitude -y clean;
  DEBIAN_FRONTEND=noninteractive apt-get clean;
  abo_writelog "System aptitude done: Purged: $1";
}

function abo_system.aptitude.update {
  DEBIAN_FRONTEND=noninteractive aptitude -y update;
  DEBIAN_FRONTEND=noninteractive aptitude -y upgrade;
  DEBIAN_FRONTEND=noninteractive aptitude -y clean;
  DEBIAN_FRONTEND=noninteractive apt-get clean;
  abo_writelog "System aptitude done: Updated: Yes ...";
}




function abo_mkdir {
  if [ ! -d "$1" ]; then
    mkdir  $1;
  fi
  if [ -n "$2" ]; then
    chmod -fR $2 $1;
  fi
  if [ -n "$3" ]; then
    chown -fR $3:$3 $1;
  fi
  abo_writelog "Directory ["$1"]:"$2" created.";
}

function abo_rmdir {
  if [ ! -d "$1" ]; then
    rm -fR $1;
  fi
  abo_writelog "Directory ["$1"] sucessfully removed.";
}

function abo_mkfile {
  VAR_PATH=$1;
  if [ -n "$2" ]; then
    mv -f $1 $1.$2;
  fi
  rm -f $1;
  touch $1;
  echo -e $VAR_FILE_HEADER>$1;
  abo_writelog "File ["$1"] created.";
}


function abo_mkfile.bash {
  VAR_PATH=$1;
  if [ -n "$2" ]; then
    mv -f $1 $1.$2;
  fi
  rm -f $1;
  touch $1;
  echo "#!/bin/bash">$1;
  echo "">>$1;
  echo -e $VAR_FILE_HEADER>>$1;
  echo "">>$1;
  echo "if [ ! -f \"$VAR_PATH_ABO/abo.global.parameters.sh\" ]; then">>$1;
  echo "  exit 0;">>$1;
  echo "fi">>$1;
  echo "source \"$VAR_PATH_ABO/abo.global.parameters.sh\";">>$1;
  echo "">>$1;
  chmod +x $1;
  abo_writelog "Bash file ["$1"] created.";
}


function abo_mkfile.empty {
  VAR_PATH=$1;
  if [ -n "$2" ]; then
    mv -f $1 $1.$2;
  fi
  rm -f $1;
  touch $1;
  abo_writelog "Empty file ["$1"] created.";
}


function abo_mkfile.append {
  VAR_PATH=$1;
  if [ -n "$2" ]; then
    mv -f $1 $1.$2;
  fi
  abo_writelog "File ["$1"] prepared for appending.";
}


function abo_download.file {
  VAR_PATH_URL=$1;
  if [ -n "$2" ]; then
    cd $2;
  fi
  if [ -f ${VAR_PATH_URL##*/} ]; then
    rm -f ${VAR_PATH_URL##*/};
  fi
  wget --continue $1;
  abo_writelog "Download of file ["${VAR_PATH_URL##*/}"] completed.";
}


function abo_download.file.bash {
  VAR_PATH_URL=$1;
  if [ -n "$2" ]; then
    cd $2;
  fi
  if [ -f ${VAR_PATH_URL##*/} ]; then
    rm -f ${VAR_PATH_URL##*/};
  fi
  wget --continue $1;
  chmod +x ${VAR_PATH_URL##*/};
  abo_writelog "Download of bash file ["${VAR_PATH_URL##*/}"] completed.";
}


function abo_download.file.bash.execute {
  VAR_PATH_URL=$1;
  if [ -n "$2" ]; then
    cd $2;
  fi
  if [ ! -f ${VAR_PATH_URL##*/} ]; then
    rm -f ${VAR_PATH_URL##*/};
  fi
  wget --continue $1;
  chmod +x ${VAR_PATH_URL##*/};
  ./${VAR_PATH_URL##*/};
  rm -f ${VAR_PATH_URL##*/};
  abo_writelog "Download and executed of bash file ["${VAR_PATH_URL##*/}"] completed.";
}


function abo_writelog {
  echo $1;
  echo "`date '+ %Y-%m-%d %H:%M:%S'`: "$1>>$VAR_FILE_LOG;
}

function abo_changestringinfile {
  mv $3 $3.old;
  sed "s/$1/$2/g" $3.old > $3;
  rm -f $3.old;
}
