#!/bin/bash
# Info: Script to install podman
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-11-2023

USAGE="install-podman-os.sh "
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1
#ORG=$(pwd)
_update_podman() {
    local has_podman
    local curr_version
    has_podman=$(type podman 2>/dev/null)
    if [ -n "$has_podman" ] ; then 
      curr_version=$(podman version | grep "^Version" | cut -f2 -d":" | sed "s/ //g")
    fi
    [ "$PODMAN_VERSION" == "$curr_version" ] && return 
    echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections
    DEBIAN_FRONTEND=noninteractive sudo apt-get update
    DEBIAN_FRONTEND=noninteractive sudo apt-get upgrade -y
    DEBIAN_FRONTEND=noninteractive sudo apt-get -y -qq install python3 python3-pip 
    DEBIAN_FRONTEND=noninteractive sudo apt-get -y -qq install podman  podman-compose

    DEBIAN_FRONTEND=noninteractive sudo apt autoremove -y
}

_config_sysctl() {
  sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
  has_nolocal_bint=$(sudo grep "net.ipv4.ip_nonlocal_bind = 1" /etc/sysctl.conf)
  if [ -z "$has_nolocal_bind" ] ; then
    echo "net.ipv4.ip_nonlocal_bind = 1" | sudo tee -a /etc/sysctl.conf >>$cmd_out
    echo "net.ipv4.ip_unprivileged_port_start=25" | sudo tee -a /etc/sysctl.conf >>$cmd_out
    #echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee -a /etc/sysctl.conf
    sudo modprobe br_netfilter
    echo 1 | sudo tee -a /proc/sys/net/bridge/bridge-nf-call-iptables >>$cmd_out
  fi
  sudo sysctl -p >>$cmd_out
  return 0
}

_config_podman() {
  if [ -r "libpod.conf" ] && [ -d "/etc/containers" ] ; then
    sudo cp libpod.conf /etc/containers
  fi
  _config_sysctl
}

[ -r "./env-podman"  ] && . ./env-podman

# Update and add packages to installation
[ -z "$1" ] || [ "$1" == "podman" ] && _update_podman
_config_podman
