#!/bin/bash

# This is script for the automated building cprocsp-pki-cades-plugin for Porteus
# https://cryptopro.ru/products/cades/downloads
# Version 2025-02-16

# Copyright 2019 Blaze, Dankov, Russia
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# root check
if [ `whoami` != "root" ]; then
    echo -e "\nOnly root can run this.\n"
    exit 1
fi

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

cleanup(){
[ -d $TMPDIR ] && rm -rf $TMPDIR
[ -d $PKG ] && rm -rf $PKG
exit	
}

if [[ "$ARCH" == i686 || "$ARCH" == i586 ]]; then
    PRGNAM=${PRGNAM:-cprocsp-pki-cades-plugin}
    SOURCE=${SOURCE:-cades-linux-ia32}
    BOLD=${BOLD:-"\e[1m"}
    CYAN=${CYAN:-"\e[96m"}
    GREEN=${GREEN:-"\e[92m"}
    RED=${RED:-"\e[31m"}
    RESET=${RESET:-"\e[0m"}
    CWD=$(pwd)
    TMPDIR=/tmp/portch
    PKG=$TMPDIR/package-$PRGNAM
    PKGINFO=$PKG/var/lib/pkgtools/packages
    OUTPUT=${OUTPUT:-/tmp}
    mkdir -p $TMPDIR $PKG $OUTPUT
    cd $PKG
    wget -q --show-progress https://cryptopro.ru/sites/default/files/products/cades/current_release_2_0/$SOURCE.tar.gz
    tar xf $SOURCE.tar.gz && rm -f $SOURCE.tar.gz && mv $PKG/$SOURCE/* $PKG && rm -rf $PKG/$SOURCE
    for i in `find . -type f | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    VERSION=`cat $PKG/opt/cprocsp/include/cadessdk_version.txt | tr -d '"'`
    rm -f *.deb *.rpm && rm -rf $PKG/opt/cprocsp/{include,src}

### fake Slackware type package info: super dumb version
mkdir -p $PKGINFO
echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > $PKGINFO/$PRGNAM-$VERSION-$ARCH

cat >> $PKGINFO/$PRGNAM-$VERSION-$ARCH << EOM
PACKAGE DESCRIPTION:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin is designed for creating and verifying
cprocsp-pki-cades-plugin: a digital signature on web pages and supports working with
cprocsp-pki-cades-plugin: a wide range of algorithms, both built into the operating system
cprocsp-pki-cades-plugin: and optionally installed.
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin home: https://www.cryptopro.ru/products/cades/plugin
cprocsp-pki-cades-plugin:
FILE LIST:
EOM

find * | grep -v var >> $PKGINFO/$PRGNAM-$VERSION-$ARCH

dir2xzm $PKG $OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm

    # check on cprocsp-pki-cades-plugin xzm file exists in /tmp
    if [ -f "$OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm" ]; then
        echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
    else
        echo -e "\n${RED}${BOLD}Faile. Your $PRGNAM module is not built.${RESET}\n"
    fi
fi

if [[ "$ARCH" == x86_64 ]]; then
    PRGNAM=${PRGNAM:-cprocsp-pki-cades-plugin}
    SOURCE=${SOURCE:-cades-linux-amd64}
    BOLD=${BOLD:-"\e[1m"}
    CYAN=${CYAN:-"\e[96m"}
    GREEN=${GREEN:-"\e[92m"}
    RED=${RED:-"\e[31m"}
    RESET=${RESET:-"\e[0m"}
    CWD=$(pwd)
    TMPDIR=/tmp/portch
    PKG=$TMPDIR/package-$PRGNAM
    PKGINFO=$PKG/var/lib/pkgtools/packages
    OUTPUT=${OUTPUT:-/tmp}
    mkdir -p $TMPDIR $PKG $OUTPUT
    cd $PKG
    wget -q --show-progress https://cryptopro.ru/sites/default/files/products/cades/current_release_2_0/$SOURCE.tar.gz
    tar xf $SOURCE.tar.gz && rm -f $SOURCE.tar.gz && mv $PKG/$SOURCE/* $PKG && rm -rf $PKG/$SOURCE
    for i in `find . -type f | fgrep .rpm | sort`; do rpm2cpio $i | cpio -idmv &>/dev/null; done
    VERSION=`cat $PKG/opt/cprocsp/include/cadessdk_version.txt | tr -d '"'`
    rm -f *.deb *.rpm && rm -rf $PKG/opt/cprocsp/{include,src}

### fake Slackware type package info: super dumb version
mkdir -p $PKGINFO
echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > $PKGINFO/$PRGNAM-$VERSION-$ARCH

cat >> $PKGINFO/$PRGNAM-$VERSION-$ARCH << EOM
PACKAGE DESCRIPTION:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin is designed for creating and verifying
cprocsp-pki-cades-plugin: a digital signature on web pages and supports working with
cprocsp-pki-cades-plugin: a wide range of algorithms, both built into the operating system
cprocsp-pki-cades-plugin: and optionally installed.
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin:
cprocsp-pki-cades-plugin: cprocsp-pki-cades-plugin home: https://www.cryptopro.ru/products/cades/plugin
cprocsp-pki-cades-plugin:
FILE LIST:
EOM

find * | grep -v var >> $PKGINFO/$PRGNAM-$VERSION-$ARCH

dir2xzm $PKG $OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm

    # check on cprocsp-pki-cades-plugin xzm file exists in /tmp
    if [ -f "$OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm" ]; then
        echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$VERSION-$ARCH.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
    else
        echo -e "\n${RED}${BOLD}Faile. Your $PRGNAM module is not built.${RESET}\n"
    fi
fi
cleanup