#!/usr/bin/env bash
echo "Run checkstyle script"

# run gofmt
echo "Run 'gofmt'"
unformatted=$(gofmt -l `pwd`/src/)
if [[ -n $unformatted ]]; then
	echo >&2 "Error: Found files not formatted by gofmt"
	for fi in $unformatted; do
		echo >&2 $fi
	done
	echo "Please run 'gofmt -w' for files listed."
	exit 1
fi

# run goimports
echo "Try update 'goimports'"
GOPATH=`pwd`/Tools go get golang.org/x/tools/cmd/goimports

echo "Run 'goimports'"
unformatted=$(Tools/bin/goimports -l `pwd`/src/)
if [[ -n $unformatted ]]; then
	echo >&2 "Error: Found files not formatted by goimports"
	for f in $unformatted; do
		echo >&2 $f
	done
	echo "Please run 'goimports -w' for files listed."
	exit 1
fi

echo "Run 'go vet'"
ln -s `pwd` `pwd`/vendor/src/github.com/aws/session-manager-plugin
go vet ./src/...


#!/usr/bin/env bash
echo "**********************************************"
echo "Creating bundle zip file Mac OS X amd64 Plugin"
echo "**********************************************"

rm -rf ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle

echo "Creating bundle workspace"

mkdir -p ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/bin

echo "Copying application files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/NOTICE
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/THIRD-PARTY
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/RELEASENOTES.md
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/VERSION
cp ${GO_SPACE}/bin/darwin_amd64_plugin/session-manager-plugin ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/bin/session-manager-plugin
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/seelog.xml.template

echo "Copying install script"

cp ${GO_SPACE}/Tools/src/darwin/install ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/install
chmod 755 ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/install;

cd ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle/bin/; strip session-manager-plugin; cd ~-

echo "Creating the bundle zip file"

if [ -f ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle.zip ]
then
    rm ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle.zip
fi

cd ${GO_SPACE}/bin/darwin_amd64_plugin;
zip -r ${GO_SPACE}/bin/darwin_amd64_plugin/sessionmanager-bundle.zip ./sessionmanager-bundle


#!/usr/bin/env bash
echo "**********************************************"
echo "Creating bundle zip file Mac OS X arm64 Plugin"
echo "**********************************************"

rm -rf ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle

echo "Creating bundle workspace"

mkdir -p ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/bin

echo "Copying application files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/NOTICE
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/THIRD-PARTY
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/RELEASENOTES.md
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/VERSION
cp ${GO_SPACE}/bin/darwin_arm64_plugin/session-manager-plugin ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/bin/session-manager-plugin
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/seelog.xml.template

echo "Copying install script"

cp ${GO_SPACE}/Tools/src/darwin/install ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/install
chmod 755 ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/install;

cd ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle/bin/; strip session-manager-plugin; cd ~-

echo "Creating the bundle zip file"

if [ -f ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle.zip ]
then
    rm ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle.zip
fi

cd ${GO_SPACE}/bin/darwin_arm64_plugin;
zip -r ${GO_SPACE}/bin/darwin_arm64_plugin/sessionmanager-bundle.zip ./sessionmanager-bundle


#!/usr/bin/env bash
echo "*********************************************"
echo "Creating deb file for Ubuntu Linux 386 Plugin"
echo "*********************************************"

rm -rf ${GO_SPACE}/bin/debian_386/debian

echo "Creating debian folders"

mkdir -p ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/debian_386/debian/etc/init/
mkdir -p ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_386/debian/usr/share/lintian/overrides/
mkdir -p ${GO_SPACE}/bin/debian_386/debian/var/lib/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_386/debian/lib/systemd/system/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_386_plugin/session-manager-plugin ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/bin/
cd ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/debian_386/debian/etc/init/
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.service ${GO_SPACE}/bin/debian_386/debian/lib/systemd/system/

echo "Copying debian package config files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/debian_386/debian/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/packaging/ubuntu_plugin/conffiles ${GO_SPACE}/bin/debian_386/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/preinst ${GO_SPACE}/bin/debian_386/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postinst ${GO_SPACE}/bin/debian_386/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/prerm ${GO_SPACE}/bin/debian_386/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postrm ${GO_SPACE}/bin/debian_386/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/lintian-overrides ${GO_SPACE}/bin/debian_386/debian/usr/share/lintian/overrides/sessionmanagerplugin

echo "Constructing the control file"

echo 'Package: session-manager-plugin' > ${GO_SPACE}/bin/debian_386/debian/control
echo 'Architecture: i386' >> ${GO_SPACE}/bin/debian_386/debian/control
echo -n 'Version: ' >> ${GO_SPACE}/bin/debian_386/debian/control
cat ${GO_SPACE}/VERSION | tr -d "\n" >> ${GO_SPACE}/bin/debian_386/debian/control
echo '-1' >> ${GO_SPACE}/bin/debian_386/debian/control
cat ${GO_SPACE}/packaging/ubuntu_plugin/control >> ${GO_SPACE}/bin/debian_386/debian/control

echo "Constructing the changelog file"

echo -n 'session-manager-plugin (' > ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/changelog
cat VERSION | tr -d "\n"  >> ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/changelog
echo '-1) unstable; urgency=low' >> ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/changelog
cat ${GO_SPACE}/packaging/ubuntu_plugin/changelog >> ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/changelog

cp ${GO_SPACE}/packaging/ubuntu_plugin/debian-binary ${GO_SPACE}/bin/debian_386/debian/

echo "Setting permissions as required by debian"

cd ${GO_SPACE}/bin/debian_386/; find ./debian -type d | xargs chmod 755; cd ~-

echo "Compressing changelog"

cd ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/; export GZIP=-9; tar cvzf changelog.gz changelog --owner=0 --group=0 ; cd ~-

rm ${GO_SPACE}/bin/debian_386/debian/usr/share/doc/sessionmanagerplugin/changelog

echo "Creating tar"

# the below permissioning is required by debian
cd ${GO_SPACE}/bin/debian_386/debian/; tar czf data.tar.gz usr etc lib --owner=0 --group=0 ; cd ~-
cd ${GO_SPACE}/bin/debian_386/debian/; tar czf control.tar.gz control conffiles preinst postinst prerm postrm --owner=0 --group=0 ; cd ~-

echo "Constructing the deb package"

ar r ${GO_SPACE}/bin/debian_386/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_386/debian/debian-binary
ar r ${GO_SPACE}/bin/debian_386/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_386/debian/control.tar.gz
ar r ${GO_SPACE}/bin/debian_386/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_386/debian/data.tar.gz
cp ${GO_SPACE}/bin/debian_386/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_386/session-manager-plugin.deb
mv ${GO_SPACE}/bin/debian_386/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/.

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/ubuntu/install.sh ${GO_SPACE}/bin/debian_386/
cp ${GO_SPACE}/Tools/src/update/ubuntu/uninstall.sh ${GO_SPACE}/bin/debian_386/

chmod 755 ${GO_SPACE}/bin/debian_386/install.sh ${GO_SPACE}/bin/debian_386/uninstall.sh

echo "Zip deb, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-ubuntu-386.tar.gz  -C ${GO_SPACE}/bin/debian_386/ session-manager-plugin.deb install.sh uninstall.sh

rm ${GO_SPACE}/bin/debian_386/install.sh
rm ${GO_SPACE}/bin/debian_386/uninstall.sh


#!/usr/bin/env bash
echo "***********************************************"
echo "Creating deb file for Ubuntu Linux amd64 Plugin"
echo "***********************************************"

rm -rf ${GO_SPACE}/bin/debian_amd64/debian

echo "Creating debian folders"

mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/etc/init/
mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/usr/share/lintian/overrides/
mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/var/lib/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_amd64/debian/lib/systemd/system/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_amd64_plugin/session-manager-plugin ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/bin/
cd ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/debian_amd64/debian/etc/init/
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.service ${GO_SPACE}/bin/debian_amd64/debian/lib/systemd/system/

echo "Copying debian package config files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/debian_amd64/debian/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/packaging/ubuntu_plugin/conffiles ${GO_SPACE}/bin/debian_amd64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/preinst ${GO_SPACE}/bin/debian_amd64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postinst ${GO_SPACE}/bin/debian_amd64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/prerm ${GO_SPACE}/bin/debian_amd64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postrm ${GO_SPACE}/bin/debian_amd64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/lintian-overrides ${GO_SPACE}/bin/debian_amd64/debian/usr/share/lintian/overrides/sessionmanagerplugin

echo "Constructing the control file"

echo 'Package: session-manager-plugin' > ${GO_SPACE}/bin/debian_amd64/debian/control
echo 'Architecture: amd64' >> ${GO_SPACE}/bin/debian_amd64/debian/control
echo -n 'Version: ' >> ${GO_SPACE}/bin/debian_amd64/debian/control
cat ${GO_SPACE}/VERSION | tr -d "\n" >> ${GO_SPACE}/bin/debian_amd64/debian/control
echo '-1' >> ${GO_SPACE}/bin/debian_amd64/debian/control
cat ${GO_SPACE}/packaging/ubuntu_plugin/control >> ${GO_SPACE}/bin/debian_amd64/debian/control

echo "Constructing the changelog file"

echo -n 'session-manager-plugin (' > ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/changelog
cat VERSION | tr -d "\n"  >> ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/changelog
echo '-1) unstable; urgency=low' >> ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/changelog
cat ${GO_SPACE}/packaging/ubuntu_plugin/changelog >> ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/changelog

cp ${GO_SPACE}/packaging/ubuntu_plugin/debian-binary ${GO_SPACE}/bin/debian_amd64/debian/

echo "Setting permissions as required by debian"

cd ${GO_SPACE}/bin/debian_amd64/; find ./debian -type d | xargs chmod 755; cd ~-

echo "Compressing changelog"

cd ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/; export GZIP=-9; tar cvzf changelog.gz changelog --owner=0 --group=0 ; cd ~-

rm ${GO_SPACE}/bin/debian_amd64/debian/usr/share/doc/sessionmanagerplugin/changelog

