#!/bin/bash
#SBATCH --job-name drivaer
#SBATCH --partition=hb120v3
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=64

np=$SLURM_NTASKS
n=$SLURM_NNODES
ppn=$SLURM_CPUS_ON_NODE

# setup openfoam environment (from eessi)
source /cvmfs/pilot.eessi-hpc.org/versions/2021.12/init/bash
ml OpenFOAM/9-foss-2021a
source $FOAM_BASH

# select the tutorial to run (and give any args required for allrun)
tutorial_path=incompressible/simpleFoam/drivaerFastback
allrun_args="-c $np -m M"

# the location to run the copy of the tutorial
local_path=$HOME/openfoam_tutorial_runs
mkdir -p $local_path

# create the case name - add size and date
casedir=$local_path/$(basename $tutorial_path)_${n}x${ppn}_$(date +%Y%m%d_%H%M%S)

cp -r $FOAM_TUTORIALS/$tutorial_path $casedir

pushd $casedir
# allow flags to be added to the mpirun command through FOAM_MPIRUN_FLAGS environment variable
sed -i '/RunFunctions/a source <(declare -f runParallel | sed "s/mpirun/SLURM_EXPORT_ENV=ALL mpirun \\\$FOAM_MPIRUN_FLAGS/g")' Allrun
# change the script to bash (as we are using bash features not necessarily available for just a sh session)
sed -i 's#/bin/sh#/bin/bash#g' Allrun

export FOAM_MPIRUN_FLAGS="-mca pml ucx $(env |grep 'WM_\|FOAM_' | cut -d'=' -f1 | sed 's/^/-x /g' | tr '\n' ' ') -x MPI_BUFFER_SIZE -x UCX_IB_MLX5_DEVX=n -x UCX_POSIX_USE_PROC_LINK=n -x PATH -x LD_LIBRARY_PATH"

