# vim:set ft=sh:

SHORTNAME=quake
LONGNAME=Quake

quake_usage() {
       echo "game-data-packager ${SHORTNAME} arguments:"
        printf "\tgame-data-packager ${SHORTNAME} path
\t\t-m path\t\tpath to a mounted Quake CD-ROM\n\
\t\t-d path\t\tpath to an unpacked Quake directory\n\
\t\t-s path\t\tpath to a Quake shareware ZIP\n\
\t\t-mp1 path\t\tpath to an unpacked Scourge of Armagon directory\n\
\t\t\t\tor a mounted Scourge of Armagon CD\n\
\t\t-mp2 path\t\tpath to an unpacked Dissolution of Eternity directory\n\
\t\t\t\tor a mounted Dissolution of Eternity CD\n\
\t\tpath\t\tpath to any of the above (game-data-packager will guess)\n"
}

mountpoint=""
method="guess"
verify_args() {
    case $# in
        0)
            quake_usage
            exit 0
            ;;
        1)
            mountpoint="$1"
            ;;
        2)
            mountpoint="$2"
            if [ "$1" = "-m" ]; then
                method="cdrom"
            elif [ "$1" = "-d" ]; then
                method="dir"
            elif [ "$1" = "-s" ]; then
                method="swzip"
            elif [ "$1" = "-mp1" ]; then
                method="mp1"
            elif [ "$1" = "-mp2" ]; then
                method="mp2"
            else
                usage >&2
                quake_usage >&2
                exit 1
            fi
            ;;
        *)
            usage >&2
            quake_usage >&2
            exit 1
            ;;
    esac
}

go() {
    
    verify_args "$@"
    case "$method" in
        "guess")
            guess_method
            ;;
        "cdrom")
            cdrom_method
            ;;
        "dir")
            dir_method
            ;;
        "swzip")
            swzip_method
            ;;
        "mp1")
            mp1_method
            ;;
        "mp2")
            mp2_method
            ;;
        *)
            die "internal error"
            ;;
     esac
}

guess_method() {
    if ifind "$mountpoint" "id1/pak0.pak" >/dev/null; then
        debug "treating $mountpoint like an installed directory"
        method="dir"
        dir_method
    elif [ -f "$mountpoint/q101_int.1" ]; then
        debug "treating $mountpoint like a CD-ROM"
        method="cdrom"
        cdrom_method
    elif [ -f "$mountpoint" ]; then
        debug "treating $mountpoint like a shareware ZIP"
        method="swzip"
        swzip_method
    elif [ -f "$mountpoint/hipnotic/pak0.pak" ]; then
        debug "treating $mountpoint like a Scourge of Armagon directory or CD"
        method="mp1"
        mp1_method
    elif [ -f "$mountpoint/rogue/pak0.pak" ]; then
        debug "treating $mountpoint like a Dissolution of Eternity directory or CD"
        method="mp2"
        mp2_method
    else
        die "couldn't figure out what method to use for mountpoint $mountpoint"
    fi
}

require_lha() {
    which lha >/dev/null|| die "package 'lha' is required for this installation method."
}

cdrom_method() {
    bit1="$mountpoint/q101_int.1"
    bit2="$mountpoint/q101_int.2"
    bit1sum=752f49131bb3ba832346e873c1bcfdc6
    bit2sum=9ed67b39020575771e29645d977c9216
    
    require_lha
    verify_file "$bit1"
    verify_file "$bit2"
    verify_md5sum "$bit1" "$bit1sum"
    verify_md5sum "$bit2" "$bit2sum"
    
    cat "$bit1" "$bit2" > "$WORKDIR/unpackme.exe"
    (
      cd "$WORKDIR"
      lha xq unpackme.exe
      rm unpackme.exe
    )
    
    pak0="$WORKDIR/id1/pak0.pak"
    if ! test -f "$pak0"; then
        pak0="$WORKDIR/ID1/PAK0.PAK"
    fi
    pak1="$WORKDIR/id1/pak1.pak"
    if ! test -f "$pak1"; then
        pak1="$WORKDIR/ID1/PAK1.PAK"
    fi
    # This CD contains version 1.01
    pak0sum=85fc9cee2035b66290da1e33be2ac86b
    pak1sum=d76b3e5678f0b64ac74ce5e340e6a685
    suffix=registered
    folder=id1
    common_method
}