echo "Creating tar"

# the below permissioning is required by debian
cd ${GO_SPACE}/bin/debian_amd64/debian/; tar czf data.tar.gz usr etc lib --owner=0 --group=0 ; cd ~-
cd ${GO_SPACE}/bin/debian_amd64/debian/; tar czf control.tar.gz control conffiles preinst postinst prerm postrm --owner=0 --group=0 ; cd ~-

echo "Constructing the deb package"

ar r ${GO_SPACE}/bin/debian_amd64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_amd64/debian/debian-binary
ar r ${GO_SPACE}/bin/debian_amd64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_amd64/debian/control.tar.gz
ar r ${GO_SPACE}/bin/debian_amd64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_amd64/debian/data.tar.gz
cp ${GO_SPACE}/bin/debian_amd64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_amd64/session-manager-plugin.deb
mv ${GO_SPACE}/bin/debian_amd64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/.

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/ubuntu/install.sh ${GO_SPACE}/bin/debian_amd64/
cp ${GO_SPACE}/Tools/src/update/ubuntu/uninstall.sh ${GO_SPACE}/bin/debian_amd64/

chmod 755 ${GO_SPACE}/bin/debian_amd64/install.sh ${GO_SPACE}/bin/debian_amd64/uninstall.sh

echo "Zip deb, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-ubuntu-amd64.tar.gz  -C ${GO_SPACE}/bin/debian_amd64/ session-manager-plugin.deb install.sh uninstall.sh

rm ${GO_SPACE}/bin/debian_amd64/install.sh
rm ${GO_SPACE}/bin/debian_amd64/uninstall.sh


#!/usr/bin/env bash
echo "***********************************************"
echo "Creating deb file for Ubuntu Linux arm64 Plugin"
echo "***********************************************"

rm -rf ${GO_SPACE}/bin/debian_arm64/debian

echo "Creating debian folders"

mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/etc/init/
mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/usr/share/lintian/overrides/
mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/var/lib/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_arm64/debian/lib/systemd/system/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_arm64_plugin/session-manager-plugin ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/bin/
cd ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/debian_arm64/debian/etc/init/
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.service ${GO_SPACE}/bin/debian_arm64/debian/lib/systemd/system/

echo "Copying debian package config files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/debian_arm64/debian/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/packaging/ubuntu_plugin/conffiles ${GO_SPACE}/bin/debian_arm64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/preinst ${GO_SPACE}/bin/debian_arm64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postinst ${GO_SPACE}/bin/debian_arm64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/prerm ${GO_SPACE}/bin/debian_arm64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postrm ${GO_SPACE}/bin/debian_arm64/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/lintian-overrides ${GO_SPACE}/bin/debian_arm64/debian/usr/share/lintian/overrides/sessionmanagerplugin

echo "Constructing the control file"

echo 'Package: session-manager-plugin' > ${GO_SPACE}/bin/debian_arm64/debian/control
echo 'Architecture: arm64' >> ${GO_SPACE}/bin/debian_arm64/debian/control
echo -n 'Version: ' >> ${GO_SPACE}/bin/debian_arm64/debian/control
cat ${GO_SPACE}/VERSION | tr -d "\n" >> ${GO_SPACE}/bin/debian_arm64/debian/control
echo '-1' >> ${GO_SPACE}/bin/debian_arm64/debian/control
cat ${GO_SPACE}/packaging/ubuntu_plugin/control >> ${GO_SPACE}/bin/debian_arm64/debian/control

echo "Constructing the changelog file"

echo -n 'session-manager-plugin (' > ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/changelog
cat VERSION | tr -d "\n"  >> ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/changelog
echo '-1) unstable; urgency=low' >> ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/changelog
cat ${GO_SPACE}/packaging/ubuntu_plugin/changelog >> ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/changelog

cp ${GO_SPACE}/packaging/ubuntu_plugin/debian-binary ${GO_SPACE}/bin/debian_arm64/debian/

echo "Setting permissions as required by debian"

cd ${GO_SPACE}/bin/debian_arm64/; find ./debian -type d | xargs chmod 755; cd ~-

echo "Compressing changelog"

cd ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/; export GZIP=-9; tar cvzf changelog.gz changelog --owner=0 --group=0 ; cd ~-

rm ${GO_SPACE}/bin/debian_arm64/debian/usr/share/doc/sessionmanagerplugin/changelog

echo "Creating tar"

# the below permissioning is required by debian
cd ${GO_SPACE}/bin/debian_arm64/debian/; tar czf data.tar.gz usr etc lib --owner=0 --group=0 ; cd ~-
cd ${GO_SPACE}/bin/debian_arm64/debian/; tar czf control.tar.gz control conffiles preinst postinst prerm postrm --owner=0 --group=0 ; cd ~-

echo "Constructing the deb package"

ar r ${GO_SPACE}/bin/debian_arm64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm64/debian/debian-binary
ar r ${GO_SPACE}/bin/debian_arm64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm64/debian/control.tar.gz
ar r ${GO_SPACE}/bin/debian_arm64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm64/debian/data.tar.gz
cp ${GO_SPACE}/bin/debian_arm64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm64/session-manager-plugin.deb
mv ${GO_SPACE}/bin/debian_arm64/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/.

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/ubuntu/install.sh ${GO_SPACE}/bin/debian_arm64/
cp ${GO_SPACE}/Tools/src/update/ubuntu/uninstall.sh ${GO_SPACE}/bin/debian_arm64/

chmod 755 ${GO_SPACE}/bin/debian_arm64/install.sh ${GO_SPACE}/bin/debian_arm64/uninstall.sh

echo "Zip deb, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-ubuntu-arm64.tar.gz  -C ${GO_SPACE}/bin/debian_arm64/ session-manager-plugin.deb install.sh uninstall.sh

rm ${GO_SPACE}/bin/debian_arm64/install.sh
rm ${GO_SPACE}/bin/debian_arm64/uninstall.sh


#!/usr/bin/env bash
echo "*********************************************"
echo "Creating deb file for Ubuntu Linux arm Plugin"
echo "*********************************************"

rm -rf ${GO_SPACE}/bin/debian_arm/debian

echo "Creating debian folders"

mkdir -p ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/debian_arm/debian/etc/init/
mkdir -p ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_arm/debian/usr/share/lintian/overrides/
mkdir -p ${GO_SPACE}/bin/debian_arm/debian/var/lib/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/debian_arm/debian/lib/systemd/system/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_arm_plugin/session-manager-plugin ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/bin/
cd ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/debian_arm/debian/etc/init/
cp ${GO_SPACE}/packaging/ubuntu_plugin/session-manager-plugin.service ${GO_SPACE}/bin/debian_arm/debian/lib/systemd/system/

echo "Copying debian package config files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/debian_arm/debian/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/packaging/ubuntu_plugin/conffiles ${GO_SPACE}/bin/debian_arm/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/preinst ${GO_SPACE}/bin/debian_arm/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postinst ${GO_SPACE}/bin/debian_arm/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/prerm ${GO_SPACE}/bin/debian_arm/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/postrm ${GO_SPACE}/bin/debian_arm/debian/
cp ${GO_SPACE}/packaging/ubuntu_plugin/lintian-overrides ${GO_SPACE}/bin/debian_arm/debian/usr/share/lintian/overrides/sessionmanagerplugin

echo "Constructing the control file"

echo 'Package: session-manager-plugin' > ${GO_SPACE}/bin/debian_arm/debian/control
echo 'Architecture: armhf' >> ${GO_SPACE}/bin/debian_arm/debian/control
echo -n 'Version: ' >> ${GO_SPACE}/bin/debian_arm/debian/control
cat ${GO_SPACE}/VERSION | tr -d "\n" >> ${GO_SPACE}/bin/debian_arm/debian/control
echo '-1' >> ${GO_SPACE}/bin/debian_arm/debian/control
cat ${GO_SPACE}/packaging/ubuntu_plugin/control >> ${GO_SPACE}/bin/debian_arm/debian/control

echo "Constructing the changelog file"

echo -n 'session-manager-plugin (' > ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/changelog
cat VERSION | tr -d "\n"  >> ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/changelog
echo '-1) unstable; urgency=low' >> ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/changelog
cat ${GO_SPACE}/packaging/ubuntu_plugin/changelog >> ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/changelog

cp ${GO_SPACE}/packaging/ubuntu_plugin/debian-binary ${GO_SPACE}/bin/debian_arm/debian/

echo "Setting permissions as required by debian"

cd ${GO_SPACE}/bin/debian_arm/; find ./debian -type d | xargs chmod 755; cd ~-

echo "Compressing changelog"

cd ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/; export GZIP=-9; tar cvzf changelog.gz changelog --owner=0 --group=0 ; cd ~-

rm ${GO_SPACE}/bin/debian_arm/debian/usr/share/doc/sessionmanagerplugin/changelog

echo "Creating tar"

# the below permissioning is required by debian
cd ${GO_SPACE}/bin/debian_arm/debian/; tar czf data.tar.gz usr etc lib --owner=0 --group=0 ; cd ~-
cd ${GO_SPACE}/bin/debian_arm/debian/; tar czf control.tar.gz control conffiles preinst postinst prerm postrm --owner=0 --group=0 ; cd ~-

echo "Constructing the deb package"

ar r ${GO_SPACE}/bin/debian_arm/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm/debian/debian-binary
ar r ${GO_SPACE}/bin/debian_arm/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm/debian/control.tar.gz
ar r ${GO_SPACE}/bin/debian_arm/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm/debian/data.tar.gz
cp ${GO_SPACE}/bin/debian_arm/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/debian_arm/session-manager-plugin.deb
mv ${GO_SPACE}/bin/debian_arm/session-manager-plugin-`cat ${GO_SPACE}/VERSION`-1.deb ${GO_SPACE}/bin/.

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/ubuntu/install.sh ${GO_SPACE}/bin/debian_arm/
cp ${GO_SPACE}/Tools/src/update/ubuntu/uninstall.sh ${GO_SPACE}/bin/debian_arm/

