#!/bin/sh
#
# Script to update the ship in files
#
# Note: Intended to be used only on vendor/qcom/proprietary/* contents
#

# usage information

usage()
{
cat <<USAGE

    Usage: updatepack [-t target] [-v variant] [-c command]
    [-f file to add/delete/move] [-n new location in case of move]

    Adds, deletes or moves files in the shipin file as per the
    arguments specified. Needs to be run from the directory whose
    files need to be updated in the ship in files.

    Required arguments:

      -v variant    CRM variant (e.g. HY11, HY22, ...)
      -c command    add/delete/move
      -f file       relative path to the file to add, delete or move
                    only files in vendor/qcom/proprietary allowed

    Optional arguments:

      -t target     specify target only if the ship file is target
                    specific (e.g. msm8660_surf, msm7630_fusion, ...)
      -n new file   new location of the file only in case of a move
                    relative path to the new location of the file
                    only locations in vendor/qcom/proprietary allowed
    Eg:

    $ ~path> sh updatepack.sh -v Hy11 -t msm8660_surf -c add -f i2c

    Add all the files in ~path\i2c to the ~target/msm8660_surf/ship_target_specific_HY11
    ship file

    $ ~path> sh updatepack.sh -v Hy11 -t msm8660_surf -c delete -f i2c\config

    Delete the file ~path\i2c\config from ~proprietary\release\crm\ship_common_HY11 ship file

USAGE
}

# args : absolute path of the file/directory that needs to be added
# adds the file to the corresponding ship file in the required format

function addFile {

    while read data
    do
        relpath=$(echo $data | sed 's|\/|\\|g' | sed 's|vendor|\nvendor|' | grep vendor)

        echo LINUX\\android\\${relpath} HY11_1\\LINUX\\android\\${relpath} >> ship_file_temp

    done

    sort ship_file_temp | uniq > $ship_file
    rm ship_file_temp
}

# args : absolute path of the file/directory that needs to be deleted
# deletes the file or all the files from the directory specified

function deleteFile {

    while read data
    do
        relpath=$(echo $data | sed 's|\/|\\\\|g' | sed 's|vendor|\nvendor|' | grep vendor)

        sed -i "/$(echo ${relpath})/d" ship_file_temp

    done

    mv ship_file_temp $ship_file
}

common=1
while getopts ":t:v:c:f:n:" opt; do
    case ${opt} in
    t)
        target=${OPTARG}
        common=0
        ;;
    v)
        variant=$(echo ${OPTARG} | tr '[a-z]' '[A-Z]')
        ;;
    c)
        command=$(echo ${OPTARG} | tr '[a-z]' '[A-Z]')
        ;;
    f)
        dirnamestr=`dirname "${OPTARG}"`
        basenamestr=`basename "${OPTARG}"`

        # getting the absolute path
        file="`cd \"$dirnamestr\" 2>/dev/null && pwd || echo \"$dirnamestr\"`/$basenamestr"

        ;;
    n)
        dirnamestr=`dirname "${OPTARG}"`
        basenamestr=`basename "${OPTARG}"`

        # getting the absolute path
        newloc="`cd \"$dirnamestr\" 2>/dev/null && pwd || echo \"$dirnamestr\"`/$basenamestr"

        ;;
    *)
        usage
        exit 1
        ;;
    esac
done

if [ -z ${variant} ] || [ -z ${command} ] || [ -z ${file} ] ; then
    usage
    exit 1
fi

# getting the absolute path of the ship file to update

if [ $common == 1 ]; then

    ship_file=$(pwd | sed "s/proprietary.*//")proprietary/release/crm/ship_common_${variant}

elif [ $common == 0 ]; then

    if [ -z ${target} ]; then
        usage
        exit 1
    fi

    ship_file=$(pwd | sed "s/proprietary.*//")proprietary/release/crm/target/${target}/ship_target_specific_${variant}

else

    usage
    exit 1

fi

if [ ! -f $ship_file ];
then
   echo "ERROR : File $ship_file does not exist."
   exit 1
else
    cp $ship_file ship_file_temp
fi

# command processing

if [ -z $(echo $file | grep vendor/qcom/proprietary) ]; then

    echo ERROR : $file not in vendor/qcom/proprietary
    echo ERROR : only files in vendor/qcom/proprietary allowed

    exit 1
fi

if [ $command == "ADD" ]; then

    if [ ! -f $file ] && [ ! -d $file ];
    then
        echo "ERROR : $file does not exist."
        exit 1
    fi

    if [ -d $file ]; then

        # prune out .git from the list of directories

        find $file -type d -name .git -prune -o \
        -type f -print | addFile

    else

        echo $file | addFile
    fi

elif [ $command == "DELETE" ]; then

    if [ -d $file ]; then

        find $file -type d -name .git -prune -o \
        -type f -print | deleteFile

    else

        echo $file | deleteFile

    fi

elif [ $command == "MOVE" ]; then

    if [ -z ${newloc} ]; then
        usage
        exit 1
    fi

    if [ ! -f $newloc ] && [ ! -d $newloc ];
    then
        echo "ERROR : $newloc does not exist."
        exit 1
    fi

    echo $file | deleteFile
    echo $newloc | addFile

else

    usage
    exit 1
fi
