#!/bin/bash

#sanify check

if [ $# -lt 1 ] ; then
	echo "usage $0 ROOTDIR"
	exit 1
fi

rootdir=$1

if [ -z "$rootdir" ]; then
	echo "ROOTDIR is not set."
	exit 1
fi

# Check root directory
if [ ! -d $rootdir ]; then
	echo "$rootdir: should be a directory."
	exit 1
fi

pushd $rootdir > /dev/null

[ -f etc/localtime ] && rm -f etc/localtime
ln -s ../usr/share/zoneinfo/Host etc/localtime
cp -fp /etc/localtime usr/share/zoneinfo/Host


# read repo keys
SLES_KEY_REL=`cat /etc/vztt/url.map | grep '^\$SLES_KEY_REL' | awk '{print $2}' 2>/dev/null`
SLES_KEY_UPD=`cat /etc/vztt/url.map | grep '^\$SLES_KEY_UPD' | awk '{print $2}' 2>/dev/null`

# Create update files for zypper
echo "[SUSE_Linux_Enterprise_Server_12_SP1_x86_64:SLES12-SP1-Pool]
name=SLES12-SP1-Pool
enabled=1
autorefresh=0
baseurl=https://updates.suse.com/SUSE/Products/SLE-SERVER/12-SP1/x86_64/product?$SLES_KEY_REL
type=rpm-md
keeppackages=0
" > etc/zypp/repos.d/SLES12.1-Base-Pool.repo

echo "[SUSE_Linux_Enterprise_Server_12_SP1_x86_64:SLES12-SP1-Updates]
name=SLES12-SP1-Updates
enabled=1
autorefresh=0
baseurl=https://updates.suse.com/SUSE/Updates/SLE-SERVER/12-SP1/x86_64/update?$SLES_KEY_UPD
type=rpm-md
keeppackages=0
" > etc/zypp/repos.d/Updates-SLES12.1-Base-Pool.repo

# Install certificate
cat /vz/template/sles/12/x86_64/config/os/default/files/244b5494.0 > $rootdir/var/lib/ca-certificates/pem/244b5494.0

popd > /dev/null