chmod 755 ${GO_SPACE}/bin/debian_arm/install.sh ${GO_SPACE}/bin/debian_arm/uninstall.sh

echo "Zip deb, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-ubuntu-arm.tar.gz  -C ${GO_SPACE}/bin/debian_arm/ session-manager-plugin.deb install.sh uninstall.sh

rm ${GO_SPACE}/bin/debian_arm/install.sh
rm ${GO_SPACE}/bin/debian_arm/uninstall.sh


#!/usr/bin/env bash
echo "***********************************************"
echo "Creating rpm file for Amazon Linux and RHEL 386"
echo "***********************************************"

rm -rf ${GO_SPACE}/bin/linux_386/linux

echo "Creating rpmbuild workspace"

mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/SPECS
mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/COORD_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/DATA_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/BUILD
mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/RPMS
mkdir -p ${GO_SPACE}/bin/linux_386/linux/rpmbuild/SRPMS
mkdir -p ${GO_SPACE}/bin/linux_386/linux/usr/bin/
mkdir -p ${GO_SPACE}/bin/linux_386/linux/etc/init/
mkdir -p ${GO_SPACE}/bin/linux_386/linux/etc/systemd/system/
mkdir -p ${GO_SPACE}/bin/linux_386/linux/etc/amazon/ssmcli/
mkdir -p ${GO_SPACE}/bin/linux_386/linux/var/lib/amazon/ssmcli/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_386/ssmcli ${GO_SPACE}/bin/linux_386/linux/usr/bin/
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/linux_386/linux/etc/amazon/ssmcli/seelog.xml.template
cp ${GO_SPACE}/packaging/linux/ssmcli.conf ${GO_SPACE}/bin/linux_386/linux/etc/init/
cp ${GO_SPACE}/packaging/linux/ssmcli.service ${GO_SPACE}/bin/linux_386/linux/etc/systemd/system/
cd ${GO_SPACE}/bin/linux_386/linux/usr/bin/; strip --strip-unneeded ssmcli; cd ~-

echo "Creating the rpm package"

SPEC_FILE="${GO_SPACE}/packaging/linux/ssmcli.spec"
BUILD_ROOT="${GO_SPACE}/bin/linux_386/linux"

setarch i386 rpmbuild --target i386 -bb --define "rpmversion `cat ${GO_SPACE}/VERSION`" --define "_topdir bin/linux_386/linux/rpmbuild" --buildroot ${BUILD_ROOT} ${SPEC_FILE}

echo "Copying rpm files to bin"

