#!/bin/sh
REPO=http://repo.postgrespro.ru/pg1c-archive/pg1c-12.6
PRODUCT_NAME="PostgreSQL for 1C 12"
if [ $(id -u) -ne 0 ]; then
	echo "This script should be run as root, because it updates apt configuration"
	exit 1
fi
listname=$(basename $REPO)
if [ -f /etc/apt/sources.list.d/${listname}.list ]; then
	echo "You have already added repository for $PRODUCT_NAME to your system."
	echo "To upgrade your $PRODUCT_NAME packages use apt-get install or"
	echo "apt-get upgrade command."
	echo "If you are sure that you want to replace repository configuration,"
	echo "remove /etc/apt/sources.list.d/${listname}.list and run this script again."
	exit 1
fi
. /etc/os-release
case "$ID" in
debian|ubuntu)
	top=$REPO/$ID
	distr=$VERSION_CODENAME
	if [ $(dpkg --print-architecture) = "i386" ]; then
		echo "32-bit ${NAME} systems are not supported by ${PRODUCT_NAME}" >&2
		exit 1
	fi
	;;
astra)
	case "$VERSION_ID" in
	*.*.*) distver=${VERSION_ID%.*}
		;;
	*.*) distver=${VERSION_ID}	
		;;
        esac	
	top=$REPO/$ID-$VARIANT_ID/$distver
	distr=$VARIANT_ID
	;;
osnova)
	top=$REPO/$ID
	distr=$VARIANT_ID
	;;
*)	echo "Unsupported debian-like distribution '$NAME'" 1>&2
	exit 1
	;;
esac
if [ -x /usr/bin/wget ]; then
	# Checking existence of repository
	exitcode=99
	while [ $exitcode -ne 0 ]; do
	wget -O - ${USER:+--user="$USER" --password="$PASSWORD"} $top/dists/$distr/InRelease >/dev/null
	exitcode=$?
	case "$exitcode" in
	8)
		echo "Version ${VERSION_ID} of ${NAME} distribution is not supported" >&2
		exit 1
	;;
	6) echo "Repository $REPO is password protected" >&2
	   echo 'Please enter your username: \c'
	   read USER
	   echo "Please enter your password (wouldn't be echoed): \\c"
	   stty -echo
	   read PASSWORD
	   echo ""
	   stty echo
	   ;;
	
	0) # repository testing is successful
	   :
	esac
	done
else
	echo "WARNING: wget program not found. Cannot check repository access."
	echo "If you have login/password to access repository, please write"
	echo "them into /etc/apt/auth.conf by hand. See apt_auth.conf(5)"
	echo "Press ENTER to continue, Ctrl-C to abort"
	read answer
fi

echo "# Repositiory for '$PRODUCT_NAME'" > "/etc/apt/sources.list.d/$listname.list"
echo "deb $top $distr main" >> "/etc/apt/sources.list.d/$listname.list"
if [ -n "$USER" ]; then
	repohost=${REPO#*://}
	repohost=${repohost%%/*}
	cat > /etc/apt/auth.conf.d/${repohost}.conf <<EOF
machine ${repohost}
login $USER
password $PASSWORD
EOF
	chmod 0600 /etc/apt/auth.conf.d/${repohost}.conf
    echo "Your username/password are saved to /etc/apt/auth.conf.d/${repohost}.conf"
fi
apt-key add - << END-OF-KEY
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQENBFWdEjABCAC6QeLt0UJUQlDI2Z+R/y1OyOMU+5Te176I0+/Xpc2v5NsucW2M
kLTdOif0iW+q5h1djL+Pc5yu1fojZCvcihhbURnWECF52BmRnOC9jI0eTHq3fcPZ
IE3gqMJSn5sx2kJZ7n8XE0RbQ/hr51BLI+lzeqR3JAKBIqpVDKRrdP9Y1xVR/7Ne
q4FNR+osm6W4sM9G+TA/YADrWX3/TPXA4AN+2uNCNY0wK7em8V0oSZJVpEzvu5EP
djC6GX08XSvhPNo52o3u3tpFWH7ICw2BEYe672bJTjmi8wFgPW04pw49Jpvw4i1R
RhkpQqQ/b9bSveoNpvN32ElAJSaize76+q/TABEBAAG0KlJvYm90IChTaWduaW5n
IHJlcG9zKSA8ZGJhQHBvc3RncmVzcHJvLnJ1PokBOAQTAQIAIgUCVZ0SMAIbAwYL
CQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQf5rlpi0t8LQpKQgAuJkOKNdnCSCt
GbNTwAbk414UPYa2B1M1DD6MfcSd6NnJNBVtRoaSWWISQB6gP+/w1jmD8XZbj/oH
5HAHjOyh9Lb3z1xeMIQnBnfGtcqmU5QrF55Yi0H9G0s+fn9oodfNXqAa/zARpBw6
q3LRSBCjT50/XA5G3AzUr7fIDb68FmEOCQukzs0uWBr5fkrRC21b1DcuhzbBay8X
pnlpB+Ma1PTIFgRdRl/KwYTzO80TWFMCeYfXQRh8StuQxRcVCqnv4F6seHqmbL7A
vOZ7GMymsz/IRHGVk4eVC6/94Y3vkV/0eQ+Yom+NtAFnep6G4OhxIeviZ697eFYF
+j4YsyDD+g==
=Q7MS
-----END PGP PUBLIC KEY BLOCK-----
END-OF-KEY
if [ "$?" -ne "0" ]; then
	echo "Error adding key " >&2
	exit 1;
fi
apt-get update
