#!/bin/sh
firmware="powerfip-firmware"
library="libpowerfip"

PWRFIP_FW_DIR=/usr/local/lib/firmware
PWRFIP_HDR_DIR=/usr/local/include/powerfip
PWRFIP_LIB_DIR=/usr/local/lib

ROOT_DIR=$(pwd)
MACHINE=$(uname -m)

# create the firmware directory if it does not exist
if [ ! -d "${PWRFIP_FW_DIR}" ];
then
    mkdir ${PWRFIP_FW_DIR}
fi

# create the header (*.h) directory if it does not exist
if [ ! -d "${PWRFIP_HDR_DIR}" ];
then
    mkdir ${PWRFIP_HDR_DIR}
fi

# copy the firmware to the target directory
cp ./firmware/${firmware}.bin ${PWRFIP_FW_DIR}

# copy the static/shared library to the target directory
cp ./lib/${MACHINE}/static/${library}.a ${PWRFIP_LIB_DIR}
cp ./lib/${MACHINE}/${library}.so.* ${PWRFIP_LIB_DIR}

# copy the headers to the target directory
cp ./include/libpowerfip.h ${PWRFIP_HDR_DIR}
cp ./include/mbox-common.h ${PWRFIP_HDR_DIR}
cp ./include/powerfip-common.h ${PWRFIP_HDR_DIR}
cp ./include/powerfip-drv.h ${PWRFIP_HDR_DIR}
cp ./include/powerfip-mbox-common.h ${PWRFIP_HDR_DIR}

# extract lib version and create shared library symbolic links
cd ${ROOT_DIR}/lib/${MACHINE}
if [ `ls | grep -Po '(?<=libpowerfip.so.)\d.\d.\d' | wc -l` -eq 1 ];
then
    PWRFIP_LIB_VER="$(ls | grep -Po '(?<=libpowerfip.so.)\d.\d.\d')"
    major=`echo ${PWRFIP_LIB_VER} | cut -d. -f1`
    minor=`echo ${PWRFIP_LIB_VER} | cut -d. -f2`
    patch=`echo ${PWRFIP_LIB_VER} | cut -d. -f3`
    # create symbolic links
    cd ${PWRFIP_LIB_DIR}
    ln -s ${library}.so.${major}.${minor}.${patch} ${library}.so.${major}
    ln -s ${library}.so.${major} ${library}.so
fi