cp ${GO_SPACE}/bin/linux_386/linux/rpmbuild/RPMS/i386/*.rpm ${GO_SPACE}/bin/
cp ${GO_SPACE}/bin/linux_386/linux/rpmbuild/RPMS/i386/*.rpm ${GO_SPACE}/bin/linux_386/ssmcli.rpm

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/linux/install.sh ${GO_SPACE}/bin/linux_386/
cp ${GO_SPACE}/Tools/src/update/linux/uninstall.sh ${GO_SPACE}/bin/linux_386/

chmod 755 ${GO_SPACE}/bin/linux_386/install.sh ${GO_SPACE}/bin/linux_386/uninstall.sh

echo "Zip rpm, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/ssmcli/`cat ${GO_SPACE}/VERSION`/ssmcli-linux-386.tar.gz  -C ${GO_SPACE}/bin/linux_386/ ssmcli.rpm install.sh uninstall.sh

rm ${GO_SPACE}/bin/linux_386/install.sh
rm ${GO_SPACE}/bin/linux_386/uninstall.sh


#!/usr/bin/env bash
echo "******************************************************"
echo "Creating rpm file for Amazon Linux and RHEL 386 Plugin"
echo "******************************************************"

rm -rf ${GO_SPACE}/bin/linux_386_plugin/linux

echo "Creating rpmbuild workspace"

mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/SPECS
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/COORD_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/DATA_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/BUILD
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/RPMS
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/SRPMS
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/bin/
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/etc/init/
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/etc/systemd/system/
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/etc/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/linux_386_plugin/linux/var/lib/amazon/sessionmanagerplugin/

echo "Copying application files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/bin/linux_386_plugin/session-manager-plugin ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/bin/
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/linux_386_plugin/linux/etc/init/
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.service ${GO_SPACE}/bin/linux_386_plugin/linux/etc/systemd/system/
cd ${GO_SPACE}/bin/linux_386_plugin/linux/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-

echo "Creating the rpm package"

SPEC_FILE="${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.spec"
BUILD_ROOT="${GO_SPACE}/bin/linux_386_plugin/linux"

setarch x86_64 rpmbuild -bb --define "rpmversion `cat ${GO_SPACE}/VERSION`" --define "_topdir bin/linux_386_plugin/linux/rpmbuild" --buildroot ${BUILD_ROOT} ${SPEC_FILE}

echo "Copying rpm files to bin"

cp ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/
cp ${GO_SPACE}/bin/linux_386_plugin/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/linux_386_plugin/session-manager-plugin.rpm

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/linux/install.sh ${GO_SPACE}/bin/linux_386_plugin/
cp ${GO_SPACE}/Tools/src/update/linux/uninstall.sh ${GO_SPACE}/bin/linux_386_plugin/

chmod 755 ${GO_SPACE}/bin/linux_386_plugin/install.sh ${GO_SPACE}/bin/linux_386_plugin/uninstall.sh

echo "Zip rpm, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-linux-386.tar.gz  -C ${GO_SPACE}/bin/linux_386_plugin/ session-manager-plugin.rpm install.sh uninstall.sh

rm ${GO_SPACE}/bin/linux_386_plugin/install.sh
rm ${GO_SPACE}/bin/linux_386_plugin/uninstall.sh


#!/usr/bin/env bash
echo "*************************************************"
echo "Creating rpm file for Amazon Linux and RHEL amd64"
echo "*************************************************"

rm -rf ${GO_SPACE}/bin/linux_amd64/linux

echo "Creating rpmbuild workspace"

mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/SPECS
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/COORD_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/DATA_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/BUILD
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/RPMS
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/SRPMS
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/usr/bin/
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/etc/init/
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/etc/systemd/system/
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/etc/amazon/ssmcli/
mkdir -p ${GO_SPACE}/bin/linux_amd64/linux/var/lib/amazon/ssmcli/

echo "Copying application files"

cp ${GO_SPACE}/bin/linux_amd64/ssmcli ${GO_SPACE}/bin/linux_amd64/linux/usr/bin/
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/linux_amd64/linux/etc/amazon/ssmcli/seelog.xml.template
cp ${GO_SPACE}/packaging/linux/ssmcli.conf ${GO_SPACE}/bin/linux_amd64/linux/etc/init/
cp ${GO_SPACE}/packaging/linux/ssmcli.service ${GO_SPACE}/bin/linux_amd64/linux/etc/systemd/system/
cd ${GO_SPACE}/bin/linux_amd64/linux/usr/bin/; strip --strip-unneeded ssmcli; cd ~-

echo "Creating the rpm package"

SPEC_FILE="${GO_SPACE}/packaging/linux/ssmcli.spec"
BUILD_ROOT="${GO_SPACE}/bin/linux_amd64/linux"

setarch x86_64 rpmbuild -bb --define "rpmversion `cat ${GO_SPACE}/VERSION`" --define "_topdir bin/linux_amd64/linux/rpmbuild" --buildroot ${BUILD_ROOT} ${SPEC_FILE}

echo "Copying rpm files to bin"

cp ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/
cp ${GO_SPACE}/bin/linux_amd64/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/linux_amd64/ssmcli.rpm

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/linux/install.sh ${GO_SPACE}/bin/linux_amd64/
cp ${GO_SPACE}/Tools/src/update/linux/uninstall.sh ${GO_SPACE}/bin/linux_amd64/

chmod 755 ${GO_SPACE}/bin/linux_amd64/install.sh ${GO_SPACE}/bin/linux_amd64/uninstall.sh

echo "Zip rpm, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/ssmcli/`cat ${GO_SPACE}/VERSION`/ssmcli-linux-amd64.tar.gz  -C ${GO_SPACE}/bin/linux_amd64/ ssmcli.rpm install.sh uninstall.sh

rm ${GO_SPACE}/bin/linux_amd64/install.sh
rm ${GO_SPACE}/bin/linux_amd64/uninstall.sh


#!/usr/bin/env bash
echo "********************************************************"
echo "Creating rpm file for Amazon Linux and RHEL amd64 Plugin"
echo "********************************************************"

rm -rf ${GO_SPACE}/bin/linux_amd64_plugin/linux

echo "Creating rpmbuild workspace"

mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/SPECS
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/COORD_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/DATA_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/BUILD
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/RPMS
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/SRPMS
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/bin/
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/etc/init/
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/etc/systemd/system/
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/etc/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/linux_amd64_plugin/linux/var/lib/amazon/sessionmanagerplugin/

echo "Copying application files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/bin/linux_amd64_plugin/session-manager-plugin ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/bin/
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/linux_amd64_plugin/linux/etc/init/
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.service ${GO_SPACE}/bin/linux_amd64_plugin/linux/etc/systemd/system/
cd ${GO_SPACE}/bin/linux_amd64_plugin/linux/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-

echo "Creating the rpm package"

SPEC_FILE="${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.spec"
BUILD_ROOT="${GO_SPACE}/bin/linux_amd64_plugin/linux"

setarch x86_64 rpmbuild -bb --define "rpmversion `cat ${GO_SPACE}/VERSION`" --define "_topdir bin/linux_amd64_plugin/linux/rpmbuild" --buildroot ${BUILD_ROOT} ${SPEC_FILE}

echo "Copying rpm files to bin"

cp ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/
cp ${GO_SPACE}/bin/linux_amd64_plugin/linux/rpmbuild/RPMS/x86_64/*.rpm ${GO_SPACE}/bin/linux_amd64_plugin/session-manager-plugin.rpm

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/linux/install.sh ${GO_SPACE}/bin/linux_amd64_plugin/
cp ${GO_SPACE}/Tools/src/update/linux/uninstall.sh ${GO_SPACE}/bin/linux_amd64_plugin/

chmod 755 ${GO_SPACE}/bin/linux_amd64_plugin/install.sh ${GO_SPACE}/bin/linux_amd64_plugin/uninstall.sh

echo "Zip rpm, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-linux-amd64.tar.gz  -C ${GO_SPACE}/bin/linux_amd64_plugin/ session-manager-plugin.rpm install.sh uninstall.sh

rm ${GO_SPACE}/bin/linux_amd64_plugin/install.sh
rm ${GO_SPACE}/bin/linux_amd64_plugin/uninstall.sh


#!/usr/bin/env bash
echo "********************************************************"
echo "Creating rpm file for Amazon Linux and RHEL arm64 Plugin"
echo "********************************************************"

rm -rf ${GO_SPACE}/bin/linux_arm64_plugin/linux

echo "Creating rpmbuild workspace"

mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/SPECS
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/COORD_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/DATA_SOURCES
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/BUILD
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/RPMS
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/SRPMS
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/bin/
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/bin/
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/etc/init/
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/etc/systemd/system/
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/etc/amazon/sessionmanagerplugin/
mkdir -p ${GO_SPACE}/bin/linux_arm64_plugin/linux/var/lib/amazon/sessionmanagerplugin/

echo "Copying application files"

cp ${GO_SPACE}/LICENSE ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/LICENSE
cp ${GO_SPACE}/NOTICE ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/NOTICE
cp ${GO_SPACE}/README.md ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/THIRD-PARTY
cp ${GO_SPACE}/VERSION ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/VERSION
cp ${GO_SPACE}/bin/linux_arm64_plugin/session-manager-plugin ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/bin/
cp ${GO_SPACE}/seelog_unix.xml ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/seelog.xml.template
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.conf ${GO_SPACE}/bin/linux_arm64_plugin/linux/etc/init/
cp ${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.service ${GO_SPACE}/bin/linux_arm64_plugin/linux/etc/systemd/system/
cd ${GO_SPACE}/bin/linux_arm64_plugin/linux/usr/local/sessionmanagerplugin/bin/; strip --strip-unneeded session-manager-plugin; cd ~-

echo "Creating the rpm package"

SPEC_FILE="${GO_SPACE}/packaging/linux_plugin/session-manager-plugin.spec"
BUILD_ROOT="${GO_SPACE}/bin/linux_arm64_plugin/linux"

rpmbuild -bb --target aarch64 --define "rpmversion `cat ${GO_SPACE}/VERSION`" --define "_topdir bin/linux_arm64_plugin/linux/rpmbuild" --buildroot ${BUILD_ROOT} ${SPEC_FILE}

echo "Copying rpm files to bin"

cp ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/RPMS/aarch64/*.rpm ${GO_SPACE}/bin/
cp ${GO_SPACE}/bin/linux_arm64_plugin/linux/rpmbuild/RPMS/aarch64/*.rpm ${GO_SPACE}/bin/linux_arm64_plugin/session-manager-plugin.rpm

echo "Copying install and uninstall script to bin"

cp ${GO_SPACE}/Tools/src/update/linux/install.sh ${GO_SPACE}/bin/linux_arm64_plugin/
cp ${GO_SPACE}/Tools/src/update/linux/uninstall.sh ${GO_SPACE}/bin/linux_arm64_plugin/

chmod 755 ${GO_SPACE}/bin/linux_arm64_plugin/install.sh ${GO_SPACE}/bin/linux_arm64_plugin/uninstall.sh

echo "Zip rpm, install and uninstall files"

tar -zcvf ${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-linux-arm64.tar.gz  -C ${GO_SPACE}/bin/linux_arm64_plugin/ session-manager-plugin.rpm install.sh uninstall.sh

rm ${GO_SPACE}/bin/linux_arm64_plugin/install.sh
rm ${GO_SPACE}/bin/linux_arm64_plugin/uninstall.sh


#!/usr/bin/env bash
echo "****************************************"
echo "Creating zip file for Windows 386 Plugin"
echo "****************************************"

BIN_FOLDER=${GO_SPACE}/bin
BUILD_FOLDER=${BIN_FOLDER}/windows_386_plugin
PACKAGE_FOLDER=${BUILD_FOLDER}/windows

rm -rf ${PACKAGE_FOLDER}

echo "Creating windows folders"

mkdir -p ${PACKAGE_FOLDER}

echo "Copying application files"

mkdir -p ${PACKAGE_FOLDER}/bin
cp ${BUILD_FOLDER}/session-manager-plugin.exe ${PACKAGE_FOLDER}/bin/session-manager-plugin.exe
cp ${GO_SPACE}/seelog_windows.xml.template ${PACKAGE_FOLDER}/seelog.xml.template

echo "Copying windows package config files"

cp ${GO_SPACE}/LICENSE ${PACKAGE_FOLDER}/LICENSE
cp ${GO_SPACE}/NOTICE ${PACKAGE_FOLDER}/NOTICE
cp ${GO_SPACE}/README.md ${PACKAGE_FOLDER}/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${PACKAGE_FOLDER}/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${PACKAGE_FOLDER}/THIRD-PARTY

echo "Constructing the zip package"

if [ -f ${PACKAGE_FOLDER}/session-manager-plugin.zip ]
then
    rm ${PACKAGE_FOLDER}/session-manager-plugin.zip
fi
cd ${PACKAGE_FOLDER}
zip -r package *

cp ${GO_SPACE}/Tools/src/update/windows/install.bat ${PACKAGE_FOLDER}/
cp ${GO_SPACE}/Tools/src/update/windows/uninstall.bat ${PACKAGE_FOLDER}/

WINDOWS_386_ZIP=${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-windows-386.zip
zip -j ${WINDOWS_386_ZIP} ${PACKAGE_FOLDER}/package.zip
zip -j ${WINDOWS_386_ZIP} ${PACKAGE_FOLDER}/install.bat
zip -j ${WINDOWS_386_ZIP} ${PACKAGE_FOLDER}/uninstall.bat

cp ${WINDOWS_386_ZIP} ${BUILD_FOLDER}
rm -rf ${PACKAGE_FOLDER}


#!/usr/bin/env bash
echo "****************************************"
echo "Creating zip file for Windows amd64 plugin"
echo "****************************************"

BIN_FOLDER=${GO_SPACE}/bin
BUILD_FOLDER=${BIN_FOLDER}/windows_amd64_plugin
PACKAGE_FOLDER=${BUILD_FOLDER}/windows

rm -rf ${PACKAGE_FOLDER}

echo "Creating windows folders"

mkdir -p ${PACKAGE_FOLDER}

echo "Copying application files"

mkdir -p ${PACKAGE_FOLDER}/bin
cp ${BUILD_FOLDER}/session-manager-plugin.exe ${PACKAGE_FOLDER}/bin/session-manager-plugin.exe
cp ${GO_SPACE}/seelog_windows.xml.template ${PACKAGE_FOLDER}/seelog.xml.template

echo "Copying windows package config files"

cp ${GO_SPACE}/LICENSE ${PACKAGE_FOLDER}/LICENSE
cp ${GO_SPACE}/NOTICE ${PACKAGE_FOLDER}/NOTICE
cp ${GO_SPACE}/README.md ${PACKAGE_FOLDER}/README.md
cp ${GO_SPACE}/RELEASENOTES.md ${PACKAGE_FOLDER}/RELEASENOTES.md
cp ${GO_SPACE}/THIRD-PARTY ${PACKAGE_FOLDER}/THIRD-PARTY

echo "Constructing the zip package"

if [ -f ${PACKAGE_FOLDER}/session-manager-plugin.zip ]
then
    rm ${PACKAGE_FOLDER}/session-manager-plugin.zip
fi
cd ${PACKAGE_FOLDER}
zip -r package *

cp ${GO_SPACE}/Tools/src/update/windows/install.bat ${PACKAGE_FOLDER}/
cp ${GO_SPACE}/Tools/src/update/windows/uninstall.bat ${PACKAGE_FOLDER}/

WINDOWS_AMD64_ZIP=${GO_SPACE}/bin/updates/sessionmanagerplugin/`cat ${GO_SPACE}/VERSION`/session-manager-plugin-windows-amd64.zip
zip -j ${WINDOWS_AMD64_ZIP} ${PACKAGE_FOLDER}/package.zip
zip -j ${WINDOWS_AMD64_ZIP} ${PACKAGE_FOLDER}/install.bat
zip -j ${WINDOWS_AMD64_ZIP} ${PACKAGE_FOLDER}/uninstall.bat

cp ${WINDOWS_AMD64_ZIP} ${BUILD_FOLDER}
rm -rf ${PACKAGE_FOLDER}


#!/bin/bash

if [[ $(/sbin/init --version 2> /dev/null) =~ upstart ]]; then
	echo "upstart detected"
	echo "Installing ssmcli"
	rpm -U ssmcli.rpm
	ssmcliVersion=$(rpm -q --qf '%{VERSION}\n' ssmcli)
	echo "Installed version: $ssmcliVersion"
	echo "starting ssmcli"
	/sbin/start ssmcli
	echo "$(status ssmcli)"
elif [[ $(systemctl 2> /dev/null) =~ -\.mount ]]; then
	if [[ "$(systemctl is-active ssmcli)" == "active" ]]; then
		echo "-> ssmcli is running"
		echo "Stopping the ssmcli"
		echo "$(systemctl stop ssmcli)"
		echo "ssmcli stopped"
		echo "$(systemctl daemon-reload)"
		echo "Reload daemon"
	else
		echo "-> ssmcli is not running on the machine"
	fi

	echo "Installing ssmcli"
	echo "$(rpm -U ssmcli.rpm)"

	echo "Starting ssmcli"
	$(systemctl daemon-reload)
	$(systemctl start ssmcli)
	echo "$(systemctl status ssmcli)"
else
	echo "ssmcli is not installed on this machine"
fi


#!/bin/bash

echo "Uninstalling ssmcli"

if [[ $(/sbin/init --version 2> /dev/null) =~ upstart ]]; then
	echo "Checking if the ssmcli is installed"
	if [ "$(rpm -q ssmcli)" != "package ssmcli is not installed" ]; then
		echo "-> ssmcli is installed on this machine"
		echo "Uninstalling the ssmcli"
		rpm --erase ssmcli
		sleep 1
	else
		echo "-> ssmcli is not installed on this machine"
	fi
elif [[ $(systemctl 2> /dev/null) =~ -\.mount ]]; then
	echo "Checking if the ssmcli is installed"
	if [[ "$(systemctl status ssmcli)" != *"Loaded: not-found"* ]]; then
		echo "-> ssmcli is installed on this machine"
		echo "Uninstalling the ssmcli"
		rpm --erase ssmcli
		sleep 1
	else
		echo "-> ssmcli is not installed on this machine"
	fi
else
	echo "ssmcli is not installed on this machine"
fi


#!/bin/bash

echo "Installing ubuntu pkg"

if [[ "$(cat /proc/1/comm)" == "init" ]]; then
    if [ "$(dpkg -s ssmcli | grep 'Status:')" == "Status: install ok installed" ]; then
	    # echo "-> ssmcli is installed in this instance"
	    # stop the ssmcli if it is running
	    # echo "Checking if the ssmcli is running"
	    if [ "$(status ssmcli)" != "ssmcli stop/waiting" ]; then
		    # echo "-> ssmcli is running in the instance"
  		    # echo "Stopping the ssmcli"
  		    /sbin/stop ssmcli
  		    sleep 1
	    fi
    fi

    # echo "Installing ssmcli"
    dpkg -i ssmcli.deb

    # echo "Starting ssmcli"
    /sbin/start ssmcli
    # echo "Status"
    status ssmcli

elif [[ "$(cat /proc/1/comm)" == "systemd" ]]; then
	if [[ "$(systemctl is-active ssmcli)" == "active" ]]; then
		# echo "-> ssmcli is running in the instance"
		systemctl stop ssmcli
		# echo "ssmcli stopped"
		systemctl daemon-reload
		# echo "Reload daemon"
	else
		echo "-> ssmcli is not running in the instance"
	fi

	# echo "Installing ssmcli"
	dpkg -i ssmcli.deb

	# echo "Starting ssmcli"
	systemctl daemon-reload
	systemctl start ssmcli
	systemctl status ssmcli
else

    echo "ssmcli is not installed on this machine"
fi


#!bin/bash

echo "Uninstalling deb pkg"

# echo "Checking if the ssmcli is installed"
# uninstall the ssmcli if it is present
if [[ "$(cat /proc/1/comm)" == "init" ]]; then
    if [ "$(dpkg -s ssmcli | grep 'Status:')" == "Status: install ok installed" ]; then
        # echo "-> ssmcli is installed in this instance"
        # stop the ssmcli if it is running
        # echo "Checking if the ssmcli is running"
        if [ "$(status ssmcli)" != "ssmcli stop/waiting" ]; then
            # echo "-> ssmcli is running in the instance"
            # echo "Stopping the ssmcli"
            /sbin/stop ssmcli
            sleep 1
        else
            echo "-> ssmcli is not running"
        fi
        # echo "Uninstalling the ssmcli"
        dpkg -r ssmcli
        sleep 1
    else
        echo "-> ssmcli is not installed on this machine"
    fi
elif [[ "$(cat /proc/1/comm)" == "systemd" ]]; then
	if [[ "$(systemctl is-active ssmcli)" == "active" ]]; then
		# echo "-> ssmcli is running in the instance"
		# echo "Stopping the ssmcli"
		systemctl stop ssmcli
		# echo "ssmcli stopped"
		systemctl daemon-reload
		# echo "Reload daemon"
	else
		echo "-> ssmcli is not running in the instance"
	fi

	# echo "Uninstalling ssmcli"
	dpkg -r ssmcli

else
    echo "ssmcli is not installed on this machine"
fi


#!/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.

# The plan9 package provides access to the raw system call
# interface of the underlying operating system.  Porting Go to
# a new architecture/operating system combination requires
# some manual effort, though there are tools that automate
# much of the process.  The auto-generated files have names
# beginning with z.
#
# This script runs or (given -n) prints suggested commands to generate z files
# for the current system.  Running those commands is not automatic.
# This script is documentation more than anything else.
#
# * asm_${GOOS}_${GOARCH}.s
#
# This hand-written assembly file implements system call dispatch.
# There are three entry points:
#
# 	func Syscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
# 	func Syscall6(trap, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr);
# 	func RawSyscall(trap, a1, a2, a3 uintptr) (r1, r2, err uintptr);
#
# The first and second are the standard ones; they differ only in
# how many arguments can be passed to the kernel.
# The third is for low-level use by the ForkExec wrapper;
# unlike the first two, it does not call into the scheduler to
# let it know that a system call is running.
#
# * syscall_${GOOS}.go
#
# This hand-written Go file implements system calls that need
# special handling and lists "//sys" comments giving prototypes
# for ones that can be auto-generated.  Mksyscall reads those
# comments to generate the stubs.
#
# * syscall_${GOOS}_${GOARCH}.go
#
# Same as syscall_${GOOS}.go except that it contains code specific
# to ${GOOS} on one particular architecture.
#
# * types_${GOOS}.c
#
# This hand-written C file includes standard C headers and then
# creates typedef or enum names beginning with a dollar sign
# (use of $ in variable names is a gcc extension).  The hardest
# part about preparing this file is figuring out which headers to
# include and which symbols need to be #defined to get the
# actual data structures that pass through to the kernel system calls.
# Some C libraries present alternate versions for binary compatibility
# and translate them on the way in and out of system calls, but
# there is almost always a #define that can get the real ones.
# See types_darwin.c and types_linux.c for examples.
#
# * zerror_${GOOS}_${GOARCH}.go
#
# This machine-generated file defines the system's error numbers,
# error strings, and signal numbers.  The generator is "mkerrors.sh".
# Usually no arguments are needed, but mkerrors.sh will pass its
# arguments on to godefs.
#
# * zsyscall_${GOOS}_${GOARCH}.go
#
# Generated by mksyscall.pl; see syscall_${GOOS}.go above.
#
# * zsysnum_${GOOS}_${GOARCH}.go
#
# Generated by mksysnum_${GOOS}.
#
# * ztypes_${GOOS}_${GOARCH}.go
#
# Generated by godefs; see types_${GOOS}.c above.

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

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

case "$1" in
-syscalls)
	for i in zsyscall*go
	do
		sed 1q $i | sed 's;^// ;;' | sh > _$i && gofmt < _$i > $i
		rm _$i
	done
	exit 0
	;;
-n)
	run="cat"
	shift
esac

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

case "$GOOSARCH" in
_* | *_ | _)
	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
	exit 1
	;;
plan9_386)
	mkerrors=
	mksyscall="./mksyscall.pl -l32 -plan9"
	mksysnum="./mksysnum_plan9.sh /n/sources/plan9/sys/src/libc/9syscall/sys.h"
	mktypes="XXX"
	;;
*)
	echo 'unrecognized $GOOS_$GOARCH: ' "$GOOSARCH" 1>&2
	exit 1
	;;
esac

(
	if [ -n "$mkerrors" ]; then echo "$mkerrors |gofmt >$zerrors"; fi
	case "$GOOS" in
	plan9)
		syscall_goos="syscall_$GOOS.go"
		if [ -n "$mksyscall" ]; then echo "$mksyscall $syscall_goos syscall_$GOOSARCH.go |gofmt >zsyscall_$GOOSARCH.go"; 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 |gofmt >ztypes_$GOOSARCH.go"; 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

CC=${CC:-gcc}

uname=$(uname)

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>
'

ccflags="$@"

# Write go tool cgo -godefs input.
(
	echo package plan9
	echo
	echo '/*'
	indirect="includes_$(uname)"
	echo "${!indirect} $includes"
	echo '*/'
	echo 'import "C"'
	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 !~ /^ETH_/ &&
		$2 !~ /^EPROC_/ &&
		$2 !~ /^EQUIV_/ &&
		$2 !~ /^EXPR_/ &&
		$2 ~ /^E[A-Z0-9_]+$/ ||
		$2 ~ /^B[0-9_]+$/ ||
		$2 ~ /^V[A-Z0-9]+$/ ||
		$2 ~ /^CS[A-Z0-9]/ ||
		$2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
		$2 ~ /^IGN/ ||
		$2 ~ /^IX(ON|ANY|OFF)$/ ||
		$2 ~ /^IN(LCR|PCK)$/ ||
		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
		$2 ~ /^C(LOCAL|READ)$/ ||
		$2 == "BRKINT" ||
		$2 == "HUPCL" ||
		$2 == "PENDIN" ||
		$2 == "TOSTOP" ||
		$2 ~ /^PAR/ ||
		$2 ~ /^SIG[^_]/ ||
		$2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
		$2 ~ /^IN_/ ||
		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
		$2 == "ICMPV6_FILTER" ||
		$2 == "SOMAXCONN" ||
		$2 == "NAME_MAX" ||
		$2 == "IFNAMSIZ" ||
		$2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
		$2 ~ /^SYSCTL_VERS/ ||
		$2 ~ /^(MS|MNT)_/ ||
		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
		$2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
		$2 ~ /^LINUX_REBOOT_CMD_/ ||
		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
		$2 !~ "NLA_TYPE_MASK" &&
		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
		$2 ~ /^SIOC/ ||
		$2 ~ /^TIOC/ ||
		$2 !~ "RTF_BITS" &&
		$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
		$2 ~ /^BIOC/ ||
		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
		$2 ~ /^CLONE_[A-Z_]+/ ||
		$2 !~ /^(BPF_TIMEVAL)$/ &&
		$2 ~ /^(BPF|DLT)_/ ||
		$2 !~ "WMESGLEN" &&
		$2 ~ /^W[A-Z0-9]+$/ {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)' |
	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)' |
	sort >_signal.grep