# Decompress the geometry
gunzip constant/geometry/*
mv constant/geometry constant/triSurface
# Reconstruct the single partition after the solver has run
sed -i 's/# runApplication reconstructPar/runApplication reconstructPar/g' Allrun

./Allrun $allrun_args

# Create a file to load the model in Paraview
touch case.foam


#!/bin/bash
#SBATCH -J PingPong
#SBATCH -N 2
#SBATCH --ntasks-per-node=1
#SBATCH -p hc44rs
#SBATCH -t 5
#SBATCH --export=NONE

source /etc/profile.d/modules.sh
module use /usr/share/Modules/modulefiles

module load mpi/impi-2021

echo "Running on $SLURM_NNODES nodes"
mpirun -ppn 1 IMB-MPI1 PingPong


#!/bin/bash
#SBATCH -J RingPingPong
#SBATCH -N 4
#SBATCH --ntasks-per-node=1
#SBATCH -p hc44rs
#SBATCH -t 5
#SBATCH --export=NONE

TYPE=latency

JOBID=$SLURM_JOBID

if [ $TYPE == "latency" ]; then
   MSGLOG="9:10"
elif [ $TYPE == "bandwidth" ]; then
   MSGLOG="22:24"
else
   echo "Error: Do not recognize MSGLOG option: $MSGLOG"
   exit 1
fi

source /etc/profile.d/modules.sh
module use /usr/share/Modules/modulefiles

module load mpi/impi-2021

#export I_MPI_FABRICS="shm:ofi"
export I_MPI_DEBUG=5
#export FI_PROVIDER=mlx

IMB_ROOT=$I_MPI_ROOT/bin

mpi_options="-np 2 -ppn 1"
host_option="-hosts"

# affinity
numactl_options=" numactl --cpunodebind 0"

hostlist=$(pwd)/hosts.$JOBID
scontrol show hostnames | sort -u > $hostlist

src=$(tail -n 1 $hostlist)
# -msglog 9:10 is for 512 and 1024 bytes message size only
for dst in $(<$hostlist); do
    mpirun $host_option $src,$dst \
        $mpi_options $numactl_options \
        $IMB_ROOT/IMB-MPI1 PingPong -msglog $MSGLOG > ${src}_to_${dst}_ringpingpong.$JOBID.log
    src=$dst
done

# clean up
rm $hostlist

if [ $TYPE == "latency" ]; then
echo "Ring Ping Pong Results (1024 bytes)"
printf "%-20s %-20s %10s\n" "Source" "Destination" "Time [usec]"
grep "^         1024 " *_ringpingpong.$JOBID.log \
    | tr -s ' ' | cut -d ' ' -f 1,4 \
    | sed 's/_to_/ /g;s/_ringpingpong[^:]*://g' \
    | sort -nk 3 \
    | xargs printf "%-20s %-20s %10s\n" | tee output.log
else
echo "Ring Ping Pong Bandwidth Results (16777216 bytes)"
printf "%-20s %-20s %10s\n" "Source" "Destination" "Mbytes/sec"
grep "^ *16777216 " *_ringpingpong.$JOBID.log \
    | tr -s ' ' | cut -d ' ' -f 1,5 \
    | sed 's/_to_/ /g;s/_ringpingpong[^:]*://g' \
    | sort -nk 3 \
    | xargs printf "%-20s %-20s %10s\n" | tee bandwidth_output.log
fi


#!/bin/bash
# Library of functions to be used across scripts
read_os()
{
    os_release=$(cat /etc/os-release | grep "^ID\=" | cut -d'=' -f 2 | xargs)
    os_maj_ver=$(cat /etc/os-release | grep "^VERSION_ID\=" | cut -d'=' -f 2 | xargs)
    full_version=$(cat /etc/os-release | grep "^VERSION\=" | cut -d'=' -f 2 | xargs)
}

is_centos()
{
    read_os
    if [ "$os_release" == "centos" ]; then
        return 0
    else
        return 1
    fi
}

is_centos8()
{
    read_os
    if [ "$os_release" == "centos" ] && [ "$os_maj_ver" == "8" ]; then
        return 0
    else
        return 1
    fi
}

is_centos7()
{
    read_os
    if [ "$os_release" == "centos" ] && [ "$os_maj_ver" == "7" ]; then
        return 0
    else
        return 1
    fi
}

is_ubuntu()
{
    read_os
    if [ "$os_release" == "ubuntu" ]; then
        return 0
    else
        return 1
    fi
}


#!/bin/bash
dnf install -y https://ecsft.cern.ch/dist/cvmfs/cvmfs-release/cvmfs-release-latest.noarch.rpm
dnf install -y cvmfs
dnf install -y https://github.com/EESSI/filesystem-layer/releases/download/latest/cvmfs-config-eessi-latest.noarch.rpm

# configure CernVM-FS (no proxy, 10GB quota for CernVM-FS cache)
bash -c "echo 'CVMFS_HTTP_PROXY=DIRECT' > /etc/cvmfs/default.local"
bash -c "echo 'CVMFS_QUOTA_LIMIT=10000' >> /etc/cvmfs/default.local"
cvmfs_config setup


#!/bin/bash
packages="sssd realmd oddjob oddjob-mkhomedir adcli samba-common samba-common-tools krb5-workstation openldap-clients policycoreutils-python-utils"

if ! rpm -q $packages; then
  echo "Installing packages $packages"
  dnf install -y $packages
  echo "Restart dbus systemd-logind"
  systemctl restart dbus
  systemctl restart systemd-logind
fi

echo "Update nameserver"
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/azure-dns
if ! grep "RES_OPTIONS" /etc/sysconfig/network; then
  echo "RES_OPTIONS=\"timeout:1 attempts:5\"" >> /etc/sysconfig/network
fi

# By default, Samba on RHEL 8.3 no longer supports the deprecated RC4 cipher suite
update-crypto-policies --set DEFAULT:AD-SUPPORT

# Stop NetworkManager overwriting /etc/resolv.conf
cat > /etc/NetworkManager/conf.d/90-dns-none.conf << EOF
[main]
dns=none
EOF

function enforce_hostname() {
  local system_hostname=$1
  local target_hostname=$2

  # ensure the correct hostname (update if necessary)
  echo "ensure the correct hostname (update if necessary)"

  if [ "$system_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect hostname ($system_hostname), it should be $target_hostname, updating"
    hostname $target_hostname
  fi
  if grep -i $system_hostname /etc/hosts; then
    logger -s "Warning: incorrect hostname ($system_hostname) in /etc/hosts, updating"
    sed -i "s/$system_hostname/$target_hostname/ig" /etc/hosts
  fi
  etc_hostname=$(</etc/hostname)
  if [ "$etc_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect /etc/hostname ($etc_hostname), it should be $target_hostname, updating"
    echo $target_hostname > /etc/hostname
  fi
  eth0_hostname=$(grep DHCP_HOSTNAME /etc/sysconfig/network-scripts/ifcfg-eth0 | cut -d'=' -f2)
  if [ "$eth0_hostname" != "" -a "$eth0_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect DHCP_HOSTNAME in /etc/sysconfig/network-scripts/ifcfg-eth0 ($etc_hostname), it should be $target_hostname, updating"
    sed -i "s/^DHCP_HOSTNAME=.*\$/DHCP_HOSTNAME=$target_hostname/g" /etc/sysconfig/network-scripts/ifcfg-eth0
    systemctl restart NetworkManager
  fi
}


#!/bin/bash
packages="nfs-utils"

if ! rpm -q $packages; then
  echo "Installing packages $packages"
  dnf install -y $packages
fi

setsebool -P use_nfs_home_dirs 1 || true


#!/bin/bash
rpm -q lbnl-nhc || dnf install -y "https://github.com/mej/nhc/releases/download/1.4.3/lbnl-nhc-1.4.3-1.el7.noarch.rpm"


#!/bin/bash
if [ ! -e /etc/yum.repos.d/influxdb.repo ]; then
echo "#### Configuration repo for InfluxDB:"
cat <<EOF | tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
EOF
fi

if ! rpm -q telegraf; then
  echo "#### Telegraf Installation:"
  dnf -y install telegraf jq
fi


#!/bin/bash
yum install -y https://ecsft.cern.ch/dist/cvmfs/cvmfs-release/cvmfs-release-latest.noarch.rpm
yum install -y cvmfs
yum install -y https://github.com/EESSI/filesystem-layer/releases/download/latest/cvmfs-config-eessi-latest.noarch.rpm

# configure CernVM-FS (no proxy, 10GB quota for CernVM-FS cache)
bash -c "echo 'CVMFS_HTTP_PROXY=DIRECT' > /etc/cvmfs/default.local"
bash -c "echo 'CVMFS_QUOTA_LIMIT=10000' >> /etc/cvmfs/default.local"
cvmfs_config setup


#!/bin/bash
yum install /usr/*bin/fuse2fs -y
yum install https://github.com/apptainer/apptainer/releases/download/v1.1.6/apptainer-1.1.6-1.x86_64.rpm -y


#!/bin/bash
packages="sssd realmd oddjob oddjob-mkhomedir adcli samba-common samba-common-tools krb5-workstation openldap-clients policycoreutils-python"

if ! rpm -q $packages; then
  echo "Installing packages $packages"
  yum install -y $packages
  echo "Restart dbus systemd-logind"
  systemctl restart dbus
  systemctl restart systemd-logind
fi

echo "Update nameserver"
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/azure-dns
if ! grep "RES_OPTIONS" /etc/sysconfig/network; then
  echo "RES_OPTIONS=\"timeout:1 attempts:5\"" >> /etc/sysconfig/network
fi

function enforce_hostname() {
  local system_hostname=$1
  local target_hostname=$2

  # ensure the correct hostname (update if necessary)
  echo "ensure the correct hostname (update if necessary)"

  if [ "$system_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect hostname ($system_hostname), it should be $target_hostname, updating"
    hostname $target_hostname
  fi
  if grep -i $system_hostname /etc/hosts; then
    logger -s "Warning: incorrect hostname ($system_hostname) in /etc/hosts, updating"
    sed -i "s/$system_hostname/$target_hostname/ig" /etc/hosts
  fi
  etc_hostname=$(</etc/hostname)
  if [ "$etc_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect /etc/hostname ($etc_hostname), it should be $target_hostname, updating"
    echo $target_hostname > /etc/hostname
  fi
  eth0_hostname=$(grep DHCP_HOSTNAME /etc/sysconfig/network-scripts/ifcfg-eth0 | cut -d'=' -f2)
  if [ "$eth0_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect DHCP_HOSTNAME in /etc/sysconfig/network-scripts/ifcfg-eth0 ($etc_hostname), it should be $target_hostname, updating"
    sed -i "s/^DHCP_HOSTNAME=.*\$/DHCP_HOSTNAME=$target_hostname/g" /etc/sysconfig/network-scripts/ifcfg-eth0
    systemctl restart NetworkManager
  fi
}


#!/bin/bash
packages="nfs-utils"

if ! rpm -q $packages; then
  echo "Installing packages $packages"
  yum install -y $packages
fi

setsebool -P use_nfs_home_dirs 1 || true


#!/bin/bash
rpm -q lbnl-nhc || yum install -y "https://github.com/mej/nhc/releases/download/1.4.3/lbnl-nhc-1.4.3-1.el7.noarch.rpm"


#!/bin/bash
if [ ! -e /etc/yum.repos.d/influxdb.repo ]; then
echo "#### Configuration repo for InfluxDB:"
cat <<EOF | tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/centos/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
EOF
fi

if ! rpm -q telegraf; then
  echo "#### Telegraf Installation:"
  yum -y install telegraf jq
fi


#!/bin/bash
# This code is executed when NHC detects an error
# Parameters:
#    1: SLURM node name
#    2+: Error message
# Logs following information in CC log and requests CC to terminate the node:
#    Hostname
#    Physical host name
#    NHC error message

echo "`date '+%Y%m%d %H:%M:%S'` $0 $*"

HOSTNAME="$1"
shift
NOTE="$*"

PHYSICAL_HOST=$(strings /var/lib/hyperv/.kvp_pool_3 | grep -A1 PhysicalHostName | head -n 2 | tail -1)
JETPACK=/opt/cycle/jetpack/bin/jetpack

echo "$0:  ERROR : Node Health Checks failed on $HOSTNAME - $PHYSICAL_HOST - $NOTE"
$JETPACK log "ERROR : Node Health Checks failed - $(hostname) - $PHYSICAL_HOST - $NOTE" --level error
/usr/libexec/nhc/node-mark-offline $HOSTNAME $NOTE
echo "$JETPACK shutdown --unhealthy" | at now +2 minutes
# To keep the VM online for debugging, replace the line above with the following:
#  $JETPACK keepalive forever


#!/bin/bash

add-apt-repository -y ppa:apptainer/ppa
apt install -y apptainer


#!/bin/bash
if ! dpkg -l chrony; then
  apt-get install -y chrony
fi


#!/bin/bash

packages="sssd libsss-simpleifp0 sssd-dbus sssd-tools realmd oddjob oddjob-mkhomedir adcli samba-common krb5-user ldap-utils packagekit resolvconf"

if [ "$os_maj_ver" == "18.04" ]; then
    packages="$packages python-sss"
fi

if ! dpkg -l $packages ; then
  apt -y update
  echo "Installing packages $packages"
  # this export is needed to stop input for krb5-user
  export DEBIAN_FRONTEND=noninteractive
  apt-get install -y $packages
  echo "Restart dbus"
  # Manually restarting dbus is blocked on ubuntu
  # systemctl restart dbus
  echo "Restart systemd-logind"
  systemctl restart systemd-logind
fi

# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/azure-dns
if grep "^options " /etc/resolv.conf; then
  sed -i 's/^options /options timeout:1 attempts:5 /' /etc/resolv.conf
else
  echo "options timeout:1 attempts:5" >> /etc/resolv.conf
fi

# Disable rdns for libdefaults in /etc/krb5.conf
if grep "rdns = false" /etc/krb5.conf; then
  echo "rdns already set to false"
else
  echo "set rdns to false for libdefaults"
  sed -i '/\[libdefaults\]/a\\trdns = false' /etc/krb5.conf
fi

function enforce_hostname() {
  local system_hostname=$1
  local target_hostname=$2

  # ensure the correct hostname (update if necessary)
  echo "ensure the correct hostname (update if necessary)"

  if [ "$system_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect hostname ($system_hostname), it should be $target_hostname, updating"
    hostname $target_hostname
  fi
  if grep -i $system_hostname /etc/hosts; then
    logger -s "Warning: incorrect hostname ($system_hostname) in /etc/hosts, updating"
    sed -i "s/$system_hostname/$target_hostname/ig" /etc/hosts
  fi
  etc_hostname=$(</etc/hostname)
  if [ "$etc_hostname" != "$target_hostname" ]; then
    logger -s "Warning: incorrect /etc/hostname ($etc_hostname), it should be $target_hostname, updating"
    echo $target_hostname > /etc/hostname
  fi

  if ! systemctl restart systemd-networkd; then
    logger -s "failed to restart systemd-networkd"
  fi
}


#!/bin/bash
packages="nfs-common"

if ! dpkg -l $packages ; then
  echo "Installing packages $packages"
  apt-get install -y $packages
fi


#!/bin/bash

cd /root
if [ ! -f /usr/sbin/nhc ] || [ ! -d /etc/nhc ] ; then
    git clone https://github.com/mej/nhc.git -b 1.4.3
    cd nhc
    ./autogen.sh
    ./configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
    #make test
    make install
fi


#!/bin/bash
if ! dpkg -l telegraf; then

  wget -qO- https://repos.influxdata.com/influxdata-archive_compat.key | tee /etc/apt/trusted.gpg.d/influxdb.asc >/dev/null
  source /etc/os-release
  echo "deb https://repos.influxdata.com/${ID} ${VERSION_CODENAME} stable" | tee /etc/apt/sources.list.d/influxdb.list
  apt-get update
  apt-get install -y telegraf jq

fi


#!/bin/bash

NVME_DISKS_NAME=`ls /dev/nvme*n1`
NVME_DISKS=`ls -latr /dev/nvme*n1 | wc -l`

echo "Number of NVMe Disks: $NVME_DISKS"

if [ "$NVME_DISKS" == "0" ]
then
    exit 0
else
    mkdir -p /mnt/nvme
    # Needed incase something did not unmount as expected. This will delete any data that may be left behind
    mdadm  --stop /dev/md*
    mdadm --create /dev/md128 -f --run --level 0 --raid-devices $NVME_DISKS $NVME_DISKS_NAME
    mkfs.xfs -f /dev/md128
    mount /dev/md128 /mnt/nvme || exit 1
fi

chmod 1777 /mnt/nvme


#!/bin/bash
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$script_dir/../files/azhop-helpers.sh"
read_os

$script_dir/../files/$os_release/init_telegraf.sh

echo "Configuring global tags"
AZHPC_VMSIZE=$(curl -s --noproxy "*" -H Metadata:true "http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15" | jq -r '.vmSize' | tr '[:upper:]' '[:lower:]')
PHYSICAL_HOST=$(strings /var/lib/hyperv/.kvp_pool_3 | grep -A1 PhysicalHostName | head -n 2 | tail -1)
VMSS=$(curl -s -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2018-10-01" | jq -r '.compute.vmScaleSetName')

sed -i "s/__SKU__/${AZHPC_VMSIZE}/g" ../files/telegraf.conf
sed -i "s/__PHYS_HOST__/${PHYSICAL_HOST}/g" ../files/telegraf.conf
sed -i "s/__VMSS__/${VMSS}/g" ../files/telegraf.conf

echo "Copy configuration file to use"
TELEGRAF_CONF_DIR=/etc/telegraf
cp ../files/telegraf.conf $TELEGRAF_CONF_DIR/telegraf.conf
chown telegraf:root $TELEGRAF_CONF_DIR/telegraf.conf
chmod 600 $TELEGRAF_CONF_DIR/telegraf.conf

echo "#### Starting Telegraf services:"
systemctl enable telegraf
systemctl restart telegraf


#!/bin/bash
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$script_dir/../files/azhop-helpers.sh"
read_os

$script_dir/../files/$os_release/init_chronyd.sh

if [ -e /dev/ptp_hyperv ]; then
  PTP="ptp_hyperv"
else
  PTP="ptp0"
fi

cat <<EOF >/etc/chrony.conf
driftfile /var/lib/chrony/drift
makestep 1.0 -1
rtcsync
logdir /var/log/chrony
refclock PHC /dev/$PTP poll 3 dpoll -2 offset 0 stratum 2
EOF

chmod 644 /etc/chrony.conf

systemctl enable chronyd
systemctl restart chronyd


#!/bin/bash
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$script_dir/../files/azhop-helpers.sh"
read_os

$script_dir/../files/$os_release/cvmfs-eessi.sh


#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$SCRIPT_DIR/../files/azhop-helpers.sh"
read_os

$SCRIPT_DIR/../files/$os_release/init_nhc.sh

# Install custom checks
cp -v $SCRIPT_DIR/../files/nhc/scripts/* /etc/nhc/scripts/

# Install azhop-node-offline.sh
cp -v $SCRIPT_DIR/../files/nhc/azhop-node-offline.sh /usr/libexec/nhc/
chmod 755 /usr/libexec/nhc/azhop-node-offline.sh

NHC_CONFIG_FILE="/etc/nhc/nhc.conf"
VM_SIZE=$(curl -s --noproxy "*" -H Metadata:true "http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15" | jq -r '.vmSize' | tr '[:upper:]' '[:lower:]' | sed 's/standard_//')

NHC_CONFIG_EXTRA="$SCRIPT_DIR/../files/nhc/nhc_${VM_SIZE}.conf"

# Use common config for all compute nodes
if [ -e $NHC_CONFIG_FILE ]; then
    rm -f ${NHC_CONFIG_FILE}.bak
    mv $NHC_CONFIG_FILE ${NHC_CONFIG_FILE}.bak
fi
cp -fv $SCRIPT_DIR/../files/nhc/nhc_common.conf $NHC_CONFIG_FILE

# Append VM size specific config if exists
if [ -e $NHC_CONFIG_EXTRA  ]; then
    cat $NHC_CONFIG_EXTRA >> $NHC_CONFIG_FILE
fi

# Add nvidia-smi health checks for GPU SKUs except NV_v4 as they don't have NVIDIA device
case $VM_SIZE in
    nv*v4)
    ;;
    nc*|nv*|nd*)
        echo " * || check_nvsmi_healthmon" >> $NHC_CONFIG_FILE
    ;;
    # Check HDR InfiniBand on all HBv2 and HBv3 SKUs
    hb*v2|hb*v3)
        echo " * || check_hw_ib 200 mlx5_ib0:1" >> $NHC_CONFIG_FILE
    ;;
    hc44rs)
        echo " * || check_hw_ib 100 mlx5_ib0:1" >> $NHC_CONFIG_FILE
    ;;

esac


#!/bin/bash
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$script_dir/../files/azhop-helpers.sh"
read_os

if [ $os_maj_ver != '18.04' ]; then
  $script_dir/../files/$os_release/init_apptainer.sh
else
  exit 0
fi


#!/bin/bash
# written by Jeff Jones. Slight modifications by Matt Chan

echo "Resetting /mnt/resource/tmpscratch and /tmp/scratch"
rm -rf /mnt/resource/tmpscratch
rm -rf /mnt/nvme/tmpscratch
rm -rf /tmp/scratch
rm -rf /mnt/scratch
if [ -d /mnt/nvme ]; then
    chmod -R 1777 /mnt/nvme
    mkdir /mnt/nvme/tmpscratch
    chmod -R 1777 /mnt/nvme/tmpscratch
#    touch /mnt/nvme/tmpscratch/PUT_YOUR_USER_SCRATCH_FOLDERS_HERE
    ln -s /mnt/nvme/tmpscratch /tmp/scratch
    ln -s /mnt/nvme/tmpscratch /mnt/scratch
elif [ -d /mnt/resource ]; then
    chmod -R 1777 /mnt/resource
    mkdir /mnt/resource/tmpscratch
    chmod -R 1777 /mnt/resource/tmpscratch
#    touch /mnt/resource/tmpscratch/PUT_YOUR_USER_SCRATCH_FOLDERS_HERE
    ln -s /mnt/resource/tmpscratch /tmp/scratch
    ln -s /mnt/resource/tmpscratch /mnt/scratch
else
    echo "/mnt/resource nor /mnt/nvme does not exist!"
    exit 1
fi
echo "Cleanup complete."


#!/bin/bash

errormessage=$( /usr/sbin/nhc 2>&1)
error=$?

# In case of health check failure, shutdown the node by calling the script /usr/libexec/nhc/azhop-node-offline.sh
if [ $error -eq 1 ]; then
    /usr/libexec/nhc/azhop-node-offline.sh $(hostname) "$errormessage"
    JETPACK=/opt/cycle/jetpack/bin/jetpack
    $JETPACK shutdown --unhealthy
fi


#!/bin/bash
# Placeholder to add any of your custom scripts here

# If you need to write OS specific scripts then copy them in the ../files/$os_release folder and use the following lines to call it
#script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#source "$script_dir/../files/azhop-helpers.sh"
#read_os

#$script_dir/../files/$os_release/mystuff.sh


#!/bin/bash
# Library of functions to be used across scripts
read_os()
{
    os_release=$(cat /etc/os-release | grep "^ID\=" | cut -d'=' -f 2 | xargs)
    os_maj_ver=$(cat /etc/os-release | grep "^VERSION_ID\=" | cut -d'=' -f 2 | xargs)
    full_version=$(cat /etc/os-release | grep "^VERSION\=" | cut -d'=' -f 2 | xargs)
}

is_centos()
{
    read_os
    if [ "$os_release" == "centos" ]; then
        return 0
    else
        return 1
    fi
}

is_centos8()
{
    read_os
    if [ "$os_release" == "centos" ] && [ "$os_maj_ver" == "8" ]; then
        return 0
    else
        return 1
    fi
}

is_centos7()
{
    read_os
    if [ "$os_release" == "centos" ] && [ "$os_maj_ver" == "7" ]; then
        return 0
    else
        return 1
    fi
}

is_ubuntu()
{
    read_os
    if [ "$os_release" == "ubuntu" ]; then
        return 0
    else
        return 1
    fi
}


#!/bin/bash

BUILD_FROM_SOURCE=yes
[ -d /opt/pbs ] && exit 0

if [ "$BUILD_FROM_SOURCE" == "yes" ] ; then
    dnf install -y gcc make rpm-build libtool hwloc-devel \
        libX11-devel libXt-devel libedit-devel libical-devel \
        ncurses-devel perl postgresql-devel postgresql-contrib python2 python2-devel tcl-devel \
        tk-devel swig expat-devel openssl-devel libXext libXft \
        autoconf automake gcc-c++ git jq

    cd /mnt
    git clone https://github.com/open-mpi/hwloc.git -b v1.11
    cd hwloc
    ./configure --enable-static --enable-embedded-mode
    make
    cd ..

    wget -q https://github.com/openpbs/openpbs/releases/download/v19.1.1/pbspro-19.1.1.tar.gz -O pbspro-19.1.1.tar.gz
    tar -xzf pbspro-19.1.1.tar.gz
    cd pbspro-19.1.1/
    ./configure --prefix=/opt/pbs PYTHON=/usr/bin/python2.7 CFLAGS="-I$PWD/../hwloc/include" LDFLAGS="-L$PWD/../src"
    make
    make install

    /opt/pbs/libexec/pbs_postinstall execution
    chmod 4755 /opt/pbs/sbin/pbs_iff /opt/pbs/sbin/pbs_rcp
else
    wget https://github.com/openpbs/openpbs/releases/download/v20.0.1/openpbs_20.0.1.centos_8.zip
    unzip -o openpbs_20.0.1.centos_8.zip
    dnf install epel-release -y
    dnf install -y openpbs_20.0.1.centos_8/openpbs-execution-20.0.1-0.x86_64.rpm jq
    rm -rf openpbs_20.0.1.centos_8.zip
    rm -rf openpbs_20.0.1.centos_8
fi


#!/bin/bash
echo "Downloading PBS RPMs"
wget -q https://github.com/PBSPro/pbspro/releases/download/v19.1.1/pbspro_19.1.1.centos7.zip
unzip -o pbspro_19.1.1.centos7.zip
echo "Installing PBS RPMs"
yum install -y epel-release
yum install -y pbspro_19.1.1.centos7/pbspro-execution-19.1.1-0.x86_64.rpm jq


#!/bin/bash
cd /mnt
wget -q https://github.com/openpbs/openpbs/releases/download/v19.1.1/pbspro-19.1.1.tar.gz -O pbspro-19.1.1.tar.gz
tar -xzf pbspro-19.1.1.tar.gz
cd pbspro-19.1.1/

apt-get install -y gcc make libtool libhwloc-dev libx11-dev libxt-dev libedit-dev libical-dev ncurses-dev perl postgresql-server-dev-all postgresql-contrib python-dev tcl-dev tk-dev swig libexpat-dev libssl-dev libxext-dev libxft-dev autoconf automake
./autogen.sh
./configure --prefix=/opt/pbs PYTHON=/usr/bin/python2.7
make
make install

/opt/pbs/libexec/pbs_postinstall execution
chmod 4755 /opt/pbs/sbin/pbs_iff /opt/pbs/sbin/pbs_rcp


#!/bin/bash

cp ../files/autostop.rb /opt/cycle/jetpack/system/bootstrap/autostop.rb
chmod 700 /opt/cycle/jetpack/system/bootstrap/autostop.rb

#write out current crontab
crontab -l > tempcron
#echo new cron into cron file
echo "* * * * * /opt/cycle/jetpack/system/bootstrap/cron_wrapper.sh /opt/cycle/jetpack/system/bootstrap/autostop.rb >> /opt/cycle/jetpack/logs/autostop.out 1>&2" >> tempcron
#install new cron file
crontab tempcron

rm tempcron


#!/bin/bash
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$script_dir/../files/azhop-helpers.sh"
read_os

find $script_dir/../files -name "*.sh" -exec chmod +x {} \;

# If PBS is not installed, then install it
if [ ! -f "/etc/pbs.conf" ]; then
    $script_dir/../files/$os_release/installpbs.sh
fi

echo "Configuring PBS"

sed -i 's/CHANGE_THIS_TO_PBS_PRO_SERVER_HOSTNAME/scheduler/' /etc/pbs.conf
sed -i 's/CHANGE_THIS_TO_PBS_PRO_SERVER_HOSTNAME/scheduler/' /var/spool/pbs/mom_priv/config
sed -i "s/^if /#if /g" /opt/pbs/lib/init.d/limits.pbs_mom
sed -i "s/^fi/#fi /g" /opt/pbs/lib/init.d/limits.pbs_mom

systemctl restart pbs || exit 1
echo "PBS Restarted"


#!/usr/bin/env bash
set -e
echo "Getting container..."
echo "TTT - $(date)"
container_image=$1
container_name=$2

echo "container_image=$container_image"
echo "container_name=$container_name"
# add the default docker:// if no prefix is provided
if [ "$container_image" != *"://"* ]; then
    container_image="docker://$container_image"
    echo "container_image=$container_image"
fi

echo "enroot import"
# remove any prefix :// and replace chars in (/:) by +
container_squashfs_path=${container_image#*://}
container_squashfs_path=${container_squashfs_path//\//+}
container_squashfs_path=${container_squashfs_path//:/+}

# get the enroot temp path to store squashfs files
ENROOT_TEMP_PATH=$(grep ENROOT_TEMP_PATH /etc/enroot/enroot.conf | cut -d' ' -f2)
ENROOT_TEMP_PATH=${ENROOT_TEMP_PATH:-/tmp}
echo "ENROOT_TEMP_PATH=$ENROOT_TEMP_PATH"
container_squashfs_path="$ENROOT_TEMP_PATH/$container_squashfs_path.sqsh"
echo "container_squashfs_path=$container_squashfs_path"

# import the container and save the sqsh file in the enroot temporary path configured in /etc/enroot/enroot.conf
enroot import --output $container_squashfs_path $container_image

echo "enroot create container"
enroot create --name $container_name $container_squashfs_path

echo "remove squashfs file $container_squashfs_path"
rm -f $container_squashfs_path


#!/usr/bin/env bash
set -e
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Starting enroot wrapper..."
echo "TTT - $(date)"
container_name=$1

echo "container_name=$container_name"
echo "enroot start"
enroot start --rw -e NOTEBOOK_ROOT="$NOTEBOOK_ROOT" -e CONFIG_FILE="$CONFIG_FILE" $container_name $THIS_DIR/start_jupyter.sh
echo "enroot remove"
enroot remove -f $container_name


#!/usr/bin/env bash
echo "Starting main script..."
echo "TTT - $(date)"

echo "Installing azure machine learning tools..."
pip install azure-cli
pip install azureml-core
pip install azure-ai-ml
pip install azure.identity

echo "Installing customized jupyter launcher..."
pip install jupyter_app_launcher
jupyter labextension list

JUPYTERAPPLAUNCHERDIR="${NOTEBOOK_ROOT}"/.local/share/jupyter/jupyter_app_launcher/
mkdir $JUPYTERAPPLAUNCHERDIR
JUPYTERAPPLAUNCHERCONF=$JUPYTERAPPLAUNCHERDIR/config.yaml

cat << EOF > $JUPYTERAPPLAUNCHERCONF
- title: AzureML Notebook
  description: AzureML notebook
  source: ${NOTEBOOK_ROOT}/new1.ipynb
  cwd: ${NOTEBOOK_ROOT}
  type: notebook
  catalog: Notebook
  icon: $JUPYTERAPPLAUNCHERDIR/azure-ml.svg
EOF

cp azure-ml.svg $JUPYTERAPPLAUNCHERDIR

#
# Start Jupyter server
#

# Set working directory to notebook root directory

cp nbbuilder.py "${NOTEBOOK_ROOT}"/
cp amlwrapperfunctions.py "${NOTEBOOK_ROOT}"/
cd "${NOTEBOOK_ROOT}"
echo "TTT - $(date)"

echo "notebook root= ${NOTEBOOK_ROOT}"
echo "building main notebook"
# have to be careful with AMLSDKJOBINPUTS as it may start with "--"
set -o xtrace
python nbbuilder.py -s $AMLSDKSUBSCRIPTION -l $AMLSDKLOCATION -rg $AMLSDKRESOURCEGROUP -ws $AMLSDKWORKSPACE -ae $AMLSDKAMLENV -mt $AMLSDKSKU -mi $AMLSDKINSTANCECOUNT -j $AMLSDKJOBCODE -ji " $AMLSDKJOBINPUTS"
set +o xtrace

# List available kernels for debugging purposes
set -x
jupyter kernelspec list
{ set +x; } 2>/dev/null
echo "TTT - $(date)"

# Launch the Jupyter server
set -x
jupyter lab --config="${CONFIG_FILE}"


#!/usr/bin/env bash
echo "Starting main script..."
echo "TTT - $(date)"

# install dependencies
pip install ipyfilechooser voila jupyter_app_launcher
jupyter labextension list

JUPYTERAPPLAUNCHERDIR="${NOTEBOOK_ROOT}/.local/share/jupyter/jupyter_app_launcher"
mkdir -p $JUPYTERAPPLAUNCHERDIR
JUPYTERAPPLAUNCHERCONF=$JUPYTERAPPLAUNCHERDIR/config.yaml

mkdir $HOME/jupyter_nb_apps

cp OFLogView.ipynb $HOME/jupyter_nb_apps
cp OFLogView.svg $JUPYTERAPPLAUNCHERDIR

cat << EOF > $JUPYTERAPPLAUNCHERCONF
- title: OpenFOAM Log Viewer
  description: Plot log files for OpenFOAM
  source: /node/${host}/${port}/voila/render/jupyter_nb_apps/OFLogView.ipynb
  type: url
  catalog: OpenFOAM
  icon: $JUPYTERAPPLAUNCHERDIR/OFLogView.svg
  args:
      sandbox: [ 'allow-same-origin', 'allow-scripts', 'allow-downloads', 'allow-modals', 'allow-popups']
      referrerPolicy: ['no-referrer']
EOF

#
# Start Jupyter server
#

# Set working directory to notebook root directory
cd "${NOTEBOOK_ROOT}"
echo "TTT - $(date)"

# List available kernels for debugging purposes
set -x
jupyter kernelspec list
{ set +x; } 2>/dev/null
echo "TTT - $(date)"

# Launch the Jupyter server
set -x
jupyter lab --config="${CONFIG_FILE}"


#!/usr/bin/env bash
set -e
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Starting enroot wrapper..."
echo "TTT - $(date)"
container_name=$1

echo "container_name=$container_name"
echo "enroot start"
enroot start --rw -e NOTEBOOK_ROOT="$NOTEBOOK_ROOT" -e CONFIG_FILE="$CONFIG_FILE" $container_name $THIS_DIR/start_rstudio.sh
echo "enroot remove"
enroot remove -f $container_name


#!/usr/bin/env bash
echo "Starting main script..."
echo "TTT - $(date)"

## solve the non-root user problem
set -x
cat << EOF >/etc/rstudio/rserver.conf
rsession-which-r=/usr/local/bin/R
server-user=$USER
auth-none=1
EOF

# Launch the Rstudio server
set -x
/usr/lib/rstudio-server/bin/rserver --www-port ${port}


#!/bin/bash
#
# PBS/Scalelib Integration for az-hop.
# Create new vizq queue and update some default settings

#/opt/pbs/bin/qmgr -c 'set server managers = root@*'
#/opt/pbs/bin/qmgr -c 'set server query_other_jobs = true'
#/opt/pbs/bin/qmgr -c 'set server scheduler_iteration = 15'
#/opt/pbs/bin/qmgr -c 'set server flatuid = true'
#/opt/pbs/bin/qmgr -c 'set server job_history_enable=true' # fixed in 2.0.3

function create_resource() {
	/opt/pbs/bin/qmgr -c "list resource $1" >/dev/null  2>/dev/null   || \
	/opt/pbs/bin/qmgr -c "create resource $1 type=$2, flag=h"
}

#create_resource slot_type string
#create_resource group_id string
#create_resource ungrouped string
#create_resource instance_id string
create_resource machinetype string
#create_resource nodearray string
#create_resource disk size
#create_resource ngpus size

#/opt/pbs/bin/qmgr -c "set queue workq resources_default.ungrouped = false"
#/opt/pbs/bin/qmgr -c "set queue workq resources_default.place = scatter" # Default in scalelib is scatter:excl
/opt/pbs/bin/qmgr -c "set queue workq default_chunk.ungrouped = false"
/opt/pbs/bin/qmgr -c "set queue workq resources_default.slot_type = execute"

#/opt/pbs/bin/qmgr -c "create queue htcq"
#/opt/pbs/bin/qmgr -c "set queue htcq queue_type = Execution"
#/opt/pbs/bin/qmgr -c "set queue htcq resources_default.ungrouped = true"
/opt/pbs/bin/qmgr -c "set queue htcq resources_default.place = pack" # Default in scalelib is free
#/opt/pbs/bin/qmgr -c "set queue htcq default_chunk.ungrouped = true"
#/opt/pbs/bin/qmgr -c "set queue htcq enabled = true"
#/opt/pbs/bin/qmgr -c "set queue htcq started = true"
/opt/pbs/bin/qmgr -c "set queue htcq resources_default.slot_type = execute"

/opt/pbs/bin/qmgr -c "create queue vizq"
/opt/pbs/bin/qmgr -c "set queue vizq queue_type = Execution"
/opt/pbs/bin/qmgr -c "set queue vizq resources_default.ungrouped = true"
/opt/pbs/bin/qmgr -c "set queue vizq resources_default.place = shared:scatter"
/opt/pbs/bin/qmgr -c "set queue vizq resources_default.slot_type = viz3d"
/opt/pbs/bin/qmgr -c "set queue vizq default_chunk.ungrouped = true"
/opt/pbs/bin/qmgr -c "set queue vizq enabled = true"
/opt/pbs/bin/qmgr -c "set queue vizq started = true"

#/opt/pbs/bin/qmgr -c "set sched only_explicit_psets=True"
#/opt/pbs/bin/qmgr -c "set sched do_not_span_psets=True"

/opt/pbs/bin/qmgr -c "set hook autoscale freq = 60"


#!/bin/bash
[[ -n $PBS_NODEFILE ]] && { ISPBS=true; JOBID=$PBS_JOBID; }
[[ -n $SLURM_NODELIST ]] && { ISSLURM=true; JOBID=$SLURM_JOBID; }

get_vm_size()
{
    export VM_SIZE=$(curl -s --noproxy "*" -H Metadata:true "http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15" | jq -r '.vmSize' | tr '[:upper:]' '[:lower:]')
}

print_vm_type()
{
    get_vm_size
    echo "Running on $VM_SIZE"
}

init_modules()
{
    echo "Initializing modules"
    source /etc/profile.d/modules.sh
    module use /usr/share/Modules/modulefiles
    module avail
}


#!/bin/bash
EXPECTED_VM_SIZE=${1,,} # convert to lowercase

VM_SIZE=$(curl -s --noproxy "*" -H Metadata:true "http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15" | jq -r '.vmSize' | tr '[:upper:]' '[:lower:]')

if [ "$VM_SIZE" != "$EXPECTED_VM_SIZE" ] && [ "$EXPECTED_VM_SIZE" != "any" ]; then
  echo "Wrong VM Size expected. Expected $EXPECTED_VM_SIZE but running on $VM_SIZE"
  echo "ERROR"
  exit 1
fi

echo "Running on $VM_SIZE"
case $VM_SIZE in
  standard_nv*)
    nvidia-smi | grep NVIDIA
    if [ $? -eq 1 ]; then
        echo "ERROR"
        exit 1
    fi
  ;;
esac

sleep 60
echo "PASSED"


#!/bin/bash
# Define a correct maximum walltime
PBS_MAX_WALLTIME="00:10:00" # 10 minutes to run a job
MAX_WAIT_TIME=1800 # 30 minutes to wait for all jobs to finish

function check_scheduler()
{
    echo "Check PBS"
    echo "Run qstat"
    qstat || exit 1
}

function wait_alljobs()
{
    local jobgroup=$1
    # Wait for all jobs to be finished
    local active_jobs=$(qselect -N $jobgroup | wc -l)
    wait_time=0
    while [ $active_jobs -ne 0 ] && [ $wait_time -lt $MAX_WAIT_TIME ]; do
        echo "There are $active_jobs active jobs since $wait_time seconds"
        sleep 60
        wait_time=$((wait_time+60))
        active_jobs=$(qselect -N $jobgroup | wc -l)
    done

    if [ $wait_time -ge $MAX_WAIT_TIME ]; then
        echo "Timeout while waiting for jobs"
        list_nodes
        qstat -anw
        qselect -N $jobgroup | xargs qdel -W force
        exit 1
    fi

    echo "All jobs finished"
}

function check_jobstatus()
{
    local jobgroup=$1
    # Get the full list of jobs
    qstat -H | grep $jobgroup &>/dev/null || exit 1
    local job_list=$(qstat -H | grep $jobgroup | cut -d'.' -f1 | xargs)

    # For each job test the exit code, in case of failure report it and set the error flag so we can exit with an error
    local test_failure=0
    for jobid in $job_list; do
        local exit_status=$(qstat -fx $jobid | grep Exit_status | xargs | cut -d' ' -f3)
        if [ $exit_status != 0 ]; then
            echo "ERROR $exit_status: Failure while running job $jobid"
            qstat -fx $jobid
            cat *.o$jobid
            test_failure=1
        else
            echo "$jobid OK"
            tail -n20 *.o$jobid
        fi
    done

    if [ $test_failure -ne 0 ]; then
        exit $test_failure
    fi
}

function get_pool_list()
{
    pbsnodes -avS &>/dev/null || exit 1
    echo $(pbsnodes -av | grep pool_name | cut -d'=' -f2 | sort | uniq | xargs)
}

function get_node_count()
{
    local pool=${1-"="}
    pbsnodes -avS &>/dev/null || exit 1
    echo $(pbsnodes -a -F dsv | grep "$pool" | wc -l)
}

function list_nodes()
{
    local pool=${1-"="}
    pbsnodes -a -F dsv | grep "$pool"
}

function get_node_list()
{
    local pool=${1-"="}
    echo $(pbsnodes -a -F dsv | grep "$pool" | cut -d'|' -f1 | cut -d'=' -f2 | sort)
}

function get_node_core_count()
{
    local pool=${1-""}
    pbsnodes -avS &>/dev/null || exit 1
    echo $(pbsnodes -a -F dsv | grep "=$pool|" | cut -d'|' -f5 | head -n1 | cut -d'=' -f2)
}

function submit_job()
{
    local job_name=$1
    local node_count=$2
    local ppn=$3
    local slot_type=$4
    shift; shift; shift; shift
    local script=$@

    echo "job_name=$job_name; node_count=$node_count; ppn=$ppn; slot_type=$slot_type; script=$script"
    qsub -l walltime=$PBS_MAX_WALLTIME -N $job_name -k oe \
            -j oe -l select=$node_count:ncpus=$ppn:mpiprocs=$ppn:slot_type=$slot_type \
            -- $script
    if [ "$?" -ne "0" ]; then
        echo "Unable to submit job"
        exit 1
    fi
}


#!/bin/bash
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source ${THIS_DIR}/common.sh
print_vm_type

init_modules

module load mpi/impi-2021

#export I_MPI_FABRICS="shm:ofi"
export I_MPI_DEBUG=5
#export FI_PROVIDER=mlx
export I_MPI_HYDRA_BOOTSTRAP=ssh
IMB_ROOT=$I_MPI_ROOT/bin
mpi_options=""
if [[ "$ISPBS" = true ]]; then
    mpi_options=" -hostfile $PBS_NODEFILE"
fi
mpirun $mpi_options -np 2 -ppn 1 $IMB_ROOT/IMB-MPI1 PingPong

module unload mpi/impi-2021
unset I_MPI_DEBUG
unset I_MPI_HYDRA_BOOTSTRAP

# module load mpi/hpcx
# IMB_ROOT=$HPCX_MPI_TESTS_DIR/imb
# mpi_options=" -bind-to core"
# mpi_options+=" -mca coll_hcoll_enable 1 -x HCOLL_ENABLE_MCAST_ALL=1"

# if [[ "$ISPBS" = true ]]; then
#     mpi_options+=" -hostfile $PBS_NODEFILE"
#     mpi_options+=" -npernode 1 -np 2"
# fi
# mpirun $mpi_options $IMB_ROOT/IMB-MPI1 PingPong


#!/bin/bash
# Define a correct maximum walltime
MAX_WALLTIME="00:10:00" # 10 minutes to run a job
MAX_WAIT_TIME=1800 # 30 minutes to wait for all jobs to finish

function check_scheduler()
{
    export MAX_WALLTIME="00:20:00"
    echo "Check SLURM"
    echo "Run sinfo"
    sinfo || exit 1
}

function wait_alljobs()
{
    local jobgroup=$1
    # Wait for all jobs to be finished
    local active_jobs=$(squeue -n $jobgroup --noheader | wc -l)
    wait_time=0
    while [ $active_jobs -ne 0 ] && [ $wait_time -lt $MAX_WAIT_TIME ]; do
        echo "There are $active_jobs active jobs since $wait_time seconds"
        sleep 60
        wait_time=$((wait_time+60))
        active_jobs=$(squeue -n $jobgroup --noheader | wc -l)
    done

    if [ $wait_time -ge $MAX_WAIT_TIME ]; then
        echo "Timeout while waiting for jobs"
        sinfo -l
        sinfo -R
        squeue -l
        scancel -n $jobgroup
        exit 1
    fi

    echo "All jobs finished"
}

function check_jobstatus()
{
    local jobgroup=$1
    local failed_jobs=$(grep "ERROR" $jobgroup* | wc -l)
    if [ $failed_jobs != 0 ]; then
        echo "ERROR : Failure while running jobs"
        grep "ERROR" $jobgroup*.out
        exit 1
    fi
}

function list_nodes()
{
    sinfo -Nel
}

function submit_job()
{
    local job_name=$1
    local node_count=$2
    local ppn=$3
    local slot_type=$4
    shift; shift; shift; shift
    local script=$@
    local slurm_options=""

    if [ "$slot_type" == "viz3d" ]; then
        slurm_options=" --gpus=1"
    fi
    echo "job_name=$job_name; node_count=$node_count; ppn=$ppn; slot_type=$slot_type; script=$script"
    sbatch --job-name=$job_name \
           --nodes=$node_count \
           --ntasks-per-node=$ppn \
           --time=$MAX_WALLTIME \
           --partition=$slot_type \
           --output="%x-%j.out" \
           --error="%x-%j.out" \
           $slurm_options \
           $script

    if [ "$?" -ne "0" ]; then
        echo "Unable to submit job"
        exit 1
    fi
}


#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
pushd $DIR
buildenv=${1:-local}
login_server=$2

tag=$(date +"%g%m.%d%H")
case "$buildenv" in
    "github")
        hpcrover="hpcrover:${tag}"
        latest="hpcrover:latest"
    ;;

    "local")
        hpcrover="hpcrover:${tag}"
    ;;
esac

echo "Creating version ${hpcrover}"

# Build the base image
#docker-compose build
docker build . -t ${login_server}/${hpcrover}

#docker tag toolset_hpcrover ${hpcrover}
case "$buildenv" in
    "github")
        docker login ${login_server}
        docker push ${login_server}/${hpcrover}
        docker tag ${login_server}/${hpcrover} ${login_server}/${latest}
    ;;

    "local")
    ;;
esac

echo "Version ${hpcrover} created."
popd


#!/bin/bash
set -e
# Installs Ansible. Optionally in a conda environment.
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

MINICONDA_URL_LINUX_X86="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"
MINICONDA_URL_LINUX_ARM="https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-aarch64.sh"
MINICONDA_URL_MAC_X86="https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh"
MINICONDA_URL_MAC_ARM="https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh"
MINICONDA_INSTALL_DIR="miniconda"
MINICONDA_INSTALL_SCRIPT="miniconda-installer.sh"

# Always use of virtual environment
INSTALL_IN_CONDA=${INSTALL_IN_CONDA:-true}
if [ $INSTALL_IN_CONDA = true ]; then
    os_type=$(uname | awk '{print tolower($0)}')
    os_arch=$(arch)
    if [[ "$os_type" == "darwin" ]]; then
        if [[ "$os_arch" == "arm64" ]]; then
            miniconda_url=$MINICONDA_URL_MAC_ARM
        else
            miniconda_url=$MINICONDA_URL_MAC_X86
        fi
    elif [[ "$os_type" == "linux" ]]; then
        if [[ "$os_arch" == "aarch64" ]]; then
            miniconda_url=$MINICONDA_URL_LINUX_ARM
        else
            miniconda_url=$MINICONDA_URL_LINUX_X86
        fi
    else
        printf "Unsupported OS"
        exit 1
    fi

    # Reuse environment if it doesn't already exist
    if [[ ! -d "${MINICONDA_INSTALL_DIR}" ]]; then
        printf "Installing Ansible in conda environment in %s from %s \n\n" "${MINICONDA_INSTALL_DIR}" "${miniconda_url}"

        # Actually install environment and install in base environment
        if [[ ! -f ${MINICONDA_INSTALL_SCRIPT} ]]; then
            wget $miniconda_url -O $MINICONDA_INSTALL_SCRIPT
        fi
        bash $MINICONDA_INSTALL_SCRIPT -b -p $MINICONDA_INSTALL_DIR
        source "${MINICONDA_INSTALL_DIR}/bin/activate"
    else
        printf "Installing Ansible in existing conda environment in %s \n\n" "${MINICONDA_INSTALL_DIR}"
        source "${MINICONDA_INSTALL_DIR}/bin/activate"
    fi

else
    printf "Attempting to install Ansible in base environment\n"
    printf "If this fails, please run this script with the --conda flag\n\n"
fi

# Install Ansible
printf "Installing Ansible\n"
python3 -m pip install -r ${THIS_DIR}/requirements.txt

# Install Ansible collections
printf "Installing Ansible collections\n"
ansible-galaxy collection install -r ${THIS_DIR}/requirements.yml

# Install azhop dependencies
printf "Installing Az-hop dependencies\n"
ansible-playbook ${THIS_DIR}/azhop-dependencies.yml

printf "\n\n"
printf "Applications installed\n"
printf "===============================================================================\n"
columns="%-16s| %.10s\n"
printf "$columns" Application Version
printf -- "-------------------------------------------------------------------------------\n"
printf "$columns" Python `python3 --version | awk '{ print $2 }'`
printf "$columns" Ansible `ansible --version | head -n 1 | awk '{ print $3 }' | sed 's/]//'`
printf "$columns" Terraform `terraform --version | head -n 1 | awk '{ print $2 }' | sed 's/v//'`
printf "$columns" Packer `packer --version`
printf "$columns" az-cli `az --version 2> /dev/null | head -n 1 | awk '{ print $2 }'`
printf "$columns" azcopy `azcopy --version | awk '{ print $3 }'`
printf "$columns" yq `yq --version | awk '{ print $4 }'`
printf "$columns" check-jsonschema `check-jsonschema --version | awk '{ print $3 }'`
printf "===============================================================================\n"

if [ $INSTALL_IN_CONDA = true ]; then
    yellow=$'\e[1;33m'
    default=$'\e[0m'
    printf "\n${yellow}Az-HOP dependencies installed in a conda environment${default}. To activate, run:\n"
    printf "\nsource %s/bin/activate\n\n" "${MINICONDA_INSTALL_DIR}"
fi


#!/bin/bash
# Install basic command-line utilities
yum install -y epel-release

yum install -y \
     curl \
     python3 \
     python3-pip \
     jq \
     wget \
     git \
     yamllint

#
# Install AzCLI
#
echo "Installing AzCLI ..."
rpm --import https://packages.microsoft.com/keys/microsoft.asc
echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | tee /etc/yum.repos.d/azure-cli.repo
yum install -y azure-cli

#
# Install AzCopy
#
echo "Installing AzCopy ..."
cd /usr/local/bin
wget -q https://aka.ms/downloadazcopy-v10-linux -O - | tar zxf - --strip-components 1 --wildcards '*/azcopy'
chmod 755 /usr/local/bin/azcopy
#
# Install Ansible
#
echo "Installing Ansible ..."
yum remove -y ansible
pip3 install setuptools-rust
pip3 install --upgrade pip
pip3 install ansible==4.10.0

