#!/bin/bash
##****************************************************************************
##
## Copyright (c) 2009-24 Broadcom Corporation
##
## This program is the proprietary software of Broadcom Corporation and/or
## its licensors, and may only be used, duplicated, modified or distributed
## pursuant to the terms and conditions of a separate, written license
## agreement executed between you and Broadcom (an "Authorized License").
## Except as set forth in an Authorized License, Broadcom grants no license
## (express or implied), right to use, or waiver of any kind with respect to
## the Software, and Broadcom expressly reserves all rights in and to the
## Software and all intellectual property rights therein.  IF YOU HAVE NO
## AUTHORIZED LICENSE, THEN YOU HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY,
## AND SHOULD IMMEDIATELY NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE
## SOFTWARE.
##
## Except as expressly set forth in the Authorized License,
##
## 1.     This program, including its structure, sequence and organization,
## constitutes the valuable trade secrets of Broadcom, and you shall use all
## reasonable efforts to protect the confidentiality thereof, and to use this
## information only in connection with your use of Broadcom integrated circuit
## products.
##
## 2.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED
## "AS IS" AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS
## OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
## RESPECT TO THE SOFTWARE.  BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL
## IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR
## A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
## ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME
## THE ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE.
##
## 3.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM
## OR ITS LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL,
## INDIRECT, OR EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY
## RELATING TO YOUR USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM
## HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN
## EXCESS OF THE AMOUNT ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1,
## WHICHEVER IS GREATER. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY
## FAILURE OF ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.
##
##****************************************************************************

gccVersion="$(mipsisa32-elf-gcc -dumpversion)"
if [ "$gccVersion" == "7.3.0" ] ; then
   echo "gccVersion is correct $gccVersion"
else
   echo "Bad gccVersion $gccVersion"
   echo "Bad gccRoot $gccRoot"
   PATH="/tools/ecos/gnutools/gnutools_730_brcm/bin:$PATH"
   gccRoot=`which mipsisa32-elf-g++`
   gccRoot=${gccRoot%/mipsisa32-elf/bin/mipsisa32-elf-g++}
   gccVersion="$(mipsisa32-elf-gcc -dumpversion)"
   echo "New gccVersion $gccVersion"
   echo "New gccRoot $gccRoot"
fi

fetchFlag=0
cleanFlag=0
openSSHTarball=""
version="7.6p1"

function error 
{
   echo "ERROR: "$1
   exit 1
}

function parse_parameters 
{
   while getopts ":v:hfc" opt; do
      case $opt in
       v)
        version=$OPTARG
        ;;

       c)
          cleanFlag=1
          ;;
       f)
          fetchFlag=1
          ;;
       h)
          echo "build script parameters"
          echo "-f   : Fetch the source tarball from ftp.openssh.org"
          echo "-v   : Specify version of OpenSSH to build. If not an existing version, use the -f flag to force a fetch"
          echo "-h   : This"
          exit 1
          ;;
       \?)
         error "Invalid option: -$OPTARG"
         ;;
       :)
         error "Option -$OPTARG requires an argument."
         ;;
      esac
   done
}

function fetch_tarball 
{
   keep=0

   # Remove tarball if it exists since we're going to fetch a new
   # one anyway.
   if [ -f $openssh_tarball ]; then
      echo "$openssh_tarball exists. Use or re-download? [Y/n]"
      read keep
      if [ "$keep" = "n" ]; then
         rm $openssh_tarball
      fi
   fi

   if [ "$keep" = "n" ]; then
      echo Fetching $openssh_tarball from ftp.openssh.org
      wget ftp://ftp.openssh.org/source/$openssh_tarball
      if [ $? -ne 0 ]; then
         error "Could not retrieve $openssh_tarball from ftp.openssh.org"
         exit 1
      fi
   fi

   # If extracted version already exists, ask user if they want to overwrite
   if [ -d $openssh_dir ]; then
      echo "$openssh_dir exists.  Overwrite? [y/N]"
      read overwrite
      if [ "$overwrite" = "y" ]; then
         rm -rf $openssh_dir
      else
         exit 1
      fi
   fi

   # Untar it
   tar -xzf $openssh_tarball
   if [ $? -ne 0 ]; then
      error "Could not unpack $openSSHTarball from ftp.openssh.org"
      exit 1
   fi
}

# Patch OpenSSH source
function patch_openssh
{
   echo "Appling diffset."
   cp ../openssh-$version.diff .
   patch -p1 < openssh-$version.diff
   rm openssh-$version.diff
   cp ../ecos/makefile .
   rm -fr contrib regress openbsd-compat/regress m4 .github
}


# Build the OpenSSH libraries. This function will first patch the OpenSSH source
# and then run the standard "configure" script.  The goal is to keep the build process
# as close to the standard openssh build as possible.
function build_openssh
{
   cd $openssh_dir
   patch_openssh
   make
}

function clean_openssh 
{
   echo Deleting $openssh_dir ...
   rm -rf $openssh_dir
   echo Extracting $openssh_tarball ...
   tar zxvf $openssh_tarball
   echo Finshed!
   exit 1
}

# Start of main

# First let's parse the paramets
parse_parameters "$@"

openssh_dir=openssh-$version
openssh_tarball=$openssh_dir.tar.gz

echo "Building revision: $version"

# See if we need to retrieve the tarball
if [ $fetchFlag = 1 ]; then
   fetch_tarball
fi

# Clean
if [ $cleanFlag = 1 ]; then
   clean_openssh
fi

# Build the code
build_openssh

echo "All done"

exit 0