echo '// mkerrors.sh' "$@"
echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
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/=\(.*\)/= Errno(\1)/'
echo ')'

echo
echo '// Signals'
echo 'const ('
cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= 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

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

	echo -E "
};

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

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

static int
intcmp(const void *a, const void *b)
{
	return *(int*)a - *(int*)b;
}

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

	printf("\n\n// Error table\n");
	printf("var errors = [...]string {\n");
	qsort(errors, nelem(errors), sizeof errors[0], intcmp);
	for(i=0; i<nelem(errors); i++) {
		e = errors[i];
		if(i > 0 && errors[i-1] == 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\",\n", e, buf);
	}
	printf("}\n\n");

	printf("\n\n// Signal table\n");
	printf("var signals = [...]string {\n");
	qsort(signals, nelem(signals), sizeof signals[0], intcmp);
	for(i=0; i<nelem(signals); i++) {
		e = signals[i];
		if(i > 0 && signals[i-1] == 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\",\n", e, 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/sh
# 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.

COMMAND="mksysnum_plan9.sh $@"

cat <<EOF
// $COMMAND
// MACHINE GENERATED BY THE ABOVE COMMAND; DO NOT EDIT

package plan9

const(
EOF

SP='[ 	]' # space or tab
sed "s/^#define${SP}\\([A-Z0-9_][A-Z0-9_]*\\)${SP}${SP}*\\([0-9][0-9]*\\)/SYS_\\1=\\2/g" \
	< $1 | grep -v SYS__

cat <<EOF
)
EOF


#!/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="./mksyscall.pl"
mkerrors="./mkerrors.sh"
zerrors="zerrors_$GOOSARCH.go"
mksysctl=""
zsysctl="zsysctl_$GOOSARCH.go"
mksysnum=
mktypes=
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" ]] && [[ "$GOARCH" != "sparc64" ]]; then
	# Use then new 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 $(dirname "$(readlink -f "$0")"):/build generate:$GOOS
	exit
fi

GOOSARCH_in=syscall_$GOOSARCH.go
case "$GOOSARCH" in
_* | *_ | _)
	echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2
	exit 1
	;;
darwin_386)
	mkerrors="$mkerrors -m32"
	mksyscall="./mksyscall.pl -l32"
	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk macosx)/usr/include/sys/syscall.h"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