echo "Installing Ansible playbooks pre-reqs"
pip3 install pypsrp
pip3 install PySocks

ansible-galaxy collection install ansible.windows
ansible-galaxy collection install community.windows
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install community.general

#
# Install Terraform
#
echo "Installing terraform ..."
#apt update -y && \
#apt install -y software-properties-common && \
yum -y remove terraform
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install terraform

#
# Install Packer
#
echo "Installing packer...."
yum remove packer -y
yum install -y packer

#
# Install yq
#
echo "Installing yq...."
VERSION=v4.13.3
BINARY=yq_linux_amd64
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/bin/yq && chmod +x /usr/bin/yq

# Clean-up
rm -f /tmp/*.zip && rm -f /tmp/*.gz && \

echo "=============="
echo "Python version"
echo "=============="
python3 --version
echo "==============="
echo "Ansible version"
echo "==============="
ansible --version
echo "================="
echo "Terraform version"
echo "================="
terraform --version
echo "=============="
echo "Packer version"
echo "=============="
/usr/bin/packer --version
echo "=========="
echo "AZ version"
echo "=========="
az --version
echo "=========="
echo "AZ Copy version"
echo "=========="
azcopy --version
echo "=========="
echo "yq version"
echo "=========="
yq --version
echo "End"


#!/bin/bash
set -Eeo pipefail

echo "user ${USERNAME}"
echo "password ${SSH_PASSWD}"

# Generate unique ssh keys , if needed
if [ ! -f /home/vscode/.ssh/ssh_host_ecdsa_key ]; then
    ssh-keygen -t ecdsa -b 521 -f /home/vscode/.ssh/ssh_host_ecdsa_key -N ''
fi

echo "${USERNAME}:${SSH_PASSWD}" | sudo chpasswd

sudo /usr/sbin/sshd -f /home/vscode/.ssh/sshd_config -D -e


#!/bin/bash

MYDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOTDIR="$MYDIR/.."

main() {
    bicep_dir=$1
    echo "$bicep_dir/generated"
    echo "$ROOTDIR/internal/azure/generated"
    echo "removing all exist type files..."
    rm -r "$ROOTDIR/internal/azure/generated"
    echo "done"
    echo "copying new type files"
    cp -r "$bicep_dir/generated" "$ROOTDIR/internal/azure/generated"
    echo "done"
    cd $ROOTDIR/internal/azure/generated
    find . -name "*.md" -type f -delete
    find . -name "*.out" -type f -delete
}

main "$@"


#!/bin/bash

touch ~/.gitcookies
chmod 0600 ~/.gitcookies

git config --global http.cookiefile ~/.gitcookies

tr , \\t <<\__END__ >>~/.gitcookies
go.googlesource.com,TRUE,/,TRUE,2147483647,o,git-kt.katbyte.me=1/sEvv4P2NiGofB7kgPV7DBbsV5V8_od3JULgYIyZJnUM
go-review.googlesource.com,TRUE,/,TRUE,2147483647,o,git-kt.katbyte.me=1/sEvv4P2NiGofB7kgPV7DBbsV5V8_od3JULgYIyZJnUM
__END__


#!/usr/bin/env bash

function runTests {
  echo "==> Running Unit Tests..."
  go test -v $TEST "$TESTARGS" -timeout=30s -parallel=20
}

function main {
  runTests
}

main


set -ev

if [[ "$1" == "goveralls" ]]; then
	echo "Testing with goveralls..."
	go get github.com/mattn/goveralls
	$HOME/gopath/bin/goveralls -service=travis-ci
else
	echo "Testing with go test..."
	go test -v ./...
fi


#!/usr/bin/env bash

set -e
echo "" > coverage.txt

for d in $(go list ./... | grep -v vendor); do
    go test -v -race -coverprofile=profile.out -covermode=atomic $d
    if [ -f profile.out ]; then
        cat profile.out >> coverage.txt
        rm profile.out
    fi
done


#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# This script runs or (given -n) prints suggested commands to generate files for
# the Architecture/OS specified by the GOARCH and GOOS environment variables.
# See README.md for more information about how the build system works.

GOOSARCH="${GOOS}_${GOARCH}"

# defaults
mksyscall="go run mksyscall.go"
mkerrors="./mkerrors.sh"
zerrors="zerrors_$GOOSARCH.go"
mksysctl=""
zsysctl="zsysctl_$GOOSARCH.go"
mksysnum=
mktypes=
mkasm=
run="sh"
cmd=""

case "$1" in
-syscalls)
	for i in zsyscall*go
	do
		# Run the command line that appears in the first line
		# of the generated file to regenerate it.
		sed 1q $i | sed 's;^// ;;' | sh > _$i && gofmt < _$i > $i
		rm _$i
	done
	exit 0
	;;
-n)
	run="cat"
	cmd="echo"
	shift
esac

case "$#" in
0)
	;;
*)
	echo 'usage: mkall.sh [-n]' 1>&2
	exit 2
esac

if [[ "$GOOS" = "linux" ]]; then
	# Use the Docker-based build system
	# Files generated through docker (use $cmd so you can Ctl-C the build or run)
	$cmd docker build --tag generate:$GOOS $GOOS
	$cmd docker run --interactive --tty --volume $(cd -- "$(dirname -- "$0")" && /bin/pwd):/build generate:$GOOS
	exit
fi

GOOSARCH_in=syscall_$GOOSARCH.go
case "$GOOSARCH" in
_* | *_ | _)
	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
	exit 1
	;;
aix_ppc)
	mkerrors="$mkerrors -maix32"
	mksyscall="go run mksyscall_aix_ppc.go -aix"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
aix_ppc64)
	mkerrors="$mkerrors -maix64"
	mksyscall="go run mksyscall_aix_ppc64.go -aix"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
darwin_amd64)
	mkerrors="$mkerrors -m64"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	mkasm="go run mkasm_darwin.go"
	;;
darwin_arm64)
	mkerrors="$mkerrors -m64"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	mkasm="go run mkasm_darwin.go"
	;;
dragonfly_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -dragonfly"
	mksysnum="go run mksysnum.go 'https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32"
	mksysnum="go run mksysnum.go 'https://svn.freebsd.org/base/stable/11/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_amd64)
	mkerrors="$mkerrors -m64"
	mksysnum="go run mksysnum.go 'https://svn.freebsd.org/base/stable/11/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_arm)
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -arm"
	mksysnum="go run mksysnum.go 'https://svn.freebsd.org/base/stable/11/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
freebsd_arm64)
	mkerrors="$mkerrors -m64"
	mksysnum="go run mksysnum.go 'https://svn.freebsd.org/base/stable/11/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
netbsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32 -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_arm)
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -netbsd -arm"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
netbsd_arm64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32 -openbsd"
	mksysctl="go run mksysctl_openbsd.go"
	mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd"
	mksysctl="go run mksysctl_openbsd.go"
	mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_arm)
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -openbsd -arm"
	mksysctl="go run mksysctl_openbsd.go"
	mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_arm64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd"
	mksysctl="go run mksysctl_openbsd.go"
	mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_mips64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd"
	mksysctl="go run mksysctl_openbsd.go"
	mksysnum="go run mksysnum.go 'https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
solaris_amd64)
	mksyscall="go run mksyscall_solaris.go"
	mkerrors="$mkerrors -m64"
	mksysnum=
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
illumos_amd64)
        mksyscall="go run mksyscall_solaris.go"
	mkerrors=
	mksysnum=
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
*)
	echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
	exit 1
	;;
esac

(
	if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
	case "$GOOS" in
	*)
		syscall_goos="syscall_$GOOS.go"
		case "$GOOS" in
		darwin | dragonfly | freebsd | netbsd | openbsd)
			syscall_goos="syscall_bsd.go $syscall_goos"
			;;
		esac
		if [ -n "$mksyscall" ]; then
			if [ "$GOOSARCH" == "aix_ppc64" ]; then
				# aix/ppc64 script generates files instead of writing to stdin.
				echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in && gofmt -w zsyscall_$GOOSARCH.go && gofmt -w zsyscall_"$GOOSARCH"_gccgo.go && gofmt -w zsyscall_"$GOOSARCH"_gc.go " ;
			elif [ "$GOOS" == "darwin" ]; then
			        # 1.12 and later, syscalls via libSystem
				echo "$mksyscall -tags $GOOS,$GOARCH,go1.12 $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go";
				# 1.13 and later, syscalls via libSystem (including syscallPtr)
				echo "$mksyscall -tags $GOOS,$GOARCH,go1.13 syscall_darwin.1_13.go |gofmt >zsyscall_$GOOSARCH.1_13.go";
			elif [ "$GOOS" == "illumos" ]; then
			        # illumos code generation requires a --illumos switch
			        echo "$mksyscall -illumos -tags illumos,$GOARCH syscall_illumos.go |gofmt > zsyscall_illumos_$GOARCH.go";
			        # illumos implies solaris, so solaris code generation is also required
				echo "$mksyscall -tags solaris,$GOARCH syscall_solaris.go syscall_solaris_$GOARCH.go |gofmt >zsyscall_solaris_$GOARCH.go";
			else
				echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go";
			fi
		fi
	esac
	if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
	if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
	if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go | go run mkpost.go > ztypes_$GOOSARCH.go"; fi
	if [ -n "$mkasm" ]; then echo "$mkasm $GOARCH"; fi
) | $run


#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# Generate Go code listing errors and other #defined constant
# values (ENAMETOOLONG etc.), by asking the preprocessor
# about the definitions.

unset LANG
export LC_ALL=C
export LC_CTYPE=C

if test -z "$GOARCH" -o -z "$GOOS"; then
	echo 1>&2 "GOARCH or GOOS not defined in environment"
	exit 1
fi

# Check that we are using the new build system if we should
if [[ "$GOOS" = "linux" ]] && [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
	echo 1>&2 "In the Docker based build system, mkerrors should not be called directly."
	echo 1>&2 "See README.md"
	exit 1
fi

if [[ "$GOOS" = "aix" ]]; then
	CC=${CC:-gcc}
else
	CC=${CC:-cc}
fi

if [[ "$GOOS" = "solaris" ]]; then
	# Assumes GNU versions of utilities in PATH.
	export PATH=/usr/gnu/bin:$PATH
fi

uname=$(uname)

includes_AIX='
#include <net/if.h>
#include <net/netopt.h>
#include <netinet/ip_mroute.h>
#include <sys/protosw.h>
#include <sys/stropts.h>
#include <sys/mman.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/termio.h>
#include <termios.h>
#include <fcntl.h>

#define AF_LOCAL AF_UNIX
'

includes_Darwin='
#define _DARWIN_C_SOURCE
#define KERNEL
#define _DARWIN_USE_64_BIT_INODE
#define __APPLE_USE_RFC_3542
#include <stdint.h>
#include <sys/attr.h>
#include <sys/clonefile.h>
#include <sys/kern_control.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/ptrace.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/sockio.h>
#include <sys/sys_domain.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <sys/xattr.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <termios.h>
'

includes_DragonFly='
#include <sys/types.h>
#include <sys/event.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_clone.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <termios.h>
#include <netinet/ip.h>
#include <net/ip_mroute/ip_mroute.h>
'

includes_FreeBSD='
#include <sys/capsicum.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/disk.h>
#include <sys/event.h>
#include <sys/sched.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <termios.h>
#include <netinet/ip.h>
#include <netinet/ip_mroute.h>
#include <sys/extattr.h>

#if __FreeBSD__ >= 10
#define IFT_CARP	0xf8	// IFT_CARP is deprecated in FreeBSD 10
#undef SIOCAIFADDR
#define SIOCAIFADDR	_IOW(105, 26, struct oifaliasreq)	// ifaliasreq contains if_data
#undef SIOCSIFPHYADDR
#define SIOCSIFPHYADDR	_IOW(105, 70, struct oifaliasreq)	// ifaliasreq contains if_data
#endif
'

includes_Linux='
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#ifndef __LP64__
#define _FILE_OFFSET_BITS 64
#endif
#define _GNU_SOURCE

// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
// these structures. We just include them copied from <bits/termios.h>.
#if defined(__powerpc__)
struct sgttyb {
        char    sg_ispeed;
        char    sg_ospeed;
        char    sg_erase;
        char    sg_kill;
        short   sg_flags;
};

struct tchars {
        char    t_intrc;
        char    t_quitc;
        char    t_startc;
        char    t_stopc;
        char    t_eofc;
        char    t_brkc;
};

struct ltchars {
        char    t_suspc;
        char    t_dsuspc;
        char    t_rprntc;
        char    t_flushc;
        char    t_werasc;
        char    t_lnextc;
};
#endif

#include <bits/sockaddr.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/timerfd.h>
#include <sys/uio.h>
#include <sys/xattr.h>
#include <linux/bpf.h>
#include <linux/can.h>
#include <linux/can/error.h>
#include <linux/can/raw.h>
#include <linux/capability.h>
#include <linux/cryptouser.h>
#include <linux/devlink.h>
#include <linux/dm-ioctl.h>
#include <linux/errqueue.h>
#include <linux/ethtool_netlink.h>
#include <linux/falloc.h>
#include <linux/fanotify.h>
#include <linux/filter.h>
#include <linux/fs.h>
#include <linux/fscrypt.h>
#include <linux/fsverity.h>
#include <linux/genetlink.h>
#include <linux/hdreg.h>
#include <linux/hidraw.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/if.h>
#include <linux/if_addr.h>
#include <linux/if_alg.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_ppp.h>
#include <linux/if_tun.h>
#include <linux/if_packet.h>
#include <linux/if_xdp.h>
#include <linux/input.h>
#include <linux/kexec.h>
#include <linux/keyctl.h>
#include <linux/loop.h>
#include <linux/lwtunnel.h>
#include <linux/magic.h>
#include <linux/memfd.h>
#include <linux/module.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netlink.h>
#include <linux/net_namespace.h>
#include <linux/nfc.h>
#include <linux/nsfs.h>
#include <linux/perf_event.h>
#include <linux/pps.h>
#include <linux/ptrace.h>
#include <linux/random.h>
#include <linux/reboot.h>
#include <linux/rtc.h>
#include <linux/rtnetlink.h>
#include <linux/sched.h>
#include <linux/seccomp.h>
#include <linux/serial.h>
#include <linux/sockios.h>
#include <linux/taskstats.h>
#include <linux/tipc.h>
#include <linux/vm_sockets.h>
#include <linux/wait.h>
#include <linux/watchdog.h>

#include <mtd/ubi-user.h>
#include <mtd/mtd-user.h>
#include <net/route.h>

#if defined(__sparc__)
// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
// definition in glibc. As only the error constants are needed here, include the
// generic termibits.h (which is included by termbits.h on sparc).
#include <asm-generic/termbits.h>
#else
#include <asm/termbits.h>
#endif

#ifndef MSG_FASTOPEN
#define MSG_FASTOPEN    0x20000000
#endif

#ifndef PTRACE_GETREGS
#define PTRACE_GETREGS	0xc
#endif

#ifndef PTRACE_SETREGS
#define PTRACE_SETREGS	0xd
#endif

#ifndef SOL_NETLINK
#define SOL_NETLINK	270
#endif

#ifdef SOL_BLUETOOTH
// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
// but it is already in bluetooth_linux.go
#undef SOL_BLUETOOTH
#endif

// Certain constants are missing from the fs/crypto UAPI
#define FS_KEY_DESC_PREFIX              "fscrypt:"
#define FS_KEY_DESC_PREFIX_SIZE         8
#define FS_MAX_KEY_SIZE                 64

// The code generator produces -0x1 for (~0), but an unsigned value is necessary
// for the tipc_subscr timeout __u32 field.
#undef TIPC_WAIT_FOREVER
#define TIPC_WAIT_FOREVER 0xffffffff

// Copied from linux/l2tp.h
// Including linux/l2tp.h here causes conflicts between linux/in.h
// and netinet/in.h included via net/route.h above.
#define IPPROTO_L2TP		115

// Copied from linux/hid.h.
// Keep in sync with the size of the referenced fields.
#define _HIDIOCGRAWNAME_LEN	128 // sizeof_field(struct hid_device, name)
#define _HIDIOCGRAWPHYS_LEN	64  // sizeof_field(struct hid_device, phys)
#define _HIDIOCGRAWUNIQ_LEN	64  // sizeof_field(struct hid_device, uniq)

#define _HIDIOCGRAWNAME		HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
#define _HIDIOCGRAWPHYS		HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
#define _HIDIOCGRAWUNIQ		HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)

'

includes_NetBSD='
#include <sys/types.h>
#include <sys/param.h>
#include <sys/event.h>
#include <sys/extattr.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/sched.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/termios.h>
#include <sys/ttycom.h>
#include <sys/wait.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_mroute.h>
#include <netinet/if_ether.h>

// Needed since <sys/param.h> refers to it...
#define schedppq 1
'

includes_OpenBSD='
#include <sys/types.h>
#include <sys/param.h>
#include <sys/event.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/select.h>
#include <sys/sched.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/termios.h>
#include <sys/ttycom.h>
#include <sys/unistd.h>
#include <sys/wait.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_mroute.h>
#include <netinet/if_ether.h>
#include <net/if_bridge.h>

// We keep some constants not supported in OpenBSD 5.5 and beyond for
// the promise of compatibility.
#define EMUL_ENABLED		0x1
#define EMUL_NATIVE		0x2
#define IPV6_FAITH		0x1d
#define IPV6_OPTIONS		0x1
#define IPV6_RTHDR_STRICT	0x1
#define IPV6_SOCKOPT_RESERVED1	0x3
#define SIOCGIFGENERIC		0xc020693a
#define SIOCSIFGENERIC		0x80206939
#define WALTSIG			0x4
'

includes_SunOS='
#include <limits.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/stat.h>
#include <sys/stream.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/mkdev.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_mroute.h>
#include <termios.h>
'

includes='
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <sys/signal.h>
#include <signal.h>
#include <sys/resource.h>
#include <time.h>
'
ccflags="$@"

# Write go tool cgo -godefs input.
(
	echo package unix
	echo
	echo '/*'
	indirect="includes_$(uname)"
	echo "${!indirect} $includes"
	echo '*/'
	echo 'import "C"'
	echo 'import "syscall"'
	echo
	echo 'const ('

	# The gcc command line prints all the #defines
	# it encounters while processing the input
	echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
	awk '
		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}

		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
		$2 ~ /^(SCM_SRCRT)$/ {next}
		$2 ~ /^(MAP_FAILED)$/ {next}
		$2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.

		$2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
		$2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}

		$2 !~ /^ECCAPBITS/ &&
		$2 !~ /^ETH_/ &&
		$2 !~ /^EPROC_/ &&
		$2 !~ /^EQUIV_/ &&
		$2 !~ /^EXPR_/ &&
		$2 !~ /^EVIOC/ &&
		$2 !~ /^EV_/ &&
		$2 ~ /^E[A-Z0-9_]+$/ ||
		$2 ~ /^B[0-9_]+$/ ||
		$2 ~ /^(OLD|NEW)DEV$/ ||
		$2 == "BOTHER" ||
		$2 ~ /^CI?BAUD(EX)?$/ ||
		$2 == "IBSHIFT" ||
		$2 ~ /^V[A-Z0-9]+$/ ||
		$2 ~ /^CS[A-Z0-9]/ ||
		$2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
		$2 ~ /^IGN/ ||
		$2 ~ /^IX(ON|ANY|OFF)$/ ||
		$2 ~ /^IN(LCR|PCK)$/ ||
		$2 !~ "X86_CR3_PCID_NOFLUSH" &&
		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
		$2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
		$2 == "BRKINT" ||
		$2 == "HUPCL" ||
		$2 == "PENDIN" ||
		$2 == "TOSTOP" ||
		$2 == "XCASE" ||
		$2 == "ALTWERASE" ||
		$2 == "NOKERNINFO" ||
		$2 == "NFDBITS" ||
		$2 ~ /^PAR/ ||
		$2 ~ /^SIG[^_]/ ||
		$2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
		$2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
		$2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
		$2 ~ /^O?XTABS$/ ||
		$2 ~ /^TC[IO](ON|OFF)$/ ||
		$2 ~ /^IN_/ ||
		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
		$2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
		$2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|MCAST|EVFILT|NOTE|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR|LOCAL)_/ ||
		$2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
		$2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
		$2 ~ /^RAW_PAYLOAD_/ ||
		$2 ~ /^TP_STATUS_/ ||
		$2 ~ /^FALLOC_/ ||
		$2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
		$2 == "SOMAXCONN" ||
		$2 == "NAME_MAX" ||
		$2 == "IFNAMSIZ" ||
		$2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
		$2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
		$2 ~ /^HW_MACHINE$/ ||
		$2 ~ /^SYSCTL_VERS/ ||
		$2 !~ "MNT_BITS" &&
		$2 ~ /^(MS|MNT|UMOUNT)_/ ||
		$2 ~ /^NS_GET_/ ||
		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
		$2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|TFD)_/ ||
		$2 ~ /^KEXEC_/ ||
		$2 ~ /^LINUX_REBOOT_CMD_/ ||
		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
		$2 ~ /^MODULE_INIT_/ ||
		$2 !~ "NLA_TYPE_MASK" &&
		$2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
		$2 ~ /^FIORDCHK$/ ||
		$2 ~ /^SIOC/ ||
		$2 ~ /^TIOC/ ||
		$2 ~ /^TCGET/ ||
		$2 ~ /^TCSET/ ||
		$2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
		$2 !~ "RTF_BITS" &&
		$2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
		$2 ~ /^BIOC/ ||
		$2 ~ /^DIOC/ ||
		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
		$2 ~ /^CLONE_[A-Z_]+/ ||
		$2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
		$2 ~ /^(BPF|DLT)_/ ||
		$2 ~ /^(CLOCK|TIMER)_/ ||
		$2 ~ /^CAN_/ ||
		$2 ~ /^CAP_/ ||
		$2 ~ /^CP_/ ||
		$2 ~ /^CPUSTATES$/ ||
		$2 ~ /^CTLIOCGINFO$/ ||
		$2 ~ /^ALG_/ ||
		$2 ~ /^FI(CLONE|DEDUPERANGE)/ ||
		$2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
		$2 ~ /^FS_IOC_.*(ENCRYPTION|VERITY|[GS]ETFLAGS)/ ||
		$2 ~ /^FS_VERITY_/ ||
		$2 ~ /^FSCRYPT_/ ||
		$2 ~ /^DM_/ ||
		$2 ~ /^GRND_/ ||
		$2 ~ /^RND/ ||
		$2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
		$2 ~ /^KEYCTL_/ ||
		$2 ~ /^PERF_/ ||
		$2 ~ /^SECCOMP_MODE_/ ||
		$2 ~ /^SEEK_/ ||
		$2 ~ /^SPLICE_/ ||
		$2 ~ /^SYNC_FILE_RANGE_/ ||
		$2 !~ /^AUDIT_RECORD_MAGIC/ &&
		$2 !~ /IOC_MAGIC/ &&
		$2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
		$2 ~ /^(VM|VMADDR)_/ ||
		$2 ~ /^IOCTL_VM_SOCKETS_/ ||
		$2 ~ /^(TASKSTATS|TS)_/ ||
		$2 ~ /^CGROUPSTATS_/ ||
		$2 ~ /^GENL_/ ||
		$2 ~ /^STATX_/ ||
		$2 ~ /^RENAME/ ||
		$2 ~ /^UBI_IOC[A-Z]/ ||
		$2 ~ /^UTIME_/ ||
		$2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
		$2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
		$2 ~ /^FSOPT_/ ||
		$2 ~ /^WDIO[CFS]_/ ||
		$2 ~ /^NFN/ ||
		$2 ~ /^XDP_/ ||
		$2 ~ /^RWF_/ ||
		$2 ~ /^(HDIO|WIN|SMART)_/ ||
		$2 ~ /^CRYPTO_/ ||
		$2 ~ /^TIPC_/ ||
		$2 !~  "DEVLINK_RELOAD_LIMITS_VALID_MASK" &&
		$2 ~ /^DEVLINK_/ ||
		$2 ~ /^ETHTOOL_/ ||
		$2 ~ /^LWTUNNEL_IP/ ||
		$2 !~ "WMESGLEN" &&
		$2 ~ /^W[A-Z0-9]+$/ ||
		$2 ~/^PPPIOC/ ||
		$2 ~ /^FAN_|FANOTIFY_/ ||
		$2 == "HID_MAX_DESCRIPTOR_SIZE" ||
		$2 ~ /^_?HIDIOC/ ||
		$2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
		$2 ~ /^MTD/ ||
		$2 ~ /^OTP/ ||
		$2 ~ /^MEM/ ||
		$2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
		$2 ~ /^__WCOREFLAG$/ {next}
		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}

		{next}
	' | sort

	echo ')'
) >_const.go