dir_method() {
    pak0=$(ifind "$mountpoint" "id1/pak0.pak" | head -1)
    pak1=$(ifind "$mountpoint" "id1/pak1.pak" | head -1)
    # Presume the directory contains version 1.06
    pak0sum=5906e5998fc3d896ddaf5e6a62e03abb
    pak1sum=d76b3e5678f0b64ac74ce5e340e6a685
    suffix=registered
    folder=id1
    common_method
}

swzip_method() {
    require_lha
    zipfile=$(unravel "$mountpoint")
    (
        cd "$WORKDIR"
        gdp_unzip "$zipfile" resource.1
        mv resource.1 resource.exe
        lha xq resource.exe id1/pak0.pak
        rm resource.exe
    )
    pak0="$WORKDIR/id1/pak0.pak"
    pak1=""

    # Presume the shareware version is 1.06
    pak0sum=5906e5998fc3d896ddaf5e6a62e03abb
    suffix=shareware
    folder=id1
    common_method
}

mp1_method() {
    pak0="$mountpoint/hipnotic/pak0.pak"
    # MD5 sum for original CD version
    pak0sum=0ab83681aaf841c4320269e02941a14a
    # MD5 sum for glQuake-compatible version (see
    # http://speeddemosarchive.com/quake/qdq/movies/sds.html for more
    # info)
    pak0sum=$pak0sum,f05b7452ae725e060fb3d14f7731fb00
    pak1=""
    suffix=armagon
    folder=hipnotic
    common_method
}

mp2_method() {
    pak0="$mountpoint/rogue/pak0.pak"
    # MP2 CD
    pak0sum=f8898a483b131de21581656c94f3c1a4
    # MD5 sum for Steam-supplied version
    pak0sum=$pak0sum,f71428763229bdb373f00a547296efe3
    # MP2 as supplied with MP1 on a single CD
    pak0sum=$pak0sum,c38a4e04219c317cd1b02f386bdfe11f
    pak1=""
    suffix=dissolution
    folder=rogue
    common_method
}

# Requires the following variables on entry:
# pak0 - full path of the pak0.pak file to include
# pak0sum - expected MD5 sums (comma-separated)
# pak1 - full path of the pak1.pak file to include (optional)
# pak1sum - expected MD5 sums (comma-separated)
# suffix - package suffix to use
# folder - base folder for the PAK files
common_method() {
    verify_file "$pak0"
    verify_md5sum_alternatives "$pak0" "$pak0sum"
    [ "$pak1" = "" ] || verify_file "$pak1"
    [ "$pak1" = "" ] || verify_md5sum_alternatives "$pak1" "$pak1sum"

    DEBBASE="quake-${suffix}_${GAME_PACKAGE_VERSION}_all.deb"
    OUTFILE=`unravel "$OUTDIR"`"/$DEBBASE"
    cp -p "$DATADIR/$DEBBASE" "$OUTFILE"

    # force lower case for our copy of the files
    ln -s "$pak0" "$WORKDIR/pak0.pak"
    [ "$pak1" = "" ] || ln -s "$pak1" "$WORKDIR/pak1.pak"

    if [ "$pak1" = "" ]; then
        slipstream "$OUTFILE" "usr/share/games/quake/${folder}/" \
            "$WORKDIR/pak0.pak"
    else
        slipstream "$OUTFILE" "usr/share/games/quake/${folder}/" \
            "$WORKDIR/pak0.pak" "$WORKDIR/pak1.pak"
    fi
    [ -d "$WORKDIR/${folder}" ] && rm -rf "$WORKDIR/${folder}"
    [ ! -f "$WORKDIR/pak1.pak" ] || rm "$WORKDIR/pak1.pak"
    rm "$WORKDIR/pak0.pak"
}