darwin_amd64)
	mkerrors="$mkerrors -m64"
	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk macosx)/usr/include/sys/syscall.h"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
darwin_arm)
	mkerrors="$mkerrors"
	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk iphoneos)/usr/include/sys/syscall.h"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
darwin_arm64)
	mkerrors="$mkerrors -m64"
	mksysnum="./mksysnum_darwin.pl $(xcrun --show-sdk-path --sdk iphoneos)/usr/include/sys/syscall.h"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
dragonfly_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="./mksyscall.pl -dragonfly"
	mksysnum="curl -s 'http://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/kern/syscalls.master' | ./mksysnum_dragonfly.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="./mksyscall.pl -l32"
	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_amd64)
	mkerrors="$mkerrors -m64"
	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
freebsd_arm)
	mkerrors="$mkerrors"
	mksyscall="./mksyscall.pl -l32 -arm"
	mksysnum="curl -s 'http://svn.freebsd.org/base/stable/10/sys/kern/syscalls.master' | ./mksysnum_freebsd.pl"
	# 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"
	;;
linux_sparc64)
	GOOSARCH_in=syscall_linux_sparc64.go
	unistd_h=/usr/include/sparc64-linux-gnu/asm/unistd.h
	mkerrors="$mkerrors -m64"
	mksysnum="./mksysnum_linux.pl $unistd_h"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_386)
	mkerrors="$mkerrors -m32"
	mksyscall="./mksyscall.pl -l32 -netbsd"
	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="./mksyscall.pl -netbsd"
	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
netbsd_arm)
	mkerrors="$mkerrors"
	mksyscall="./mksyscall.pl -l32 -netbsd -arm"
	mksysnum="curl -s 'http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_netbsd.pl"
	# 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_386)
	mkerrors="$mkerrors -m32"
	mksyscall="./mksyscall.pl -l32 -openbsd"
	mksysctl="./mksysctl_openbsd.pl"
	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_amd64)
	mkerrors="$mkerrors -m64"
	mksyscall="./mksyscall.pl -openbsd"
	mksysctl="./mksysctl_openbsd.pl"
	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
	mktypes="GOARCH=$GOARCH go tool cgo -godefs"
	;;
openbsd_arm)
	mkerrors="$mkerrors"
	mksyscall="./mksyscall.pl -l32 -openbsd -arm"
	mksysctl="./mksysctl_openbsd.pl"
	mksysnum="curl -s 'http://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/kern/syscalls.master' | ./mksysnum_openbsd.pl"
	# 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="./mksyscall_solaris.pl"
	mkerrors="$mkerrors -m64"
	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 echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go"; 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
) | $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" ]] && [[ "$GOARCH" != "sparc64" ]]; then
	if [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
		echo 1>&2 "In the new build system, mkerrors should not be called directly."
		echo 1>&2 "See README.md"
		exit 1
	fi
fi

CC=${CC:-cc}

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

uname=$(uname)

includes_Darwin='
#define _DARWIN_C_SOURCE
#define KERNEL
#define _DARWIN_USE_64_BIT_INODE
#include <stdint.h>
#include <sys/attr.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/ptrace.h>
#include <sys/socket.h>
#include <sys/sockio.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/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/mman.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 <net/ip_mroute/ip_mroute.h>
'

includes_FreeBSD='
#include <sys/capability.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/socket.h>
#include <sys/sockio.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/socket.h>
#include <sys/xattr.h>
#include <linux/if.h>
#include <linux/if_alg.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/if_tun.h>
#include <linux/if_packet.h>
#include <linux/if_addr.h>
#include <linux/falloc.h>
#include <linux/filter.h>
#include <linux/fs.h>
#include <linux/keyctl.h>
#include <linux/magic.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netlink.h>
#include <linux/perf_event.h>
#include <linux/random.h>
#include <linux/reboot.h>
#include <linux/rtnetlink.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <linux/seccomp.h>
#include <linux/sockios.h>
#include <linux/wait.h>
#include <linux/icmpv6.h>
#include <linux/serial.h>
#include <linux/can.h>
#include <linux/vm_sockets.h>
#include <linux/taskstats.h>
#include <linux/genetlink.h>
#include <linux/stat.h>
#include <linux/watchdog.h>
#include <linux/hdreg.h>
#include <net/route.h>
#include <asm/termbits.h>

#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
'

includes_NetBSD='
#include <sys/types.h>
#include <sys/param.h>
#include <sys/event.h>
#include <sys/mman.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/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/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/socket.h>
#include <sys/sockio.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/in.h>
#include <termios.h>
#include <netinet/ip.h>
#include <netinet/ip_mroute.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 !~ /^ETH_/ &&
		$2 !~ /^EPROC_/ &&
		$2 !~ /^EQUIV_/ &&
		$2 !~ /^EXPR_/ &&
		$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 ~ /^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 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
		$2 ~ /^TP_STATUS_/ ||
		$2 ~ /^FALLOC_/ ||
		$2 == "ICMPV6_FILTER" ||
		$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 ~ /^(MS|MNT|UMOUNT)_/ ||
		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
		$2 ~ /^(O|F|E?FD|NAME|S|PTRACE|PT)_/ ||
		$2 ~ /^LINUX_REBOOT_CMD_/ ||
		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
		$2 !~ "NLA_TYPE_MASK" &&
		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
		$2 ~ /^SIOC/ ||
		$2 ~ /^TIOC/ ||
		$2 ~ /^TCGET/ ||
		$2 ~ /^TCSET/ ||
		$2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
		$2 !~ "RTF_BITS" &&
		$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
		$2 ~ /^BIOC/ ||
		$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)$/ &&
		$2 ~ /^(BPF|DLT)_/ ||
		$2 ~ /^CLOCK_/ ||
		$2 ~ /^CAN_/ ||
		$2 ~ /^CAP_/ ||
		$2 ~ /^ALG_/ ||
		$2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE|IOC_(GET|SET)_ENCRYPTION)/ ||
		$2 ~ /^GRND_/ ||
		$2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
		$2 ~ /^KEYCTL_/ ||
		$2 ~ /^PERF_EVENT_IOC_/ ||
		$2 ~ /^SECCOMP_MODE_/ ||
		$2 ~ /^SPLICE_/ ||
		$2 !~ /^AUDIT_RECORD_MAGIC/ &&
		$2 ~ /^[A-Z0-9_]+_MAGIC2?$/ ||
		$2 ~ /^(VM|VMADDR)_/ ||
		$2 ~ /^IOCTL_VM_SOCKETS_/ ||
		$2 ~ /^(TASKSTATS|TS)_/ ||
		$2 ~ /^CGROUPSTATS_/ ||
		$2 ~ /^GENL_/ ||
		$2 ~ /^STATX_/ ||
		$2 ~ /^UTIME_/ ||
		$2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
		$2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
		$2 ~ /^FSOPT_/ ||
		$2 ~ /^WDIOC_/ ||
		$2 ~ /^NFN/ ||
		$2 ~ /^(HDIO|WIN|SMART)_/ ||
		$2 !~ "WMESGLEN" &&
		$2 ~ /^W[A-Z0-9]+$/ ||
		$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)' |
	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)' |
	sort >_signal.grep

echo '// mkerrors.sh' "$@"
echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
echo
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


#1/usr/bin/env bash

PROJECT_DIR=""
SMITHY_SOURCE_DIR=$(cd `dirname $0` && pwd)

usage() {
  echo "Usage: $0 [-s SMITHY_SOURCE_DIR] [-d PROJECT_DIR]" 1>&2
  exit 1
}

