#!/bin/sh

if [ "$(id -u)" -ne 0 ]; then
   echo "This script must be run as root"
   exit 1
fi

if [ "$1" != "enable" ] && [ "$1" != "disable" ]; then
   echo "usage: amazon-linux-https [ enable | disable ]"
   exit 1
fi

region=$(head -n 1 /etc/yum/vars/awsregion)
domain=$(head -n 1 /etc/yum/vars/awsdomain)

if [ "$1" = "enable" ]; then
   echo "https" > /etc/yum/vars/awsproto
   if [ "$region" = "default" ]; then
      echo "amazonlinux" > /etc/yum/vars/amazonlinux
      echo "amazonlinux.com" > /etc/yum/vars/awsdomain
   else
      if [ "$domain" = "amazonaws.com" ] || [ "$domain" = "amazonaws.com.cn" ]; then
         echo "amazonlinux-2-repos-$region.s3.dualstack" > /etc/yum/vars/amazonlinux
      else
         echo "amazonlinux-2-repos-$region.s3" > /etc/yum/vars/amazonlinux
      fi
   fi
elif [ "$1" = "disable" ]; then
   echo "http" > /etc/yum/vars/awsproto
   echo "amazonlinux" > /etc/yum/vars/amazonlinux
   if [ "$region" = "default" ]; then
      echo "amazonaws.com" > /etc/yum/vars/awsdomain
   fi
fi

