set -x
install $SELINUX -d -m 755 "$DESTDIR$CNIDIR"
install $SELINUX -D -m 755 -t "$DESTDIR$OPT_CNI_BIN_DIR" cni-plugins/*
install $SELINUX -D -m 644 -t "$DESTDIR$CNIDIR" contrib/10-crio-bridge.conf
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/conmon
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/crictl
install $SELINUX -d -m 755 "$DESTDIR$BASHINSTALLDIR"
install $SELINUX -d -m 755 "$DESTDIR$FISHINSTALLDIR"
install $SELINUX -d -m 755 "$DESTDIR$ZSHINSTALLDIR"
install $SELINUX -d -m 755 "$DESTDIR$CONTAINERS_DIR"
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/crio-status
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/crio
install $SELINUX -D -m 644 -t "$DESTDIR$ETCDIR" etc/crictl.yaml
install $SELINUX -D -m 644 -t "$DESTDIR$OCIDIR" etc/crio-umount.conf
install $SELINUX -D -m 644 -t "$DESTDIR$ETCDIR/crio" etc/crio.conf
install $SELINUX -D -m 644 -t "$DESTDIR$ETCDIR/crio/crio.conf.d" etc/10-crun.conf
install $SELINUX -D -m 644 -t "$DESTDIR$MANDIR/man5" man/crio.conf.5
install $SELINUX -D -m 644 -t "$DESTDIR$MANDIR/man5" man/crio.conf.d.5
install $SELINUX -D -m 644 -t "$DESTDIR$MANDIR/man8" man/crio-status.8
install $SELINUX -D -m 644 -t "$DESTDIR$MANDIR/man8" man/crio.8
install $SELINUX -D -m 644 -t "$DESTDIR$BASHINSTALLDIR" completions/bash/crio
install $SELINUX -D -m 644 -t "$DESTDIR$FISHINSTALLDIR" completions/fish/crio.fish
install $SELINUX -D -m 644 -t "$DESTDIR$ZSHINSTALLDIR" completions/zsh/_crio
install $SELINUX -D -m 644 -t "$DESTDIR$CONTAINERS_DIR" contrib/policy.json
install $SELINUX -D -m 644 -t "$DESTDIR$SYSTEMDDIR" contrib/crio.service
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/pinns
install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/crun

# only install runc if it's not already in the path
if ! command -v runc; then
    install $SELINUX -D -m 755 -t "$DESTDIR$BINDIR" bin/runc
fi

if [ -n "$SELINUX" ]; then
    if command -v chcon >/dev/null; then
        chcon -u system_u -r object_r -t container_runtime_exec_t \
            "$DESTDIR$BINDIR/crio" \
            "$DESTDIR$BINDIR/crio-status" \
            "$DESTDIR$BINDIR/crun"

        if [ "$ARCH" = amd64 ]; then
            chcon -u system_u -r object_r -t container_runtime_exec_t \
                "$DESTDIR$BINDIR/runc"
        fi

        chcon -u system_u -r object_r -t bin_t \
            "$DESTDIR$BINDIR/conmon" \
            "$DESTDIR$BINDIR/crictl" \
            "$DESTDIR$BINDIR/pinns"

        chcon -R -u system_u -r object_r -t bin_t \
            "$DESTDIR$OPT_CNI_BIN_DIR"

        chcon -R -u system_u -r object_r -t container_config_t \
            "$DESTDIR$ETCDIR/crio" \
            "$DESTDIR$OCIDIR/crio-umount.conf"

        chcon -R -u system_u -r object_r -t systemd_unit_file_t \
            "$DESTDIR$SYSTEMDDIR/crio.service"
    fi
fi