while getopts "hs:d:" options; do
  case "${options}" in
  s)
    SMITHY_SOURCE_DIR=${OPTARG}
    if [ "$SMITHY_SOURCE_DIR" == "" ]; then
      echo "path to smithy-go source directory is required" || exit
      usage
    fi
    ;;
  d)
    PROJECT_DIR=${OPTARG}
    ;;
  h)
    usage
    ;;
  *)
    usage
    ;;
  esac
done

if [ "$PROJECT_DIR" != "" ]; then
  cd $PROJECT_DIR || exit
fi

go mod graph | awk '{print $1}' | cut -d '@' -f 1 | sort | uniq | grep "github.com/aws/smithy-go" | while read x; do
  repPath=${x/github.com\/aws\/smithy-go/${SMITHY_SOURCE_DIR}}
  echo -replace $x=$repPath
done | xargs go mod edit


#!/bin/sh

PKGS="cpu disk docker host load mem net process"

GOOS=$(go env GOOS)
GOARCH=$(go env GOARCH)
GOARCH=$(go env GOARCH)

for DIR in . v3
do
        (cd "$DIR" || exit
        for PKG in $PKGS
        do
                if [ -e "${PKG}/types_${GOOS}.go" ]; then
                        (echo "// +build $GOOS"
                        echo "// +build $GOARCH"
                        go tool cgo -godefs "${PKG}/types_${GOOS}.go") | gofmt > "${PKG}/${PKG}_${GOOS}_${GOARCH}.go"
                fi
        done)
done


#!/usr/bin/env bash

set -eu

# this scripts is used when migrating v2 to v3.
# usage: cd ${GOPATH}/src/github.com/shirou/gopsutil && bash tools/v3migration/v3migration.sh

DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)"
ROOT=$(cd "${DIR}"/../.. && pwd)

## 1. refresh
cd "${ROOT}"

/bin/rm -rf v3

## 2. copy directories
# docker is removed, #464 will be fixed
mkdir -p v3
cp -rp cpu disk docker host internal load mem net process winservices v3
cp Makefile v3

# build migartion tool
go build -o v3/v3migration "${DIR}"/v3migration.go

V3DIR=$(cd "${ROOT}"/v3 && pwd)
cd "${V3DIR}"

## 3. mod
go mod init

###  change import path
find . -name "*.go" -print0 | xargs -0 -I@ sed -i 's|"github.com/shirou/gopsutil/|"github.com/shirou/gopsutil/v3/|g' @

############ Issues

# #429 process.NetIOCounters is pointless on Linux
./v3migration "$(pwd)" 429
sed -i '/NetIOCounters/d' process/process.go
sed -i "/github.com\/shirou\/gopsutil\/v3\/net/d" process/process_bsd.go

# #464 CgroupMem : fix typo and wrong file names
sed -i 's|memoryLimitInBbytes|memoryLimitInBytes|g' docker/docker.go
sed -i 's|memoryLimitInBbytes|memory.limit_in_bytes|g' docker/docker_linux.go
sed -i 's|memoryFailcnt|memory.failcnt|g' docker/docker_linux.go

# fix #346
sed -i 's/Soft     int32/Soft     uint64/' process/process.go
sed -i 's/Hard     int32/Hard     uint64/' process/process.go
sed -i 's| //TODO too small. needs to be uint64||' process/process.go
sed -i 's|limitToInt(val string) (int32, error)|limitToUint(val string) (uint64, error)|' process/process_*.go
sed -i 's|limitToInt|limitToUint|' process/process_*.go
sed -i 's|return int32(res), nil|return uint64(res), nil|' process/process_*.go
sed -i 's|math.MaxInt32|math.MaxUint64|' process/process_*.go

# fix #545
# variable names
sed -i 's|WritebackTmp|WriteBackTmp|g' mem/*.go
sed -i 's|Writeback|WriteBack|g' mem/*.go
sed -i 's|SReclaimable|Sreclaimable|g' mem/*.go
sed -i 's|SUnreclaim|Sunreclaim|g' mem/*.go
sed -i 's|VMallocTotal|VmallocTotal|g' mem/*.go
sed -i 's|VMallocUsed|VmallocUsed|g' mem/*.go
sed -i 's|VMallocChunk|VmallocChunk|g' mem/*.go

# json field name
sed -i 's|hostid|hostId|g' host/host.go
sed -i 's|hostid|hostId|g' host/host_test.go
sed -i 's|sensorTemperature|temperature|g' host/host.go
sed -i 's|sensorTemperature|temperature|g' host/host_test.go

sed -i 's|writeback|writeBack|g' mem/*.go
sed -i 's|writeBacktmp|writeBackTmp|g' mem/*.go
sed -i 's|pagetables|pageTables|g' mem/*.go
sed -i 's|swapcached|swapCached|g' mem/*.go
sed -i 's|commitlimit|commitLimit|g' mem/*.go
sed -i 's|committedas|committedAS|g' mem/*.go
sed -i 's|hightotal|highTotal|g' mem/*.go
sed -i 's|highfree|highFree|g' mem/*.go
sed -i 's|lowtotal|lowTotal|g' mem/*.go
sed -i 's|lowfree|lowFree|g' mem/*.go
sed -i 's|swaptotal|swapTotal|g' mem/*.go
sed -i 's|swapfree|swapFree|g' mem/*.go
sed -i 's|vmalloctotal|vmallocTotal|g' mem/*.go
sed -i 's|vmallocused|vmallocUsed|g' mem/*.go
sed -i 's|vmallocchunk|vmallocChunk|g' mem/*.go
sed -i 's|hugepagestotal|hugePagesTotal|g' mem/*.go
sed -i 's|hugepagesfree|hugePagesFree|g' mem/*.go
sed -i 's|hugepagesize|hugePageSize|g' mem/*.go
sed -i 's|pgin|pgIn|g' mem/*.go
sed -i 's|pgout|pgOut|g' mem/*.go
sed -i 's|pgfault|pgFault|g' mem/*.go
sed -i 's|pgmajfault|pgMajFault|g' mem/*.go

sed -i 's|hardwareaddr|hardwareAddr|g' net/*.go
sed -i 's|conntrackCount|connTrackCount|g' net/*.go
sed -i 's|conntrackMax|connTrackMax|g' net/*.go
sed -i 's|delete_list|deleteList|g' net/*.go
sed -i 's|insert_failed|insertFailed|g' net/*.go
sed -i 's|early_drop|earlyDrop|g' net/*.go
sed -i 's|expect_create|expectCreate|g' net/*.go
sed -i 's|expect_delete|expectDelete|g' net/*.go
sed -i 's|search_restart|searchRestart|g' net/*.go
sed -i 's|icmp_error|icmpError|g' net/*.go
sed -i 's|expect_new|expectNew|g' net/*.go

# fix no more public API/types/constants defined only for some platforms

sed -i 's|CTLKern|ctlKern|g' cpu/*.go
sed -i 's|CPNice|cpNice|g' cpu/*.go
sed -i 's|CPSys|cpSys|g' cpu/*.go
sed -i 's|CPIntr|cpIntr|g' cpu/*.go
sed -i 's|CPIdle|cpIdle|g' cpu/*.go
sed -i 's|CPUStates|cpUStates|g' cpu/*.go
sed -i 's|CTLKern|ctlKern|g' cpu/cpu_openbsd.go
sed -i 's|CTLHw|ctlHw|g' cpu/cpu_openbsd.go
sed -i 's|SMT|sMT|g' cpu/cpu_openbsd.go
sed -i 's|KernCptime|kernCptime|g' cpu/cpu_openbsd.go
sed -i 's|KernCptime2|kernCptime2|g' cpu/cpu_openbsd.go
sed -i 's|Win32_Processor|win32Processor|g' cpu/cpu_windows.go

sed -i 's|DEVSTAT_NO_DATA|devstat_NO_DATA|g' disk/*.go
sed -i 's|DEVSTAT_READ|devstat_READ|g' disk/*.go
sed -i 's|DEVSTAT_WRITE|devstat_WRITE|g' disk/*.go
sed -i 's|DEVSTAT_FREE|devstat_FREE|g' disk/*.go
sed -i 's|Devstat|devstat|g' disk/*.go
sed -i 's|Bintime|bintime|g' disk/*.go
sed -i 's|SectorSize|sectorSize|g' disk/disk_linux.go
sed -i 's|FileFileCompression|fileFileCompression|g' disk/disk_windows.go
sed -i 's|FileReadOnlyVolume|fileReadOnlyVolume|g' disk/disk_windows.go

sed -i 's|USER_PROCESS|user_PROCESS|g' host/host_*.go
sed -i 's|LSB|lsbStruct|g' host/host_linux*

sed -i 's| BcacheStats | bcacheStats |g' mem/*.go

sed -i 's|TCPStatuses|tcpStatuses|g' net/*.go
sed -i 's|CT_ENTRIES|ctENTRIES|g' net/net_linux.go
sed -i 's|CT_SEARCHED|ctSEARCHED|g' net/net_linux.go
sed -i 's|CT_FOUND|ctFOUND|g' net/net_linux.go
sed -i 's|CT_NEW|ctNEW|g' net/net_linux.go
sed -i 's|CT_INVALID|ctINVALID|g' net/net_linux.go
sed -i 's|CT_IGNORE|ctIGNORE|g' net/net_linux.go
sed -i 's|CT_DELETE|ctDELETE|g' net/net_linux.go
sed -i 's|CT_DELETE_LIST|ctDELETE_LIST|g' net/net_linux.go
sed -i 's|CT_INSERT|ctINSERT|g' net/net_linux.go
sed -i 's|CT_INSERT_FAILED|ctINSERT_FAILED|g' net/net_linux.go
sed -i 's|CT_DROP|ctDROP|g' net/net_linux.go
sed -i 's|CT_EARLY_DROP|ctEARLY_DROP|g' net/net_linux.go
sed -i 's|CT_ICMP_ERROR|ctICMP_ERROR|g' net/net_linux.go
sed -i 's|CT_EXPECT_NEW|ctEXPECT_NEW|g' net/net_linux.go
sed -i 's|CT_EXPECT_CREATE|ctEXPECT_CREATE|g' net/net_linux.go
sed -i 's|CT_EXPECT_DELETE|ctEXPECT_DELETE|g' net/net_linux.go
sed -i 's|CT_SEARCH_RESTART|ctSEARCH_RESTART|g' net/net_linux.go

sed -i 's|PageSize|pageSize|g' process/process_*.go
sed -i 's|PrioProcess|prioProcess|g' process/process_*.go
sed -i 's|ClockTicks|clockTicks|g' process/process_*.go

./v3migration "$(pwd)" issueRemoveUnusedValue

############ SHOULD BE FIXED BY HAND


#!/bin/sh

## Example in bash using STDIN signaling

counter=0

while read LINE; do
    echo "counter_bash count=${counter}"
    counter=$((counter+1))
done

trap "echo terminate 1>&2" EXIT


docker build -t local_telegraf -f scripts/alpine.docker .

docker-compose -f plugins/inputs/kibana/test_environment/docker-compose.yml up


# Usage: sh file.sh output_filename.ext
# reads from stdin and writes out to a file named on the command line.
while read line; do
  echo "$line" >> $1
done < /dev/stdin


#!/bin/bash
# no auth
mongod --dbpath data/noauth --fork --logpath /var/log/mongodb_noauth/mongod.log --bind_ip 0.0.0.0 --port 27017

# scram auth
mongod --dbpath data/scram --fork --logpath /var/log/mongodb_scram/mongod.log --bind_ip 0.0.0.0 --port 27018
mongo localhost:27018/admin --eval "db.createUser({user:\"root\", pwd:\"changeme\", roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27018/admin --eval "db.shutdownServer()"
mongod --dbpath data/scram --fork --logpath /var/log/mongodb_scram/mongod.log --auth --setParameter authenticationMechanisms=SCRAM-SHA-256 --bind_ip 0.0.0.0 --port 27018

# get client certificate subject for creating x509 authenticating user
dn=$(openssl x509 -in ./private/client.pem -noout -subject -nameopt RFC2253 | sed 's/subject=//g')

# x509 auth
mongod --dbpath data/x509 --fork --logpath /var/log/mongodb_x509/mongod.log --bind_ip 0.0.0.0 --port 27019
mongo localhost:27019/admin --eval "db.getSiblingDB(\"\$external\").runCommand({createUser:\"$dn\",roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27019/admin --eval "db.shutdownServer()"
mongod --dbpath data/x509 --fork --logpath /var/log/mongodb_x509/mongod.log --auth --setParameter authenticationMechanisms=MONGODB-X509 --tlsMode preferTLS --tlsCAFile certs/cacert.pem --tlsCertificateKeyFile private/server.pem --bind_ip 0.0.0.0 --port 27019

# x509 auth short expirey
# mongodb will not start with an expired certificate. service must be started before certificate expires. tests should be run after certificate expiry
mongod --dbpath data/x509_expire --fork --logpath /var/log/mongodb_x509_expire/mongod.log --bind_ip 0.0.0.0 --port 27020
mongo localhost:27020/admin --eval "db.getSiblingDB(\"\$external\").runCommand({createUser:\"$dn\",roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27020/admin --eval "db.shutdownServer()"
mongod --dbpath data/x509_expire --fork --logpath /var/log/mongodb_x509_expire/mongod.log --auth --setParameter authenticationMechanisms=MONGODB-X509 --tlsMode preferTLS --tlsCAFile certs/cacert.pem --tlsCertificateKeyFile private/serverexp.pem --bind_ip 0.0.0.0 --port 27020

# note about key size and mongodb
# x509 must be 2048 bytes or stronger in order for mongodb to start. otherwise you will receive similar error below
# {"keyFile":"/opt/private/server.pem","error":"error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too small"}

# copy key files to /opt/export. docker volume should point /opt/export to outputs/mongodb/dev in order to run non short x509 tests
cp /opt/certs/cacert.pem /opt/private/client.pem /opt/private/clientenc.pem /opt/export

while true; do sleep 1; done # leave container running.


#!/bin/sh

tmpdir="$(mktemp -d)"

cleanup() {
	rm -rf "$tmpdir"
}
trap cleanup EXIT

targets="$(go tool dist list)"

for target in ${targets}; do
	# only check platforms we build for
	case "${target}" in
		linux/*) ;;
		windows/*) ;;
		freebsd/*) ;;
		darwin/*) ;;
		*) continue;;
	esac

	GOOS=${target%%/*} GOARCH=${target##*/} \
		go list -deps -f '{{with .Module}}{{.Path}}{{end}}' ./cmd/telegraf/ >> "${tmpdir}/golist"