# Pull out the error names for later.
errors=$(
	echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
	sort
)

# Pull out the signal names for later.
signals=$(
	echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
	sort
)

# Again, writing regexps to a file.
echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
	sort >_error.grep
echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
	sort >_signal.grep

echo '// mkerrors.sh' "$@"
echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
echo
echo "//go:build ${GOARCH} && ${GOOS}"
echo "// +build ${GOARCH},${GOOS}"
echo
go tool cgo -godefs -- "$@" _const.go >_error.out
cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
echo
echo '// Errors'
echo 'const ('
cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
echo ')'

echo
echo '// Signals'
echo 'const ('
cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
echo ')'

# Run C program to print error and syscall strings.
(
	echo -E "
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>

#define nelem(x) (sizeof(x)/sizeof((x)[0]))

enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below

struct tuple {
	int num;
	const char *name;
};

struct tuple errors[] = {
"
	for i in $errors
	do
		echo -E '	{'$i', "'$i'" },'
	done

	echo -E "
};

struct tuple signals[] = {
"
	for i in $signals
	do
		echo -E '	{'$i', "'$i'" },'
	done

	# Use -E because on some systems bash builtin interprets \n itself.
	echo -E '
};

static int
tuplecmp(const void *a, const void *b)
{
	return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
}

int
main(void)
{
	int i, e;
	char buf[1024], *p;

	printf("\n\n// Error table\n");
	printf("var errorList = [...]struct {\n");
	printf("\tnum  syscall.Errno\n");
	printf("\tname string\n");
	printf("\tdesc string\n");
	printf("} {\n");
	qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
	for(i=0; i<nelem(errors); i++) {
		e = errors[i].num;
		if(i > 0 && errors[i-1].num == e)
			continue;
		strcpy(buf, strerror(e));
		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
			buf[0] += a - A;
		printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
	}
	printf("}\n\n");

	printf("\n\n// Signal table\n");
	printf("var signalList = [...]struct {\n");
	printf("\tnum  syscall.Signal\n");
	printf("\tname string\n");
	printf("\tdesc string\n");
	printf("} {\n");
	qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
	for(i=0; i<nelem(signals); i++) {
		e = signals[i].num;
		if(i > 0 && signals[i-1].num == e)
			continue;
		strcpy(buf, strsignal(e));
		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
			buf[0] += a - A;
		// cut trailing : number.
		p = strrchr(buf, ":"[0]);
		if(p)
			*p = '\0';
		printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
	}
	printf("}\n\n");

	return 0;
}

'
) >_errors.c

