#!/bin/sh

#########################################################################################################################
##
## Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##
#########################################################################################################################

set -eu

TEMPDIR="$(mktemp -d -p /tmp)"
DESTDIR="/usr/share/PolicyDefinitions/"

#SOURCE_URL="https://download.microsoft.com/download/6/e/a/6ea1bfb3-4900-4e65-b52f-6bf8c05ba872/preferences.msi"
#SOURCE_URL="https://download.microsoft.com/download/6/e/a/6ea1bfb3-4900-4e65-b52f-6bf8c05ba872/2008ADMX.msi"
#SOURCE_URL="https://download.microsoft.com/download/7/F/5/7F5E839D-8254-46D6-B9E0-F1B555AFA48B/Windows8.1-Server2012R2ADMX-RTM.msi"
#SOURCE_URL="https://download.microsoft.com/download/5/4/3/543EFACB-2A3F-4D68-AC67-10DE7C4E0B36/WindowsServer2016_TP5_ADMX.msi"
#SOURCE_URL="https://download.microsoft.com/download/3/0/6/30680643-987a-450c-b906-a455fff4aee8/Administrative%20Templates%20(.admx)%20for%20Windows%2010%20October%202020%20Update.msi"
#SOURCE_URL="https://download.microsoft.com/download/a/b/a/aba58fd9-64dc-4416-aa1e-40bcb270f649/Administrative%20Templates%20(.admx)%20for%20Windows%20Server%202022%20August%202021%20Update.msi"
#SOURCE_URL="https://download.microsoft.com/download/a/6/4/a644f64b-6dde-49dc-b775-85665c81ea89/Administrative%20Templates%20(.admx)%20for%20Windows%2011%20October%202021%20Update.msi"
#SOURCE_URL="https://download.microsoft.com/download/8/d/d/8ddd685d-7d55-42e2-9555-6ab365050734/Administrative%20Templates%20(.admx)%20for%20Windows%2011%20September%202022%20Update.msi"

# Administrative Templates (.admx) for Windows 10 2022 Update (22H2)
SOURCE_URL="https://download.microsoft.com/download/c/3/c/c3cd85c0-0785-4cf7-a48e-cdc9b8e20108/Administrative%20Templates%20(.admx)%20for%20Windows%2010%20October%202022%20Update.msi"

# Administrative Templates (.admx) for Windows 11 2023 Update (23H2)
#SOURCE_URL="https://download.microsoft.com/download/b/e/e/bee408b9-d574-4c96-a1a6-45648d5565bf/Administrative%20Templates%20(.admx)%20for%20Windows%2011%20October%202023%20Update.msi"

# Administrative Templates (.admx) for Windows 10 2022 Update (22H2)
#SOURCE_URL="https://download.microsoft.com/download/c/3/c/c3cd85c0-0785-4cf7-a48e-cdc9b8e20108/Administrative%20Templates%20(.admx)%20for%20Windows%2010%20October%202022%20Update.msi"

PROG="${0##*/}"
PROG_VERSION='0.1.1'

SHORT_OPTIONS=':d:hv-:s:'

cleanup()
{
	test -d "$TEMPDIR" || return
	if mountpoint -q "$TEMPDIR"; then
		echo "Skip removing '$TEMPDIR' as mountpoint."
		return
	fi
	echo "Removing admx-msi-setup temporary files..."
	rm -rf "$TEMPDIR"
}

trap cleanup EXIT INT HUP

show_help()
{
	cat <<EOF
$PROG - download msi files and extract them in <destination-directory> default value is $DESTDIR.
Usage: $PROG [-d <destination-directory>] [-s <admx-msi-source>]

EOF
	exit
}

show_version()
{
	cat <<EOF
$PROG version $PROG_VERSION

EOF
	exit
}

while getopts "${SHORT_OPTIONS}" o; do
	    case "${o}" in
		h) show_help
			;;
		v) show_version
			;;
		d) DESTDIR="${OPTARG}"
			;;
		s) SOURCE_URL="${OPTARG}"
			;;
		*) echo "Unrecognized option: $1"; exit 3
			;;
	    esac
done

download_files()
{
  wget -N -q --tries=10 "$SOURCE_URL" -O "$TEMPDIR/package.msi"
}

extract_files()
{
  msiextract "$TEMPDIR/package.msi" -C "$TEMPDIR"
  SOURCEDIR=$(find "$TEMPDIR" -type d -name "PolicyDefinitions" -print | head -n 1)
  if [ -z "$SOURCEDIR" ]; then
      echo "Policy definitions not found in package!"
      exit 1
  else
      cd "$SOURCEDIR"
      mkdir -p "${DESTDIR}"
      cp -r -- * "${DESTDIR}"
  fi
}

main() 
{
	download_files
	extract_files
}

main