done

for dep in $(LC_ALL=C sort -u "${tmpdir}/golist"); do
	case "${dep}" in
		# ignore ourselves
		github.com/influxdata/telegraf) continue;;

		# dependency is replaced in go.mod
		github.com/satori/go.uuid) continue;;

		# go-autorest has a single license for all sub modules
		github.com/Azure/go-autorest/autorest)
			dep=github.com/Azure/go-autorest;;
		github.com/Azure/go-autorest/*)
			continue;;

		# single license for all sub modules
		cloud.google.com/go/*)
			continue;;
	esac

	# Remove single and double digit version from path; these are generally not
	# actual parts of the path and instead indicate a branch or tag.
	#   example: github.com/influxdata/go-syslog/v2 -> github.com/influxdata/go-syslog
	dep="${dep%%/v[0-9]}"
	dep="${dep%%/v[0-9][0-9]}"

	echo "${dep}" >> "${tmpdir}/HEAD"
done

# If there are two versions of a library that have the same base (like
# github.com/foo/bar github.com/foo/bar/v3) there will be a duplicate
# in the list.  Remove duplicates again.
mv "${tmpdir}/HEAD" "${tmpdir}/HEAD-dup"
uniq "${tmpdir}/HEAD-dup" > "${tmpdir}/HEAD"

grep '^-' docs/LICENSE_OF_DEPENDENCIES.md | grep -v github.com/DataDog/datadog-agent | cut -f 2 -d' ' > "${tmpdir}/LICENSE_OF_DEPENDENCIES.md"

diff -U0 "${tmpdir}/LICENSE_OF_DEPENDENCIES.md" "${tmpdir}/HEAD" || {
cat - <<EOF

The docs/LICENSE_OF_DEPENDENCIES.md file does not contain the expected entries.

Lines prefixed with '+' should be added to LICENSE_OF_DEPENDENCIES.md and '-'
lines should be removed.

Include a link to the appropriate licenses for any additions.
EOF
exit 1
}


#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

usage () {
    echo "Check that no dynamic symbols provided by glibc are newer than a given version"
    echo "Usage:"
    echo "   $0 program version"
    echo "where program is the elf binary to check and version is a dotted version string like 2.3.4"
    exit 1
}

#validate input and display help
[[ $# = 2 ]] || usage
prog=$1
max=$2

#make sure dependencies are installed
have_deps=true
for i in objdump grep sort uniq sed; do
    if ! command -v "$i" > /dev/null; then
	echo "$i not in path"
	have_deps=false
    fi
done
if [[ $have_deps = false ]]; then
    exit 1
fi

#compare dotted versions
#see https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
vercomp () {
    if [[ $1 == $2 ]]
    then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            return 2
        fi
    done
    return 0
}

if ! objdump -p "$prog" | grep -q NEEDED; then
    echo "$prog doesn't have dynamic library dependencies"
    exit 0
fi

objdump -T "$prog" | # get the dynamic symbol table
    sed -n "s/.* GLIBC_\([0-9.]\+\).*/\1/p" | # find the entries for glibc and grab the version
    sort | uniq | # remove duplicates
    while read v; do
        set +e
        vercomp "$v" "$max" # fail if any version is newer than our max
        comp=$?
        set -e
        if [[ $comp -eq 1 ]]; then
            echo "$v is newer than $max"
            exit 1
        fi
    done

exit 0


#!/bin/bash
# CIRCLE-CI SCRIPT: This file is used exclusively for CI
# To prevent the tests/builds to run for only a doc change, this script checks what files have changed in a pull request.

BRANCH="$(git rev-parse --abbrev-ref HEAD)"
echo $BRANCH
if [[ ${CIRCLE_PULL_REQUEST##*/} != "" ]]; then # Only skip if their is an associated pull request with this job
    # Ask git for all the differences between this branch and master
    # Then use grep to look for changes in the .circleci/ directory, anything named *.go or *.mod or *.sum or *.sh or Makefile
    # If no match is found, then circleci step halt will stop the CI job but mark it successful
    git diff master --name-only --no-color | egrep -e "^(\.circleci\/.*)$|^(.*\.(go|mod|sum|sh))$|^Makefile$" || circleci step halt;
fi


#!/bin/bash
# This script is responsible for generating the Telegraf config found under the `etc` directory.
# This script is meant to be only ran in within the Circle CI pipeline so that the Tiger Bot can update them automatically.
# It supports Windows and Linux because the configs are different depending on the OS.

os=$1 # windows or linux
exe_path="/build/extracted" # Path will contain telegraf binary
config_name="telegraf.conf"

if [ "$os" = "windows" ]; then
    zip=$(/bin/find ./build/dist -maxdepth 1 -name "*windows_amd64.zip" -print)
    exe_path="$PWD/build/extracted"
    unzip "$zip" -d "$exe_path"
    config_name="telegraf_windows.conf"
    exe_path=$(/bin/find "$exe_path" -name telegraf.exe -type f -print)
else
    tar_path=$(find /build/dist -maxdepth 1 -name "*linux_amd64.tar.gz" -print | grep -v ".*static.*")
    mkdir "$exe_path"
    tar --extract --file="$tar_path" --directory "$exe_path"
    exe_path=$(find "$exe_path" -name telegraf -type f -print | grep ".*usr/bin/.*")
fi

$exe_path config > $config_name

mkdir ./new-config
mv $config_name ./new-config