$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out


#!/bin/bash
set -e

version=${1:?"version not specified"}

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
rpm --import https://packages.microsoft.com/keys/microsoft.asc
dnf install -y https://packages.microsoft.com/config/centos/8/packages-microsoft-prod.rpm

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    # See: https://access.redhat.com/solutions/2779441
    dnf check-update --refresh || [[ $? == 100 ]]
    dnf install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

apt-get update
apt-get install -y curl software-properties-common gpg
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
apt-add-repository https://packages.microsoft.com/debian/11/prod

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    apt-get update
    apt-get install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

apt-get update
apt-get install -y curl software-properties-common gpg
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
apt-add-repository https://packages.microsoft.com/debian/10/prod

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    apt-get update
    apt-get install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

rpm --import https://packages.microsoft.com/keys/microsoft.asc

# To accelarate syncing the repo metadatas
rm -f /etc/yum.repos.d/*

dnf install -y https://packages.microsoft.com/config/fedora/34/packages-microsoft-prod.rpm

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    # See: https://access.redhat.com/solutions/2779441
    dnf check-update --refresh || [[ $? == 100 ]]
    dnf install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

rpm --import https://packages.microsoft.com/keys/microsoft.asc

# To accelarate syncing the repo metadatas
rm -f /etc/yum.repos.d/*

dnf install -y https://packages.microsoft.com/config/fedora/35/packages-microsoft-prod.rpm

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    # See: https://access.redhat.com/solutions/2779441
    dnf check-update --refresh || [[ $? == 100 ]]
    dnf install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

rpm --import https://packages.microsoft.com/keys/microsoft.asc

# To accelarate syncing the repo metadatas
rm -f /etc/yum.repos.d/*

dnf install -y https://packages.microsoft.com/config/fedora/36/packages-microsoft-prod.rpm

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    # See: https://access.redhat.com/solutions/2779441
    dnf check-update --refresh || [[ $? == 100 ]]
    dnf install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

apt-get update
apt-get install -y curl software-properties-common gpg
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
apt-add-repository https://packages.microsoft.com/ubuntu/18.04/multiarch/prod

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    apt-get update
    apt-get install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

apt-get update
apt-get install -y curl software-properties-common gpg
curl -sSL https://packages.microsoft.com/keys/microsoft.asc > /etc/apt/trusted.gpg.d/microsoft.asc
apt-add-repository https://packages.microsoft.com/ubuntu/20.04/prod

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    apt-get update
    apt-get install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done
(( count <= total ))

grep $version <(azapi2azurerm -v)


#!/bin/bash
set -e

version=${1:?"version not specified"}

apt-get update
apt-get install -y curl software-properties-common gpg
curl -sSL https://packages.microsoft.com/keys/microsoft.asc > /etc/apt/trusted.gpg.d/microsoft.asc
apt-add-repository https://packages.microsoft.com/ubuntu/22.04/prod

total=60
count=1
while ((count <= total)); do
    echo "Try ($count/$total)"
    apt-get update
    apt-get install -y azapi2azurerm && break

    sleep 1m
    ((count++))
done

grep $version <(azapi2azurerm -v)


#!/bin/bash
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

today=$(date +%Y%m%d)

sed -i -r -e 's/const Repo = "([0-9]{8})"/const Repo = "'$today'"/' $GOFILE


#!/usr/bin/env bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# This script runs or (given -n) prints suggested commands to generate files for
# the Architecture/OS specified by the GOARCH and GOOS environment variables.
# See README.md for more information about how the build system works.

GOOSARCH="${GOOS}_${GOARCH}"

# defaults
mksyscall="go run mksyscall.go"
mkerrors="./mkerrors.sh"
zerrors="zerrors_$GOOSARCH.go"
mksysctl=""
zsysctl="zsysctl_$GOOSARCH.go"
mksysnum=
mktypes=
mkasm=
run="sh"
cmd=""

case "$1" in
-syscalls)
	for i in zsyscall*go
	do
		# Run the command line that appears in the first line
		# of the generated file to regenerate it.
		sed 1q $i | sed 's;^// ;;' | sh > _$i && gofmt < _$i > $i
		rm _$i
	done
	exit 0
	;;
-n)
	run="cat"
	cmd="echo"
	shift
esac

case "$#" in
0)
	;;
*)
	echo 'usage: mkall.sh [-n]' 1>&2
	exit 2
esac

if [[ "$GOOS" = "linux" ]]; then
	# Use the Docker-based build system
	# Files generated through docker (use $cmd so you can Ctl-C the build or run)
	$cmd docker build --tag generate:$GOOS $GOOS
	$cmd docker run --interactive --tty --volume $(cd -- "$(dirname -- "$0")/.." && pwd):/build generate:$GOOS
	exit
fi

GOOSARCH_in=syscall_$GOOSARCH.go
case "$GOOSARCH" in
_* | *_ | _)
	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
	exit 1
	;;
aix_ppc)
	mkerrors="$mkerrors -maix32"
	mksyscall="go run mksyscall_aix_ppc.go -aix"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
aix_ppc64)
	mkerrors="$mkerrors -maix64"
	mksyscall="go run mksyscall_aix_ppc64.go -aix"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
darwin_amd64)
	mkerrors="$mkerrors -m64"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	mkasm="go run mkasm.go"
	;;
darwin_arm64)
	mkerrors="$mkerrors -m64"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	mkasm="go run mkasm.go"
	;;
dragonfly_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -dragonfly"
	mksysnum="go run mksysnum.go 'https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32"
	mksysnum="go run mksysnum.go 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_amd64)
	mkerrors="$mkerrors -m64"
	mksysnum="go run mksysnum.go 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_arm)
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -arm"
	mksysnum="go run mksysnum.go 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
freebsd_arm64)
	mkerrors="$mkerrors -m64"
	mksysnum="go run mksysnum.go 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
freebsd_riscv64)
	mkerrors="$mkerrors -m64"
	mksysnum="go run mksysnum.go 'https://cgit.freebsd.org/src/plain/sys/kern/syscalls.master?h=stable/12'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
netbsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32 -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_arm)
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -netbsd -arm"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
netbsd_arm64)
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -netbsd"
	mksysnum="go run mksysnum.go 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master'"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_386)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m32"
	mksyscall="go run mksyscall.go -l32 -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_amd64)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_arm)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors"
	mksyscall="go run mksyscall.go -l32 -openbsd -arm -libc"
	mksysctl="go run mksysctl_openbsd.go"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_arm64)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_mips64)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_ppc64)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
openbsd_riscv64)
	mkasm="go run mkasm.go"
	mkerrors="$mkerrors -m64"
	mksyscall="go run mksyscall.go -openbsd -libc"
	mksysctl="go run mksysctl_openbsd.go"
	# Let the type of C char be signed for making the bare syscall
	# API consistent across platforms.
	mktypes="GOARCH=$GOARCH go tool cgo -godefs -- -fsigned-char"
	;;
solaris_amd64)
	mksyscall="go run mksyscall_solaris.go"
	mkerrors="$mkerrors -m64"
	mksysnum=
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
illumos_amd64)
        mksyscall="go run mksyscall_solaris.go"
	mkerrors=
	mksysnum=
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
*)
	echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
	exit 1
	;;
esac

(
	if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
	case "$GOOS" in
	*)
		syscall_goos="syscall_$GOOS.go"
		case "$GOOS" in
		darwin | dragonfly | freebsd | netbsd | openbsd)
			syscall_goos="syscall_bsd.go $syscall_goos"
			;;
		esac
		if [ -n "$mksyscall" ]; then
			if [ "$GOOSARCH" == "aix_ppc64" ]; then
				# aix/ppc64 script generates files instead of writing to stdin.
				echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in && gofmt -w zsyscall_$GOOSARCH.go && gofmt -w zsyscall_"$GOOSARCH"_gccgo.go && gofmt -w zsyscall_"$GOOSARCH"_gc.go " ;
			elif [ "$GOOS" == "illumos" ]; then
			        # illumos code generation requires a --illumos switch
			        echo "$mksyscall -illumos -tags illumos,$GOARCH syscall_illumos.go |gofmt > zsyscall_illumos_$GOARCH.go";
			        # illumos implies solaris, so solaris code generation is also required
				echo "$mksyscall -tags solaris,$GOARCH syscall_solaris.go syscall_solaris_$GOARCH.go |gofmt >zsyscall_solaris_$GOARCH.go";
			else
				echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go";
			fi
		fi
	esac
	if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
	if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
	if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go | go run mkpost.go > ztypes_$GOOSARCH.go"; fi
	if [ -n "$mkasm" ]; then echo "$mkasm $GOOS $GOARCH"; fi
) | $run